BaseView.js 6.17 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Fragment } from 'react';
张秀玲's avatar
张秀玲 committed
2
import { injectIntl, FormattedMessage } from 'react-intl';
陈帅's avatar
陈帅 committed
3
import { Form, Input, Upload, Select, Button } from 'antd';
愚道's avatar
愚道 committed
4
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
5 6 7
import styles from './BaseView.less';
import GeographicView from './GeographicView';
import PhoneView from './PhoneView';
张秀玲's avatar
张秀玲 committed
8
// import { getTimeDistance } from '../../../utils/utils';
陈帅's avatar
陈帅 committed
9 10 11 12 13 14 15

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

// 头像组件 方便以后独立,增加裁剪之类的功能
const AvatarView = ({ avatar }) => (
  <Fragment>
张秀玲's avatar
张秀玲 committed
16
    <div className={styles.avatar_title}>Avatar</div>
陈帅's avatar
陈帅 committed
17 18 19 20 21
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
张秀玲's avatar
张秀玲 committed
22 23 24
        <Button icon="upload">
          <FormattedMessage id="app.settings.basic.avatar" defaultMessage="Change avatar" />
        </Button>
陈帅's avatar
陈帅 committed
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,
}))
陈帅's avatar
陈帅 committed
55
@Form.create()
张秀玲's avatar
张秀玲 committed
56
class BaseView extends Component {
陈帅's avatar
陈帅 committed
57 58 59
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
60

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

陈帅's avatar
陈帅 committed
70
  getAvatarURL() {
陈帅's avatar
陈帅 committed
71 72 73
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
陈帅's avatar
陈帅 committed
74
    }
jim's avatar
jim committed
75
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
陈帅's avatar
陈帅 committed
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

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