index.js 2.1 KB
Newer Older
vipcxj's avatar
vipcxj committed
1 2 3 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
import React from 'react';
import PropTypes from 'prop-types';
import { Form, Select, Button } from 'antd';
import { connect } from 'dva';
import styles from './index.less';

const FormItem = Form.Item;

class Domain extends React.Component {

  constructor(props, context) {
    super(props, context);
    this.handleSubmit = this::this.handleSubmit;
  }

  componentDidMount() {
    this.props.dispatch({ type: 'domain/fetch' });
  }

  handleSubmit(e) {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (!err) {
        this.props.dispatch({
          type: 'domain/switch',
          payload: values.domain,
        });
      }
    });
  }

  render() {
    const { form, domain, loading } = this.props;
34
    const currentDomain = domain.init;
vipcxj's avatar
vipcxj committed
35
    const selectOptions = {};
36 37 38 39 40
    const decoratorOptions = {
      rules: [
        { required: true },
      ],
    };
41
    if (!currentDomain || !currentDomain.path) {
vipcxj's avatar
vipcxj committed
42 43
      selectOptions.placeholder = '请选择项目...';
    } else {
44
      decoratorOptions.initialValue = currentDomain.path;
vipcxj's avatar
vipcxj committed
45 46 47 48 49 50 51 52 53
    }
    return (
      <div className={styles.canvas}>
        <div className={styles.container}>
          <Form onSubmit={this.handleSubmit}>
            <FormItem>
              {
                form.getFieldDecorator('domain', decoratorOptions)(
                  <Select {...selectOptions}>
54
                    {domain.list.map(({ path, name }) => {
55
                      return <Select.Option value={path} key={path}>{name}</Select.Option>;
vipcxj's avatar
vipcxj committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                    })}
                  </Select>,
                )
              }
            </FormItem>
            <FormItem>
              <Button type="primary" htmlType="submit" className={styles.submit} loading={loading}>
                确定
              </Button>
            </FormItem>
          </Form>
        </div>
      </div>
    );
  }
}

Domain.propTypes = {
  domain: PropTypes.object,
  loading: PropTypes.bool,
  form: PropTypes.object,
};

const mapStateToProps = ({ domain, loading }) => {
  return {
    domain,
    loading: loading.effects['domain/switch'],
  };
};

export default connect(mapStateToProps)(Form.create()(Domain));