BaseView.js 6 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>
16 17 18
    <div className={styles.avatar_title}>
      <FormattedMessage id="app.settings.basic.avatar" defaultMessage="Avatar" />
    </div>
陈帅's avatar
陈帅 committed
19 20 21 22 23
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
张秀玲's avatar
张秀玲 committed
24
        <Button icon="upload">
25
          <FormattedMessage id="app.settings.basic.change-avatar" defaultMessage="Change avatar" />
张秀玲's avatar
张秀玲 committed
26
        </Button>
陈帅's avatar
陈帅 committed
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,
}))
陈帅's avatar
陈帅 committed
57
@Form.create()
张秀玲's avatar
张秀玲 committed
58
class BaseView extends Component {
陈帅's avatar
陈帅 committed
59 60 61
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
62

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

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

陈帅's avatar
陈帅 committed
85
  render() {
陈帅's avatar
陈帅 committed
86 87 88
    const {
      form: { getFieldDecorator },
    } = this.props;
陈帅's avatar
陈帅 committed
89
    return (
jim's avatar
jim committed
90
      <div className={styles.baseView} ref={this.getViewDom}>
陈帅's avatar
陈帅 committed
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' })}>
陈帅's avatar
陈帅 committed
94
              {getFieldDecorator('email', {
张秀玲's avatar
张秀玲 committed
95 96 97
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
98
                    message: formatMessage({ id: 'app.settings.basic.email-message' }, {}),
张秀玲's avatar
张秀玲 committed
99 100
                  },
                ],
陈帅's avatar
陈帅 committed
101 102
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
103
            <FormItem label={formatMessage({ id: 'app.settings.basic.nickname' })}>
陈帅's avatar
陈帅 committed
104
              {getFieldDecorator('name', {
张秀玲's avatar
张秀玲 committed
105 106 107
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
108
                    message: formatMessage({ id: 'app.settings.basic.nickname-message' }, {}),
张秀玲's avatar
张秀玲 committed
109 110
                  },
                ],
陈帅's avatar
陈帅 committed
111 112
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
113
            <FormItem label={formatMessage({ id: 'app.settings.basic.profile' })}>
陈帅's avatar
陈帅 committed
114
              {getFieldDecorator('profile', {
张秀玲's avatar
张秀玲 committed
115 116 117
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
118
                    message: formatMessage({ id: 'app.settings.basic.profile-message' }, {}),
张秀玲's avatar
张秀玲 committed
119 120 121 122
                  },
                ],
              })(
                <Input.TextArea
afc163's avatar
afc163 committed
123
                  placeholder={formatMessage({ id: 'app.settings.basic.profile-placeholder' })}
张秀玲's avatar
张秀玲 committed
124 125 126
                  rows={4}
                />
              )}
陈帅's avatar
陈帅 committed
127
            </FormItem>
afc163's avatar
afc163 committed
128
            <FormItem label={formatMessage({ id: 'app.settings.basic.country' })}>
陈帅's avatar
陈帅 committed
129
              {getFieldDecorator('country', {
张秀玲's avatar
张秀玲 committed
130 131 132
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
133
                    message: formatMessage({ id: 'app.settings.basic.country-message' }, {}),
张秀玲's avatar
张秀玲 committed
134 135
                  },
                ],
陈帅's avatar
陈帅 committed
136
              })(
137
                <Select style={{ maxWidth: 220 }}>
陈帅's avatar
陈帅 committed
138
                  <Option value="China">中国</Option>
jim's avatar
jim committed
139
                </Select>
陈帅's avatar
陈帅 committed
140 141
              )}
            </FormItem>
afc163's avatar
afc163 committed
142
            <FormItem label={formatMessage({ id: 'app.settings.basic.geographic' })}>
陈帅's avatar
陈帅 committed
143 144 145 146
              {getFieldDecorator('geographic', {
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
147
                    message: formatMessage({ id: 'app.settings.basic.geographic-message' }, {}),
陈帅's avatar
陈帅 committed
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' })}>
陈帅's avatar
陈帅 committed
156
              {getFieldDecorator('address', {
张秀玲's avatar
张秀玲 committed
157 158 159
                rules: [
                  {
                    required: true,
陈帅's avatar
陈帅 committed
160
                    message: formatMessage({ id: 'app.settings.basic.address-message' }, {}),
张秀玲's avatar
张秀玲 committed
161 162
                  },
                ],
陈帅's avatar
陈帅 committed
163 164
              })(<Input />)}
            </FormItem>
afc163's avatar
afc163 committed
165
            <FormItem label={formatMessage({ id: 'app.settings.basic.phone' })}>
陈帅's avatar
陈帅 committed
166 167
              {getFieldDecorator('phone', {
                rules: [
张秀玲's avatar
张秀玲 committed
168 169
                  {
                    required: true,
陈帅's avatar
陈帅 committed
170
                    message: formatMessage({ id: 'app.settings.basic.phone-message' }, {}),
张秀玲's avatar
张秀玲 committed
171
                  },
陈帅's avatar
陈帅 committed
172 173 174 175
                  { validator: validatorPhone },
                ],
              })(<PhoneView />)}
            </FormItem>
张秀玲's avatar
张秀玲 committed
176 177 178 179 180 181
            <Button type="primary">
              <FormattedMessage
                id="app.settings.basic.update"
                defaultMessage="Update Information"
              />
            </Button>
陈帅's avatar
陈帅 committed
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;