base.tsx 6.39 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Fragment } from 'react';
陈帅's avatar
陈帅 committed
2
import { formatMessage, FormattedMessage } from 'umi-plugin-react/locale';
陈帅's avatar
陈帅 committed
3
import { Form, Input, Upload, Select, Button } from 'antd';
陈帅's avatar
陈帅 committed
4
import { FormComponentProps } from 'antd/lib/form';
愚道's avatar
愚道 committed
5
import { connect } from 'dva';
陈帅's avatar
陈帅 committed
6 7 8
import styles from './BaseView.less';
import GeographicView from './GeographicView';
import PhoneView from './PhoneView';
陈帅's avatar
陈帅 committed
9
import { CurrentUser } from '../data';
陈帅's avatar
陈帅 committed
10 11 12 13
const FormItem = Form.Item;
const { Option } = Select;

// 头像组件 方便以后独立,增加裁剪之类的功能
陈帅's avatar
陈帅 committed
14
const AvatarView = ({ avatar }: { avatar: string }) => (
陈帅's avatar
陈帅 committed
15
  <Fragment>
16
    <div className={styles.avatar_title}>
17
      <FormattedMessage id="BLOCK_NAME.basic.avatar" defaultMessage="Avatar" />
18
    </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="BLOCK_NAME.basic.change-avatar" defaultMessage="Change avatar" />
张秀玲's avatar
张秀玲 committed
26
        </Button>
陈帅's avatar
陈帅 committed
27 28 29 30
      </div>
    </Upload>
  </Fragment>
);
陈帅's avatar
陈帅 committed
31 32 33 34
interface SelectItem {
  label: string;
  key: string;
}
陈帅's avatar
陈帅 committed
35

陈帅's avatar
陈帅 committed
36 37 38 39 40 41 42 43
const validatorGeographic = (
  _: any,
  value: {
    province: SelectItem;
    city: SelectItem;
  },
  callback: (message?: string) => void
) => {
陈帅's avatar
陈帅 committed
44 45 46 47 48 49 50 51 52 53
  const { province, city } = value;
  if (!province.key) {
    callback('Please input your province!');
  }
  if (!city.key) {
    callback('Please input your city!');
  }
  callback();
};

陈帅's avatar
陈帅 committed
54
const validatorPhone = (rule: any, value: string, callback: (message?: string) => void) => {
陈帅's avatar
陈帅 committed
55 56 57 58 59 60 61 62 63 64
  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
65 66 67 68 69
interface BaseViewProps extends FormComponentProps {
  currentUser: CurrentUser;
}

@connect(({ BLOCK_NAME_CAMEL_CASE }: { BLOCK_NAME_CAMEL_CASE: { currentUser: CurrentUser } }) => ({
70
  currentUser: BLOCK_NAME_CAMEL_CASE.currentUser,
愚道's avatar
愚道 committed
71
}))
陈帅's avatar
陈帅 committed
72
class BaseView extends Component<BaseViewProps> {
陈帅's avatar
陈帅 committed
73 74 75
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
76

陈帅's avatar
陈帅 committed
77
  setBaseInfo = () => {
陈帅's avatar
陈帅 committed
78 79
    const { currentUser, form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
陈帅's avatar
陈帅 committed
80 81
      const obj = {};
      obj[key] = currentUser[key] || null;
陈帅's avatar
陈帅 committed
82
      form.setFieldsValue(obj);
陈帅's avatar
陈帅 committed
83 84
    });
  };
陈帅's avatar
陈帅 committed
85

陈帅's avatar
陈帅 committed
86
  getAvatarURL() {
陈帅's avatar
陈帅 committed
87 88 89
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
陈帅's avatar
陈帅 committed
90
    }
jim's avatar
jim committed
91
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
陈帅's avatar
陈帅 committed
92 93
    return url;
  }
陈帅's avatar
陈帅 committed
94
  view: HTMLDivElement | undefined;
陈帅's avatar
陈帅 committed
95

陈帅's avatar
陈帅 committed
96
  getViewDom = (ref: HTMLDivElement) => {
97 98
    this.view = ref;
  };
陈帅's avatar
陈帅 committed
99

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

陈帅's avatar
陈帅 committed
204
export default Form.create()(BaseView);