base.tsx 6.79 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';
3
import { Form, Input, Upload, Select, Button, message } 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
const validatorGeographic = (
  _: any,
  value: {
    province: SelectItem;
    city: SelectItem;
  },
陈帅's avatar
陈帅 committed
42
  callback: (message?: string) => void,
陈帅's avatar
陈帅 committed
43
) => {
陈帅'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
interface BaseViewProps extends FormComponentProps {
66
  currentUser?: CurrentUser;
陈帅's avatar
陈帅 committed
67 68 69
}

@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
  view: HTMLDivElement | undefined;
陈帅's avatar
陈帅 committed
74 75 76
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
77

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

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

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

105 106 107 108 109 110 111 112 113 114
  handlerSubmit = (event: Event) => {
    event.preventDefault();
    const { form } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        message.success(formatMessage({ id: 'BLOCK_NAME.basic.updatesucc' }));
      }
    });
  };

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

陈帅's avatar
陈帅 committed
219
export default Form.create<BaseViewProps>()(BaseView);