BaseView.js 4.41 KB
Newer Older
1 2
import React, { Component, Fragment } from 'react';
import { Form, Input, Upload, Select, Button } from 'antd';
ๆ„š้“'s avatar
ๆ„š้“ committed
3
import { connect } from 'dva';
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
import styles from './BaseView.less';
import GeographicView from './GeographicView';
import PhoneView from './PhoneView';

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

// ๅคดๅƒ็ป„ไปถ ๆ–นไพฟไปฅๅŽ็‹ฌ็ซ‹๏ผŒๅขžๅŠ ่ฃๅ‰ชไน‹็ฑป็š„ๅŠŸ่ƒฝ
const AvatarView = ({ avatar }) => (
  <Fragment>
    <div className={styles.avatar_title}>ๅคดๅƒ</div>
    <div className={styles.avatar}>
      <img src={avatar} alt="avatar" />
    </div>
    <Upload fileList={[]}>
      <div className={styles.button_view}>
        <Button icon="upload">ๆ›ดๆขๅคดๅƒ</Button>
      </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
48 49 50
@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
51 52 53 54 55
@Form.create()
export default class BaseView extends Component {
  componentDidMount() {
    this.setBaseInfo();
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
56

57
  setBaseInfo = () => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
58 59
    const { currentUser, form } = this.props;
    Object.keys(form.getFieldsValue()).forEach(key => {
60 61
      const obj = {};
      obj[key] = currentUser[key] || null;
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
62
      form.setFieldsValue(obj);
63 64
    });
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
65

66
  getAvatarURL() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
67 68 69
    const { currentUser } = this.props;
    if (currentUser.avatar) {
      return currentUser.avatar;
70
    }
jim's avatar
jim committed
71
    const url = 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
72 73
    return url;
  }
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
74

jim's avatar
jim committed
75
  getViewDom = ref => {
76 77
    this.view = ref;
  };
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
78

79
  render() {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
80 81 82
    const {
      form: { getFieldDecorator },
    } = this.props;
83
    return (
jim's avatar
jim committed
84
      <div className={styles.baseView} ref={this.getViewDom}>
85 86 87 88
        <div className={styles.left}>
          <Form layout="vertical" onSubmit={this.handleSubmit} hideRequiredMark>
            <FormItem label="้‚ฎ็ฎฑ">
              {getFieldDecorator('email', {
jim's avatar
jim committed
89
                rules: [{ required: true, message: 'Please input your email!' }],
90 91 92 93
              })(<Input />)}
            </FormItem>
            <FormItem label="ๆ˜ต็งฐ">
              {getFieldDecorator('name', {
jim's avatar
jim committed
94
                rules: [{ required: true, message: 'Please input your nick name!' }],
95 96 97 98
              })(<Input />)}
            </FormItem>
            <FormItem label="ไธชไบบ็ฎ€ไป‹">
              {getFieldDecorator('profile', {
jim's avatar
jim committed
99
                rules: [{ required: true, message: 'Please input personal profile!' }],
100
              })(<Input.TextArea placeholder="็ฎ€ๅ•็š„ไป‹็ปไธ‹่‡ชๅทฑ" rows={4} />)}
101 102 103
            </FormItem>
            <FormItem label="ๅ›ฝๅฎถ/ๅœฐๅŒบ">
              {getFieldDecorator('country', {
jim's avatar
jim committed
104
                rules: [{ required: true, message: 'Please input your country!' }],
105
              })(
106
                <Select style={{ maxWidth: 220 }}>
107
                  <Option value="China">ไธญๅ›ฝ</Option>
jim's avatar
jim committed
108
                </Select>
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
              )}
            </FormItem>
            <FormItem label="ๆ‰€ๅœจ็œๅธ‚">
              {getFieldDecorator('geographic', {
                rules: [
                  {
                    required: true,
                    message: 'Please input your geographic info!',
                  },
                  {
                    validator: validatorGeographic,
                  },
                ],
              })(<GeographicView />)}
            </FormItem>
            <FormItem label="่ก—้“ๅœฐๅ€">
              {getFieldDecorator('address', {
jim's avatar
jim committed
126
                rules: [{ required: true, message: 'Please input your address!' }],
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
              })(<Input />)}
            </FormItem>
            <FormItem label="่”็ณป็”ต่ฏ">
              {getFieldDecorator('phone', {
                rules: [
                  { required: true, message: 'Please input your phone!' },
                  { validator: validatorPhone },
                ],
              })(<PhoneView />)}
            </FormItem>
            <Button type="primary">ๆ›ดๆ–ฐไฟกๆฏ</Button>
          </Form>
        </div>
        <div className={styles.right}>
          <AvatarView avatar={this.getAvatarURL()} />
        </div>
      </div>
    );
  }
}