index.tsx 12.7 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import React, { Component, Fragment } from 'react';
2
import { connect } from 'dva';
ddcat1115's avatar
ddcat1115 committed
3
import moment from 'moment';
jim's avatar
jim committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  message,
  Badge,
  Divider,
} from 'antd';
陈帅's avatar
陈帅 committed
21 22 23 24 25 26
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';
27
import styles from './style.less';
陈帅's avatar
陈帅 committed
28 29
import UpdateForm, { IFormValsType } from './components/UpdateForm';
import CreateForm from './components/CreateForm';
30 31

const FormItem = Form.Item;
afc163's avatar
afc163 committed
32
const { Option } = Select;
陈帅's avatar
陈帅 committed
33
const getValue = (obj: { [x: string]: string[] }) =>
jim's avatar
jim committed
34 35 36
  Object.keys(obj)
    .map(key => obj[key])
    .join(',');
陈帅's avatar
陈帅 committed
37 38

type IStatusMapType = 'default' | 'processing' | 'success' | 'error';
ddcat1115's avatar
ddcat1115 committed
39 40
const statusMap = ['default', 'processing', 'success', 'error'];
const status = ['关闭', '运行中', '已上线', '异常'];
41

陈帅's avatar
陈帅 committed
42 43 44 45 46
interface TableListProps extends FormComponentProps {
  dispatch: Dispatch;
  loading: boolean;
  BLOCK_NAME_CAMEL_CASE: IStateType;
}
ddcat1115's avatar
ddcat1115 committed
47

陈帅's avatar
陈帅 committed
48 49 50 51 52 53 54
interface TableListState {
  modalVisible: boolean;
  updateModalVisible: boolean;
  expandForm: boolean;
  selectedRows: Array<TableListItem>;
  formValues: { [key: string]: string };
  stepFormValues: Partial<TableListItem>;
ddcat1115's avatar
ddcat1115 committed
55 56 57
}

/* eslint react/no-multi-comp:0 */
陈帅's avatar
陈帅 committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
@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<TableListProps, TableListState> {
  state: TableListState = {
76 77 78 79 80 81 82 83
    modalVisible: false,
    updateModalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
    stepFormValues: {},
  };

陈帅's avatar
陈帅 committed
84
  columns: StandardTableColumnProps[] = [
愚道's avatar
愚道 committed
85 86 87 88 89 90 91 92 93 94 95 96 97
    {
      title: '规则名称',
      dataIndex: 'name',
    },
    {
      title: '描述',
      dataIndex: 'desc',
    },
    {
      title: '服务调用次数',
      dataIndex: 'callNo',
      sorter: true,
      align: 'right',
陈帅's avatar
陈帅 committed
98
      render: (val: string) => `${val} 万`,
愚道's avatar
愚道 committed
99 100 101 102 103 104 105 106 107
      // mark to display a total number
      needTotal: true,
    },
    {
      title: '状态',
      dataIndex: 'status',
      filters: [
        {
          text: status[0],
陈帅's avatar
陈帅 committed
108
          value: '0',
愚道's avatar
愚道 committed
109 110 111
        },
        {
          text: status[1],
陈帅's avatar
陈帅 committed
112
          value: '1',
愚道's avatar
愚道 committed
113 114 115
        },
        {
          text: status[2],
陈帅's avatar
陈帅 committed
116
          value: '2',
愚道's avatar
愚道 committed
117 118 119
        },
        {
          text: status[3],
陈帅's avatar
陈帅 committed
120
          value: '3',
愚道's avatar
愚道 committed
121 122
        },
      ],
陈帅's avatar
陈帅 committed
123
      render(val: IStatusMapType) {
愚道's avatar
愚道 committed
124 125 126 127 128 129 130
        return <Badge status={statusMap[val]} text={status[val]} />;
      },
    },
    {
      title: '上次调度时间',
      dataIndex: 'updatedAt',
      sorter: true,
陈帅's avatar
陈帅 committed
131
      render: (val: string) => <span>{moment(val).format('YYYY-MM-DD HH:mm:ss')}</span>,
愚道's avatar
愚道 committed
132 133 134 135 136 137 138 139 140 141 142 143 144
    },
    {
      title: '操作',
      render: (text, record) => (
        <Fragment>
          <a onClick={() => this.handleUpdateModalVisible(true, record)}>配置</a>
          <Divider type="vertical" />
          <a href="">订阅警报</a>
        </Fragment>
      ),
    },
  ];

145 146 147
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
148
      type: 'BLOCK_NAME_CAMEL_CASE/fetch',
149 150 151
    });
  }

陈帅's avatar
陈帅 committed
152 153 154 155 156
  handleStandardTableChange = (
    pagination: Partial<TableListPagination>,
    filtersArg: Record<keyof TableListItem, string[]>,
    sorter: SorterResult<TableListItem>
  ) => {
157 158 159 160 161 162 163 164 165
    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;
    }, {});

陈帅's avatar
陈帅 committed
166
    const params: Partial<TableListParams> = {
167 168 169 170 171 172 173 174 175 176
      currentPage: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
      ...filters,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
177
      type: 'BLOCK_NAME_CAMEL_CASE/fetch',
178 179
      payload: params,
    });
jim's avatar
jim committed
180
  };
181 182 183 184

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
185 186 187
    this.setState({
      formValues: {},
    });
188
    dispatch({
189
      type: 'BLOCK_NAME_CAMEL_CASE/fetch',
190 191
      payload: {},
    });
jim's avatar
jim committed
192
  };
193 194

  toggleForm = () => {
陈帅's avatar
陈帅 committed
195
    const { expandForm } = this.state;
196
    this.setState({
陈帅's avatar
陈帅 committed
197
      expandForm: !expandForm,
198
    });
jim's avatar
jim committed
199
  };
200

陈帅's avatar
陈帅 committed
201
  handleMenuClick = (e: { key: string }) => {
202 203 204
    const { dispatch } = this.props;
    const { selectedRows } = this.state;

205
    if (!selectedRows) return;
206 207 208
    switch (e.key) {
      case 'remove':
        dispatch({
209
          type: 'BLOCK_NAME_CAMEL_CASE/remove',
210
          payload: {
ddcat1115's avatar
ddcat1115 committed
211
            key: selectedRows.map(row => row.key),
212 213 214 215 216 217 218 219 220 221 222
          },
          callback: () => {
            this.setState({
              selectedRows: [],
            });
          },
        });
        break;
      default:
        break;
    }
jim's avatar
jim committed
223
  };
224

陈帅's avatar
陈帅 committed
225
  handleSelectRows = (rows: TableListItem[]) => {
226 227 228
    this.setState({
      selectedRows: rows,
    });
jim's avatar
jim committed
229
  };
230

陈帅's avatar
陈帅 committed
231
  handleSearch = (e: React.FormEvent) => {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    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({
249
        type: 'BLOCK_NAME_CAMEL_CASE/fetch',
250 251 252
        payload: values,
      });
    });
jim's avatar
jim committed
253
  };
254

陈帅's avatar
陈帅 committed
255
  handleModalVisible = (flag?: boolean) => {
256 257 258
    this.setState({
      modalVisible: !!flag,
    });
jim's avatar
jim committed
259
  };
260

陈帅's avatar
陈帅 committed
261
  handleUpdateModalVisible = (flag?: boolean, record?: IFormValsType) => {
ddcat1115's avatar
ddcat1115 committed
262 263 264 265
    this.setState({
      updateModalVisible: !!flag,
      stepFormValues: record || {},
    });
jim's avatar
jim committed
266
  };
ddcat1115's avatar
ddcat1115 committed
267

陈帅's avatar
陈帅 committed
268
  handleAdd = (fields: { desc: any }) => {
陈帅's avatar
陈帅 committed
269 270
    const { dispatch } = this.props;
    dispatch({
271
      type: 'BLOCK_NAME_CAMEL_CASE/add',
272
      payload: {
ddcat1115's avatar
ddcat1115 committed
273
        desc: fields.desc,
274 275 276 277
      },
    });

    message.success('添加成功');
ddcat1115's avatar
ddcat1115 committed
278
    this.handleModalVisible();
jim's avatar
jim committed
279
  };
ddcat1115's avatar
ddcat1115 committed
280

陈帅's avatar
陈帅 committed
281
  handleUpdate = (fields: IFormValsType) => {
陈帅's avatar
陈帅 committed
282 283
    const { dispatch } = this.props;
    dispatch({
284
      type: 'BLOCK_NAME_CAMEL_CASE/update',
ddcat1115's avatar
ddcat1115 committed
285
      payload: {
286 287 288
        name: fields.name,
        desc: fields.desc,
        key: fields.key,
ddcat1115's avatar
ddcat1115 committed
289
      },
290
    });
ddcat1115's avatar
ddcat1115 committed
291 292 293

    message.success('配置成功');
    this.handleUpdateModalVisible();
jim's avatar
jim committed
294
  };
295

afc163's avatar
afc163 committed
296
  renderSimpleForm() {
陈帅's avatar
陈帅 committed
297 298
    const { form } = this.props;
    const { getFieldDecorator } = form;
afc163's avatar
afc163 committed
299 300
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
301 302
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
ddcat1115's avatar
ddcat1115 committed
303
            <FormItem label="规则名称">
jim's avatar
jim committed
304
              {getFieldDecorator('name')(<Input placeholder="请输入" />)}
afc163's avatar
afc163 committed
305 306
            </FormItem>
          </Col>
307
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
308 309 310 311 312 313 314 315 316
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
317
          <Col md={8} sm={24}>
FOCUS's avatar
FOCUS committed
318
            <span className={styles.submitButtons}>
jim's avatar
jim committed
319 320 321 322 323 324
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
afc163's avatar
afc163 committed
325 326 327 328 329 330 331 332 333 334 335
              <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
                展开 <Icon type="down" />
              </a>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  renderAdvancedForm() {
陈帅's avatar
陈帅 committed
336 337 338
    const {
      form: { getFieldDecorator },
    } = this.props;
afc163's avatar
afc163 committed
339 340
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
afc163's avatar
afc163 committed
341
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
342
          <Col md={8} sm={24}>
ddcat1115's avatar
ddcat1115 committed
343
            <FormItem label="规则名称">
jim's avatar
jim committed
344
              {getFieldDecorator('name')(<Input placeholder="请输入" />)}
afc163's avatar
afc163 committed
345 346
            </FormItem>
          </Col>
347
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
348 349 350 351 352 353 354 355 356
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
357
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
358
            <FormItem label="调用次数">
jim's avatar
jim committed
359
              {getFieldDecorator('number')(<InputNumber style={{ width: '100%' }} />)}
afc163's avatar
afc163 committed
360 361 362
            </FormItem>
          </Col>
        </Row>
363 364
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
365 366 367 368 369 370
            <FormItem label="更新日期">
              {getFieldDecorator('date')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入更新日期" />
              )}
            </FormItem>
          </Col>
371
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
372 373 374 375 376 377 378 379 380
            <FormItem label="使用状态">
              {getFieldDecorator('status3')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
381
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
382 383 384 385 386 387 388 389 390 391
            <FormItem label="使用状态">
              {getFieldDecorator('status4')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
        </Row>
afc163's avatar
afc163 committed
392
        <div style={{ overflow: 'hidden' }}>
陈帅's avatar
陈帅 committed
393
          <div style={{ float: 'right', marginBottom: 24 }}>
jim's avatar
jim committed
394 395 396 397 398 399
            <Button type="primary" htmlType="submit">
              查询
            </Button>
            <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
              重置
            </Button>
afc163's avatar
afc163 committed
400 401 402
            <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
              收起 <Icon type="up" />
            </a>
陈帅's avatar
陈帅 committed
403
          </div>
afc163's avatar
afc163 committed
404 405 406 407 408 409
        </div>
      </Form>
    );
  }

  renderForm() {
陈帅's avatar
陈帅 committed
410 411
    const { expandForm } = this.state;
    return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
afc163's avatar
afc163 committed
412 413
  }

414
  render() {
陈帅's avatar
陈帅 committed
415
    const {
416
      BLOCK_NAME_CAMEL_CASE: { data },
陈帅's avatar
陈帅 committed
417
      loading,
陈帅's avatar
陈帅 committed
418
      form,
陈帅's avatar
陈帅 committed
419
    } = this.props;
陈帅's avatar
陈帅 committed
420

jim's avatar
jim committed
421
    const { selectedRows, modalVisible, updateModalVisible, stepFormValues } = this.state;
422 423 424
    const menu = (
      <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
        <Menu.Item key="remove">删除</Menu.Item>
425
        <Menu.Item key="approval">批量审批</Menu.Item>
426 427 428
      </Menu>
    );

429 430 431 432
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
    };
ddcat1115's avatar
ddcat1115 committed
433 434 435 436
    const updateMethods = {
      handleUpdateModalVisible: this.handleUpdateModalVisible,
      handleUpdate: this.handleUpdate,
    };
437
    return (
438
      <Fragment>
439
        <Card bordered={false}>
440
          <div className={styles.tableList}>
jim's avatar
jim committed
441
            <div className={styles.tableListForm}>{this.renderForm()}</div>
442
            <div className={styles.tableListOperator}>
afc163's avatar
afc163 committed
443 444 445
              <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>
                新建
              </Button>
jim's avatar
jim committed
446 447 448 449 450 451 452 453 454 455
              {selectedRows.length > 0 && (
                <span>
                  <Button>批量操作</Button>
                  <Dropdown overlay={menu}>
                    <Button>
                      更多操作 <Icon type="down" />
                    </Button>
                  </Dropdown>
                </span>
              )}
456 457 458
            </div>
            <StandardTable
              selectedRows={selectedRows}
Andreas Cederström's avatar
Andreas Cederström committed
459
              loading={loading}
460
              data={data}
ddcat1115's avatar
ddcat1115 committed
461
              columns={this.columns}
462 463 464 465 466
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
陈帅's avatar
陈帅 committed
467
        <CreateForm {...parentMethods} modalVisible={modalVisible} form={form} />
jim's avatar
jim committed
468 469 470 471 472
        {stepFormValues && Object.keys(stepFormValues).length ? (
          <UpdateForm
            {...updateMethods}
            updateModalVisible={updateModalVisible}
            values={stepFormValues}
陈帅's avatar
陈帅 committed
473
            form={form}
jim's avatar
jim committed
474 475
          />
        ) : null}
476
      </Fragment>
477 478 479
    );
  }
}
lijiehua's avatar
lijiehua committed
480

陈帅's avatar
陈帅 committed
481
export default Form.create<TableListProps>()(TableList);