BaseView.js 5.2 KB
Newer Older
陈帅's avatar
陈帅 committed
1 2
import React, { Component, Fragment } from 'react';
import { Form, Input, Upload, Select, Button } from 'antd';
3 4
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
陈帅's avatar
陈帅 committed
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 48 49 50
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();
};

@Form.create()
export default class BaseView extends Component {
51 52 53
  state = {
    md: false,
  };
陈帅's avatar
陈帅 committed
54 55
  componentDidMount() {
    this.setBaseInfo();
56 57 58 59 60
    this.resize();
    window.addEventListener('resize', this.resize);
  }
  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
陈帅's avatar
陈帅 committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }
  setBaseInfo = () => {
    const { currentUser } = this.props;
    Object.keys(this.props.form.getFieldsValue()).forEach((key) => {
      const obj = {};
      obj[key] = currentUser[key] || null;
      this.props.form.setFieldsValue(obj);
    });
  };
  getAvatarURL() {
    if (this.props.currentUser.avatar) {
      return this.props.currentUser.avatar;
    }
    const url =
      'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png';
    return url;
  }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  getViewDom = (ref) => {
    this.view = ref;
  };
  @Bind()
  @Debounce(200)
  resize() {
    if (this.view.offsetWidth > 696) {
      this.setState({
        md: false,
      });
      return;
    }
    this.setState({
      md: true,
    });
  }
陈帅's avatar
陈帅 committed
94 95 96
  render() {
    const { getFieldDecorator } = this.props.form;
    return (
97 98 99 100
      <div
        className={`${styles.baseView} ${this.state.md ? styles.md : ''}`}
        ref={this.getViewDom}
      >
陈帅's avatar
陈帅 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        <div className={styles.left}>
          <Form layout="vertical" onSubmit={this.handleSubmit} hideRequiredMark>
            <FormItem label="邮箱">
              {getFieldDecorator('email', {
                rules: [
                  { required: true, message: 'Please input your email!' },
                ],
              })(<Input />)}
            </FormItem>
            <FormItem label="昵称">
              {getFieldDecorator('name', {
                rules: [
                  { required: true, message: 'Please input your nick name!' },
                ],
              })(<Input />)}
            </FormItem>
            <FormItem label="个人简介">
              {getFieldDecorator('profile', {
                rules: [
                  { required: true, message: 'Please input personal profile!' },
                ],
              })(<Input.TextArea rows={4} />)}
            </FormItem>
            <FormItem label="国家/地区">
              {getFieldDecorator('country', {
                rules: [
                  { required: true, message: 'Please input your country!' },
                ],
              })(
130
                <Select style={{ maxWidth: 220 }}>
陈帅's avatar
陈帅 committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
                  <Option value="China">中国</Option>
                  <Option value="USA">美国</Option>
                  <Option value="France">法国</Option>
                  <Option value="Russian">俄罗斯</Option>
                  <Option value="UK">英国</Option>
                </Select>,
              )}
            </FormItem>
            <FormItem label="所在省市">
              {getFieldDecorator('geographic', {
                rules: [
                  {
                    required: true,
                    message: 'Please input your geographic info!',
                  },
                  {
                    validator: validatorGeographic,
                  },
                ],
              })(<GeographicView />)}
            </FormItem>
            <FormItem label="街道地址">
              {getFieldDecorator('address', {
                rules: [
                  { required: true, message: 'Please input your address!' },
                ],
              })(<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>
    );
  }
}