BaseView.js 5.95 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
    <div className={styles.avatar_title}>Avatar</div>
17 18 19 20 21
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
22 23 24
        <Button icon="upload">
          <FormattedMessage id="app.settings.basic.avatar" defaultMessage="Change avatar" />
        </Button>
25 26 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
      </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
52 53 54
@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
55
@Form.create()
56
class BaseView extends Component {
57 58 59
  componentDidMount() {
    this.setBaseInfo();
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
60

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

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

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

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