DeptmentList.js 7.3 KB
Newer Older
zhenxianyimeng's avatar
zhenxianyimeng committed
1
import React, { PureComponent, Fragment } from 'react';
2
import { Button, Card, Table, Form, Divider, Modal, Input, TreeSelect, message } from 'antd';
zhenxianyimeng's avatar
zhenxianyimeng committed
3 4 5 6 7
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import { connect } from 'dva';
import styles from './DeptmentList.less';
import PaginationHelper from '../../../helpers/PaginationHelper';
import moment from 'moment';
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 51 52 53 54 55 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 88 89 90 91 92
const FormItem = Form.Item;
// 添加 form 表单
const CreateForm = Form.create()(props => {
  const {
    modalVisible,
    form,
    handleAdd,
    handleModalVisible,
    modalType,
    initValues,
    selectTree,
  } = props;

  const okHandle = () => {
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      let pid = fieldsValue.pid;
      if (fieldsValue.pid) {
        pid = pid.split('-')[1];
        fieldsValue.pid = pid;
      }
      form.resetFields();
      handleAdd({
        fields: fieldsValue,
        modalType,
        initValues,
      });
    });
  };

  const selectStyle = {
    width: 200,
  };

  function onTypeChange(event) {
    initValues.type = parseInt(event.target.value);
  }

  // 给 type 赋予初始值
  initValues.type = initValues.type || 1;

  const title = modalType === 'add' ? '添加部门' : '编辑部门';
  const okText = modalType === 'add' ? '添加' : '编辑';
  return (
    <Modal
      destroyOnClose
      title={title}
      visible={modalVisible}
      onOk={okHandle}
      okText={okText}
      onCancel={() => handleModalVisible()}
    >
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="部门名称">
        {form.getFieldDecorator('name', {
          initialValue: initValues.name,
          rules: [{ required: true, message: '请输入部门名称!', min: 2 }],
        })(<Input placeholder="部门名称" />)}
      </FormItem>
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="部门排序">
        {form.getFieldDecorator('sort', {
          initialValue: initValues.sort,
        })(<Input placeholder="部门排序" />)}
      </FormItem>
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="父级部门">
        {form.getFieldDecorator('pid', {
          rules: [{ required: true, message: '请选择父级编号!' }],
          initialValue:
            initValues.pid === 0
              ? `根节点-${initValues.pid}`
              : initValues.pid
              ? `${initValues.name}-${initValues.pid}`
              : undefined,
        })(
          <TreeSelect
            showSearch
            style={{ width: 300 }}
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            treeData={selectTree}
            placeholder="选择父级部门"
          />
        )}
      </FormItem>
    </Modal>
  );
});
zhenxianyimeng's avatar
zhenxianyimeng committed
93 94 95 96 97 98 99 100

@connect(({ deptmentList, loading }) => ({
  deptmentList,
  deptmentData: deptmentList.deptmentData,
  loading: loading.models.deptmentList,
}))
@Form.create()
export default class DepetmentList extends PureComponent {
101 102 103 104 105 106
  state = {
    modalVisible: false,
    modalType: 'add', //add or update
    initValues: {},
  };

zhenxianyimeng's avatar
zhenxianyimeng committed
107 108 109
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
110
      type: 'deptmentList/getDeptmentAll',
zhenxianyimeng's avatar
zhenxianyimeng committed
111 112 113 114 115 116
      payload: {
        ...PaginationHelper.defaultPayload,
      },
    });
  }

117 118 119 120 121 122 123 124 125 126
  initFetch = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'deptmentList/getDeptmentAll',
      payload: {
        ...PaginationHelper.defaultPayload,
      },
    });
  };

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  handleModalVisible = (flag, modalType, initValues) => {
    this.setState({
      modalVisible: !!flag,
      initValues: initValues || {},
      modalType: modalType || 'add',
    });
    if (flag) {
      //query treeSelect
      const { dispatch } = this.props;
      dispatch({
        type: 'deptmentList/getDeptmentAll',
        payload: {},
      });
    }
  };

zhenxianyimeng's avatar
zhenxianyimeng committed
143 144
  handleDelete(row) {
    const { dispatch } = this.props;
145
    const _this = this;
zhenxianyimeng's avatar
zhenxianyimeng committed
146 147 148 149 150 151 152 153 154 155 156 157
    Modal.confirm({
      title: `确认删除?`,
      content: `${row.name}`,
      onOk() {
        dispatch({
          type: 'deptmentList/delete',
          payload: {
            body: {
              id: row.id,
            },
            onSuccess: () => {
              message.success('删除成功');
158
              _this.initFetch();
zhenxianyimeng's avatar
zhenxianyimeng committed
159 160 161 162 163 164 165 166 167 168 169
            },
            onFail: response => {
              message.warn('删除失败' + response.message);
            },
          },
        });
      },
      onCancel() {},
    });
  }

170 171 172 173 174 175 176 177 178 179 180 181
  handleAdd = ({ fields, modalType, initValues }) => {
    const { dispatch } = this.props;
    if (modalType === 'add') {
      dispatch({
        type: 'deptmentList/add',
        payload: {
          body: {
            ...fields,
          },
          onSuccess: () => {
            message.success('添加成功');
            this.handleModalVisible();
182
            this.initFetch();
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
          },
          onFail: response => {
            message.warn('添加失败' + response.message);
          },
        },
      });
    } else {
      dispatch({
        type: 'deptmentList/update',
        payload: {
          body: {
            ...initValues,
            ...fields,
          },
          onSuccess: () => {
            message.success('更新成功成功');
            this.handleModalVisible();
200
            this.initFetch();
201 202 203 204 205 206 207 208 209
          },
          onFail: response => {
            message.warn('更新失败' + response.message);
          },
        },
      });
    }
  };

zhenxianyimeng's avatar
zhenxianyimeng committed
210
  render() {
211
    const { deptmentData, deptmentList, loading } = this.props;
212 213 214 215 216 217 218 219
    const { selectTree } = deptmentList;
    const { modalVisible, modalType, initValues } = this.state;
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
      modalType,
      initValues,
    };
zhenxianyimeng's avatar
zhenxianyimeng committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    const columns = [
      {
        title: '部门名称',
        dataIndex: 'name',
      },
      {
        title: '排序',
        dataIndex: 'sort',
      },
      {
        title: '创建时间',
        dataIndex: 'createTime',
        sorter: true,
        render: val => <span>{moment(val).format('YYYY-MM-DD')}</span>,
      },
      {
        title: '操作',
        render: (text, record) => (
          <Fragment>
            <a onClick={() => this.handleModalVisible(true, 'update', record)}>编辑</a>
            <Divider type="vertical" />
            <a className={styles.tableDelete} onClick={() => this.handleDelete(record)}>
              删除
            </a>
          </Fragment>
        ),
      },
    ];

    // const {
    // 	deptmentList: {deptmentData},
    // 	loading,
    // } = this.props;

    return (
      <PageHeaderWrapper>
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListOperator}>
              <Button
                icon="plus"
                type="primary"
                onClick={() => this.handleModalVisible(true, 'add', {})}
              >
                新建部门
              </Button>
            </div>
          </div>
          <Table
            defaultExpandAllRows={true}
            columns={columns}
271
            dataSource={deptmentList.list ? deptmentList.list : []}
zhenxianyimeng's avatar
zhenxianyimeng committed
272
            rowKey="id"
273
            loading={loading}
zhenxianyimeng's avatar
zhenxianyimeng committed
274 275
          />
        </Card>
276
        <CreateForm {...parentMethods} selectTree={selectTree} modalVisible={modalVisible} />
zhenxianyimeng's avatar
zhenxianyimeng committed
277 278 279 280
      </PageHeaderWrapper>
    );
  }
}