BaseView.js 6.28 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
class BaseView extends Component {
  constructor(props) {
    super(props);
    const { intl } = props;
    this.formLabelData = {
      email: intl.formatMessage({ id: 'app.settings.basic.email' }, {}),
      emailMessage: intl.formatMessage({ id: 'app.settings.basic.email-message' }, {}),
      nickname: intl.formatMessage({ id: 'app.settings.basic.nickname' }, {}),
      nicknameMessage: intl.formatMessage({ id: 'app.settings.basic.nickname-message' }, {}),
      profile: intl.formatMessage({ id: 'app.settings.basic.profile' }, {}),
      profileMessage: intl.formatMessage({ id: 'app.settings.basic.profile-message' }, {}),
      profilePlaceholder: intl.formatMessage({ id: 'app.settings.basic.profile-placeholder' }, {}),
      country: intl.formatMessage({ id: 'app.settings.basic.country' }, {}),
      countryMessage: intl.formatMessage({ id: 'app.settings.basic.country-message' }, {}),
      geographic: intl.formatMessage({ id: 'app.settings.basic.geographic' }, {}),
      geographicMessage: intl.formatMessage({ id: 'app.settings.basic.geographic-message' }, {}),
      address: intl.formatMessage({ id: 'app.settings.basic.address' }, {}),
      addressMessage: intl.formatMessage({ id: 'app.settings.basic.address-message' }, {}),
      phone: intl.formatMessage({ id: 'app.settings.basic.phone' }, {}),
      phoneMessage: intl.formatMessage({ id: 'app.settings.basic.phone-message' }, {}),
    };
  }

陈帅's avatar
陈帅 committed
79 80 81
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
82

陈帅's avatar
陈帅 committed
83
  setBaseInfo = () => {
陈帅's avatar
陈帅 committed
84 85
    const { currentUser, form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
陈帅's avatar
陈帅 committed
86 87
      const obj = {};
      obj[key] = currentUser[key] || null;
陈帅's avatar
陈帅 committed
88
      form.setFieldsValue(obj);
陈帅's avatar
陈帅 committed
89 90
    });
  };
陈帅's avatar
陈帅 committed
91

陈帅's avatar
陈帅 committed
92
  getAvatarURL() {
陈帅's avatar
陈帅 committed
93 94 95
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
陈帅's avatar
陈帅 committed
96
    }
jim's avatar
jim committed
97
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
陈帅's avatar
陈帅 committed
98 99
    return url;
  }
陈帅's avatar
陈帅 committed
100

jim's avatar
jim committed
101
  getViewDom = ref => {
102 103
    this.view = ref;
  };
陈帅's avatar
陈帅 committed
104

陈帅's avatar
陈帅 committed
105
  render() {
陈帅's avatar
陈帅 committed
106 107 108
    const {
      form: { getFieldDecorator },
    } = this.props;
陈帅's avatar
陈帅 committed
109
    return (
jim's avatar
jim committed
110
      <div className={styles.baseView} ref={this.getViewDom}>
陈帅's avatar
陈帅 committed
111 112
        <div className={styles.left}>
          <Form layout="vertical" onSubmit={this.handleSubmit} hideRequiredMark>
张秀玲's avatar
张秀玲 committed
113
            <FormItem label={this.formLabelData.email}>
陈帅's avatar
陈帅 committed
114
              {getFieldDecorator('email', {
张秀玲's avatar
张秀玲 committed
115
                rules: [{ required: true, message: this.formLabelData.emailMessage }],
陈帅's avatar
陈帅 committed
116 117
              })(<Input />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
118
            <FormItem label={this.formLabelData.nickname}>
陈帅's avatar
陈帅 committed
119
              {getFieldDecorator('name', {
张秀玲's avatar
张秀玲 committed
120
                rules: [{ required: true, message: this.formLabelData.nicknameMessage }],
陈帅's avatar
陈帅 committed
121 122
              })(<Input />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
123
            <FormItem label={this.formLabelData.profile}>
陈帅's avatar
陈帅 committed
124
              {getFieldDecorator('profile', {
张秀玲's avatar
张秀玲 committed
125 126
                rules: [{ required: true, message: this.formLabelData.profileMessage }],
              })(<Input.TextArea placeholder={this.formLabelData.profilePlaceholder} rows={4} />)}
陈帅's avatar
陈帅 committed
127
            </FormItem>
张秀玲's avatar
张秀玲 committed
128
            <FormItem label={this.formLabelData.country}>
陈帅's avatar
陈帅 committed
129
              {getFieldDecorator('country', {
张秀玲's avatar
张秀玲 committed
130
                rules: [{ required: true, message: this.formLabelData.countryMessage }],
陈帅's avatar
陈帅 committed
131
              })(
132
                <Select style={{ maxWidth: 220 }}>
陈帅's avatar
陈帅 committed
133
                  <Option value="China">中国</Option>
jim's avatar
jim committed
134
                </Select>
陈帅's avatar
陈帅 committed
135 136
              )}
            </FormItem>
张秀玲's avatar
张秀玲 committed
137
            <FormItem label={this.formLabelData.geographic}>
陈帅's avatar
陈帅 committed
138 139 140 141
              {getFieldDecorator('geographic', {
                rules: [
                  {
                    required: true,
张秀玲's avatar
张秀玲 committed
142
                    message: this.formLabelData.geographicMessage,
陈帅's avatar
陈帅 committed
143 144 145 146 147 148 149
                  },
                  {
                    validator: validatorGeographic,
                  },
                ],
              })(<GeographicView />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
150
            <FormItem label={this.formLabelData.address}>
陈帅's avatar
陈帅 committed
151
              {getFieldDecorator('address', {
张秀玲's avatar
张秀玲 committed
152
                rules: [{ required: true, message: this.formLabelData.addressMessage }],
陈帅's avatar
陈帅 committed
153 154
              })(<Input />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
155
            <FormItem label={this.formLabelData.phone}>
陈帅's avatar
陈帅 committed
156 157
              {getFieldDecorator('phone', {
                rules: [
张秀玲's avatar
张秀玲 committed
158
                  { required: true, message: this.formLabelData.phoneMessage },
陈帅's avatar
陈帅 committed
159 160 161 162
                  { validator: validatorPhone },
                ],
              })(<PhoneView />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
163 164 165 166 167 168
            <Button type="primary">
              <FormattedMessage
                id="app.settings.basic.update"
                defaultMessage="Update Information"
              />
            </Button>
陈帅's avatar
陈帅 committed
169 170 171 172 173 174 175 176 177
          </Form>
        </div>
        <div className={styles.right}>
          <AvatarView avatar={this.getAvatarURL()} />
        </div>
      </div>
    );
  }
}
张秀玲's avatar
张秀玲 committed
178
export default injectIntl(BaseView);