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

陈帅's avatar
陈帅 committed
190
export default BaseView;