base.js 5.95 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 8 9 10 11 12 13 14
import styles from './BaseView.less';
import GeographicView from './GeographicView';
import PhoneView from './PhoneView';

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

// 头像组件 方便以后独立,增加裁剪之类的功能
const AvatarView = ({ avatar }) => (
  <Fragment>
15
    <div className={styles.avatar_title}>
16
      <FormattedMessage id="BLOCK_NAME.basic.avatar" defaultMessage="Avatar" />
17
    </div>
陈帅's avatar
陈帅 committed
18 19 20 21 22
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
张秀玲's avatar
张秀玲 committed
23
        <Button icon="upload">
24
          <FormattedMessage id="BLOCK_NAME.basic.change-avatar" defaultMessage="Change avatar" />
张秀玲's avatar
张秀玲 committed
25
        </Button>
陈帅's avatar
陈帅 committed
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 52
      </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();
};

53 54
@connect(({ BLOCK_NAME_CAMEL_CASE }) => ({
  currentUser: BLOCK_NAME_CAMEL_CASE.currentUser,
愚道's avatar
愚道 committed
55
}))
陈帅's avatar
陈帅 committed
56
@Form.create()
张秀玲's avatar
张秀玲 committed
57
class BaseView extends Component {
陈帅's avatar
陈帅 committed
58 59 60
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
61

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

陈帅's avatar
陈帅 committed
71
  getAvatarURL() {
陈帅's avatar
陈帅 committed
72 73 74
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
陈帅's avatar
陈帅 committed
75
    }
jim's avatar
jim committed
76
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
陈帅's avatar
陈帅 committed
77 78
    return url;
  }
陈帅's avatar
陈帅 committed
79

jim's avatar
jim committed
80
  getViewDom = ref => {
81 82
    this.view = ref;
  };
陈帅's avatar
陈帅 committed
83

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

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