base.tsx 6.48 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
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 74 75
  componentDidMount() {
    this.setBaseInfo();
  }
陈帅's avatar
陈帅 committed
76

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

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

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

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

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