import React, { Component, Fragment } from 'react'; import { connect } from 'dva'; import moment from 'moment'; import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu, InputNumber, DatePicker, message, Badge, Divider, } from 'antd'; import { FormComponentProps } from 'antd/lib/form'; import { SorterResult } from 'antd/lib/table'; import StandardTable, { StandardTableColumnProps } from './components/StandardTable'; import { TableListItem, TableListParams, TableListPagination } from './data'; import { Dispatch } from 'redux'; import { IStateType } from './model'; import styles from './style.less'; import UpdateForm, { IFormValsType } from './components/UpdateForm'; import CreateForm from './components/CreateForm'; const FormItem = Form.Item; const { Option } = Select; const getValue = (obj: { [x: string]: string[] }) => Object.keys(obj) .map(key => obj[key]) .join(','); type IStatusMapType = 'default' | 'processing' | 'success' | 'error'; const statusMap = ['default', 'processing', 'success', 'error']; const status = ['关闭', '运行中', '已上线', '异常']; interface TableListProps extends FormComponentProps { dispatch: Dispatch; loading: boolean; BLOCK_NAME_CAMEL_CASE: IStateType; } interface TableListState { modalVisible: boolean; updateModalVisible: boolean; expandForm: boolean; selectedRows: Array; formValues: { [key: string]: string }; stepFormValues: Partial; } /* eslint react/no-multi-comp:0 */ @connect( ({ BLOCK_NAME_CAMEL_CASE, loading, }: { BLOCK_NAME_CAMEL_CASE: IStateType; loading: { models: { [key: string]: boolean; }; }; }) => ({ BLOCK_NAME_CAMEL_CASE, loading: loading.models.rule, }) ) class TableList extends Component { state: TableListState = { modalVisible: false, updateModalVisible: false, expandForm: false, selectedRows: [], formValues: {}, stepFormValues: {}, }; columns: StandardTableColumnProps[] = [ { title: '规则名称', dataIndex: 'name', }, { title: '描述', dataIndex: 'desc', }, { title: '服务调用次数', dataIndex: 'callNo', sorter: true, align: 'right', render: (val: string) => `${val} 万`, // mark to display a total number needTotal: true, }, { title: '状态', dataIndex: 'status', filters: [ { text: status[0], value: '0', }, { text: status[1], value: '1', }, { text: status[2], value: '2', }, { text: status[3], value: '3', }, ], render(val: IStatusMapType) { return ; }, }, { title: '上次调度时间', dataIndex: 'updatedAt', sorter: true, render: (val: string) => {moment(val).format('YYYY-MM-DD HH:mm:ss')}, }, { title: '操作', render: (text, record) => ( this.handleUpdateModalVisible(true, record)}>配置 订阅警报 ), }, ]; componentDidMount() { const { dispatch } = this.props; dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/fetch', }); } handleStandardTableChange = ( pagination: Partial, filtersArg: Record, sorter: SorterResult ) => { const { dispatch } = this.props; const { formValues } = this.state; const filters = Object.keys(filtersArg).reduce((obj, key) => { const newObj = { ...obj }; newObj[key] = getValue(filtersArg[key]); return newObj; }, {}); const params: Partial = { currentPage: pagination.current, pageSize: pagination.pageSize, ...formValues, ...filters, }; if (sorter.field) { params.sorter = `${sorter.field}_${sorter.order}`; } dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/fetch', payload: params, }); }; handleFormReset = () => { const { form, dispatch } = this.props; form.resetFields(); this.setState({ formValues: {}, }); dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/fetch', payload: {}, }); }; toggleForm = () => { const { expandForm } = this.state; this.setState({ expandForm: !expandForm, }); }; handleMenuClick = (e: { key: string }) => { const { dispatch } = this.props; const { selectedRows } = this.state; if (!selectedRows) return; switch (e.key) { case 'remove': dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/remove', payload: { key: selectedRows.map(row => row.key), }, callback: () => { this.setState({ selectedRows: [], }); }, }); break; default: break; } }; handleSelectRows = (rows: TableListItem[]) => { this.setState({ selectedRows: rows, }); }; handleSearch = (e: React.FormEvent) => { e.preventDefault(); const { dispatch, form } = this.props; form.validateFields((err, fieldsValue) => { if (err) return; const values = { ...fieldsValue, updatedAt: fieldsValue.updatedAt && fieldsValue.updatedAt.valueOf(), }; this.setState({ formValues: values, }); dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/fetch', payload: values, }); }); }; handleModalVisible = (flag?: boolean) => { this.setState({ modalVisible: !!flag, }); }; handleUpdateModalVisible = (flag?: boolean, record?: IFormValsType) => { this.setState({ updateModalVisible: !!flag, stepFormValues: record || {}, }); }; handleAdd = (fields: { desc: any }) => { const { dispatch } = this.props; dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/add', payload: { desc: fields.desc, }, }); message.success('添加成功'); this.handleModalVisible(); }; handleUpdate = (fields: IFormValsType) => { const { dispatch } = this.props; dispatch({ type: 'BLOCK_NAME_CAMEL_CASE/update', payload: { name: fields.name, desc: fields.desc, key: fields.key, }, }); message.success('配置成功'); this.handleUpdateModalVisible(); }; renderSimpleForm() { const { form } = this.props; const { getFieldDecorator } = form; return (
{getFieldDecorator('name')()} {getFieldDecorator('status')( )} 展开
); } renderAdvancedForm() { const { form: { getFieldDecorator }, } = this.props; return (
{getFieldDecorator('name')()} {getFieldDecorator('status')( )} {getFieldDecorator('number')()} {getFieldDecorator('date')( )} {getFieldDecorator('status3')( )} {getFieldDecorator('status4')( )}
收起
); } renderForm() { const { expandForm } = this.state; return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm(); } render() { const { BLOCK_NAME_CAMEL_CASE: { data }, loading, form, } = this.props; const { selectedRows, modalVisible, updateModalVisible, stepFormValues } = this.state; const menu = ( 删除 批量审批 ); const parentMethods = { handleAdd: this.handleAdd, handleModalVisible: this.handleModalVisible, }; const updateMethods = { handleUpdateModalVisible: this.handleUpdateModalVisible, handleUpdate: this.handleUpdate, }; return (
{this.renderForm()}
{selectedRows.length > 0 && ( )}
{stepFormValues && Object.keys(stepFormValues).length ? ( ) : null}
); } } export default Form.create()(TableList);