BaseView.js 6 KB
Newer Older
1
import React, { Component, Fragment } from 'react';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
2
import { formatMessage, FormattedMessage } from 'umi/locale';
3
import { Form, Input, Upload, Select, Button } from 'antd';
ๆ„š้“'s avatar
ๆ„š้“ committed
4
import { connect } from 'dva';
5 6 7
import styles from './BaseView.less';
import GeographicView from './GeographicView';
import PhoneView from './PhoneView';
8
// import { getTimeDistance } from '@/utils/utils';
9 10 11 12 13 14 15

const FormItem = Form.Item;
const { Option } = Select;

// ๅคดๅƒ็ป„ไปถ ๆ–นไพฟไปฅๅŽ็‹ฌ็ซ‹๏ผŒๅขžๅŠ ่ฃๅ‰ชไน‹็ฑป็š„ๅŠŸ่ƒฝ
const AvatarView = ({ avatar }) => (
  <Fragment>
16 17 18
    <div className={styles.avatar_title}>
      <FormattedMessage id="app.settings.basic.avatar" defaultMessage="Avatar" />
    </div>
19 20 21 22 23
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
24
        <Button icon="upload">
25
          <FormattedMessage id="app.settings.basic.change-avatar" defaultMessage="Change avatar" />
26
        </Button>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      </div>
    </Upload>
  </Fragment>
);

const validatorGeographic = (rule, value, callback) => {
  const { province, city } = value;
  if (!province.key) {
    callback('Please input your province!');
  }
  if (!city.key) {
    callback('Please input your city!');
  }
  callback();
};

const validatorPhone = (rule, value, callback) => {
  const values = value.split('-');
  if (!values[0]) {
    callback('Please input your area code!');
  }
  if (!values[1]) {
    callback('Please input your phone number!');
  }
  callback();
};

ๆ„š้“'s avatar
ๆ„š้“ committed
54 55 56
@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
57
@Form.create()
58
class BaseView extends Component {
59 60 61
  componentDidMount() {
    this.setBaseInfo();
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
62

63
  setBaseInfo = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
64 65
    const { currentUser, form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
66 67
      const obj = {};
      obj[key] = currentUser[key] || null;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
68
      form.setFieldsValue(obj);
69 70
    });
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
71

72
  getAvatarURL() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
73 74 75
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
76
    }
jim's avatar
jim committed
77
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
78 79
    return url;
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
80

jim's avatar
jim committed
81
  getViewDom = ref => {
82 83
    this.view = ref;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
84

85
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
86 87 88
    const {
      form: { getFieldDecorator },
    } = this.props;
89
    return (
jim's avatar
jim committed
90
      <div className={styles.baseView} ref={this.getViewDom}>
91 92
        <div className={styles.left}>
          <Form layout="vertical" onSubmit={this.handleSubmit} hideRequiredMark>
afc163's avatar
afc163 committed
93
            <FormItem label={formatMessage({ id: 'app.settings.basic.email' })}>
94
              {getFieldDecorator('email', {
95 96 97
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
98
                    message: formatMessage({ id: 'app.settings.basic.email-message' }, {}),
99 100
                  },
                ],
101 102
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
103
            <FormItem label={formatMessage({ id: 'app.settings.basic.nickname' })}>
104
              {getFieldDecorator('name', {
105 106 107
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
108
                    message: formatMessage({ id: 'app.settings.basic.nickname-message' }, {}),
109 110
                  },
                ],
111 112
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
113
            <FormItem label={formatMessage({ id: 'app.settings.basic.profile' })}>
114
              {getFieldDecorator('profile', {
115 116 117
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
118
                    message: formatMessage({ id: 'app.settings.basic.profile-message' }, {}),
119 120 121 122
                  },
                ],
              })(
                <Input.TextArea
afc163's avatar
afc163 committed
123
                  placeholder={formatMessage({ id: 'app.settings.basic.profile-placeholder' })}
124 125 126
                  rows={4}
                />
              )}
127
            </FormItem>
afc163's avatar
afc163 committed
128
            <FormItem label={formatMessage({ id: 'app.settings.basic.country' })}>
129
              {getFieldDecorator('country', {
130 131 132
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
133
                    message: formatMessage({ id: 'app.settings.basic.country-message' }, {}),
134 135
                  },
                ],
136
              })(
137
                <Select style={{ maxWidth: 220 }}>
138
                  <Option value="China">ไธญๅ›ฝ</Option>
jim's avatar
jim committed
139
                </Select>
140 141
              )}
            </FormItem>
afc163's avatar
afc163 committed
142
            <FormItem label={formatMessage({ id: 'app.settings.basic.geographic' })}>
143 144 145 146
              {getFieldDecorator('geographic', {
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
147
                    message: formatMessage({ id: 'app.settings.basic.geographic-message' }, {}),
148 149 150 151 152 153 154
                  },
                  {
                    validator: validatorGeographic,
                  },
                ],
              })(<GeographicView />)}
            </FormItem>
afc163's avatar
afc163 committed
155
            <FormItem label={formatMessage({ id: 'app.settings.basic.address' })}>
156
              {getFieldDecorator('address', {
157 158 159
                rules: [
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
160
                    message: formatMessage({ id: 'app.settings.basic.address-message' }, {}),
161 162
                  },
                ],
163 164
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
165
            <FormItem label={formatMessage({ id: 'app.settings.basic.phone' })}>
166 167
              {getFieldDecorator('phone', {
                rules: [
168 169
                  {
                    required: true,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
170
                    message: formatMessage({ id: 'app.settings.basic.phone-message' }, {}),
171
                  },
172 173 174 175
                  { validator: validatorPhone },
                ],
              })(<PhoneView />)}
            </FormItem>
176 177 178 179 180 181
            <Button type="primary">
              <FormattedMessage
                id="app.settings.basic.update"
                defaultMessage="Update Information"
              />
            </Button>
182 183 184 185 186 187 188 189 190
          </Form>
        </div>
        <div className={styles.right}>
          <AvatarView avatar={this.getAvatarURL()} />
        </div>
      </div>
    );
  }
}
lijiehua's avatar
lijiehua committed
191

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
192
export default BaseView;