TableList.js 17.6 KB
Newer Older
ddcat1115's avatar
ddcat1115 committed
1
import React, { PureComponent, Fragment } from 'react';
2
import { connect } from 'dva';
ddcat1115's avatar
ddcat1115 committed
3
import moment from 'moment';
ddcat1115's avatar
ddcat1115 committed
4 5
import { Row, Col, Card, Form, Input, Select, Icon, Button, Dropdown, Menu,
  InputNumber, DatePicker, Modal, message, Badge, Divider, Steps, Radio } from 'antd';
6 7 8 9 10 11
import StandardTable from '../../components/StandardTable';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';

import styles from './TableList.less';

const FormItem = Form.Item;
ddcat1115's avatar
ddcat1115 committed
12 13
const { Step } = Steps;
const { TextArea } = Input;
afc163's avatar
afc163 committed
14
const { Option } = Select;
ddcat1115's avatar
ddcat1115 committed
15
const RadioGroup = Radio.Group;
16
const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
ddcat1115's avatar
ddcat1115 committed
17 18
const statusMap = ['default', 'processing', 'success', 'error'];
const status = ['关闭', '运行中', '已上线', '异常'];
19

20
const CreateForm = Form.create()((props) => {
valleykid's avatar
valleykid committed
21
  const { modalVisible, form, handleAdd, handleModalVisible } = props;
22
  const okHandle = () => {
valleykid's avatar
valleykid committed
23
    form.validateFields((err, fieldsValue) => {
24
      if (err) return;
jim's avatar
jim committed
25
      form.resetFields();
valleykid's avatar
valleykid committed
26
      handleAdd(fieldsValue);
27 28 29 30
    });
  };
  return (
    <Modal
ddcat1115's avatar
ddcat1115 committed
31
      destroyOnClose
32 33 34
      title="新建规则"
      visible={modalVisible}
      onOk={okHandle}
valleykid's avatar
valleykid committed
35
      onCancel={() => handleModalVisible()}
36 37 38 39 40 41 42
    >
      <FormItem
        labelCol={{ span: 5 }}
        wrapperCol={{ span: 15 }}
        label="描述"
      >
        {form.getFieldDecorator('desc', {
ddcat1115's avatar
ddcat1115 committed
43
          rules: [{ required: true, message: '请输入至少五个字符的规则描述!', min: 5 }],
44
        })(
valleykid's avatar
valleykid committed
45
          <Input placeholder="请输入" />
46 47 48 49 50 51
        )}
      </FormItem>
    </Modal>
  );
});

ddcat1115's avatar
ddcat1115 committed
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 93 94 95 96 97 98 99 100 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 130 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
@Form.create()
class UpdateForm extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      formVals: {
        name: props.values.name,
        desc: props.values.desc,
        key: props.values.key,
        target: '0',
        template: '0',
        type: '1',
        time: '',
        frequency: 'month',
      },
      currentStep: 0,
    };

    this.formLayout = {
      labelCol: { span: 7 },
      wrapperCol: { span: 13 },
    };
  }
  handleNext = (currentStep) => {
    const { form, handleUpdate } = this.props;
    form.validateFields((err, fieldsValue) => {
      if (err) return;
      const formVals = { ...this.state.formVals, ...fieldsValue };
      this.setState({
        formVals,
      }, () => {
        if (currentStep < 2) {
          this.forward();
        } else {
          handleUpdate(this.state.formVals);
        }
      });
    });
  }
  backward = () => {
    this.setState({
      currentStep: this.state.currentStep - 1,
    });
  }
  forward = () => {
    this.setState({
      currentStep: this.state.currentStep + 1,
    });
  }
  renderContent = (currentStep, formVals) => {
    const { form } = this.props;
    if (currentStep === 1) {
      return [
        <FormItem key="target" {...this.formLayout} label="监控对象">
          {form.getFieldDecorator('target', {
            initialValue: formVals.target,
          })(
            <Select style={{ width: '100%' }}>
              <Option value="0">表一</Option>
              <Option value="1">表二</Option>
            </Select>
          )}
        </FormItem>,
        <FormItem key="template" {...this.formLayout} label="规则模板">
          {form.getFieldDecorator('template', {
            initialValue: formVals.template,
          })(
            <Select style={{ width: '100%' }}>
              <Option value="0">规则模板一</Option>
              <Option value="1">规则模板二</Option>
            </Select>
          )}
        </FormItem>,
        <FormItem key="type" {...this.formLayout} label="规则类型">
          {form.getFieldDecorator('type', {
            initialValue: formVals.type,
          })(
            <RadioGroup>
              <Radio value="0"></Radio>
              <Radio value="1"></Radio>
            </RadioGroup>
          )}
        </FormItem>,
      ];
    }
    if (currentStep === 2) {
      return [
        <FormItem key="time" {...this.formLayout} label="开始时间">
          {form.getFieldDecorator('time', {
            rules: [{ required: true, message: '请选择开始时间!' }],
          })(
            <DatePicker
              style={{ width: '100%' }}
              showTime
              format="YYYY-MM-DD HH:mm:ss"
              placeholder="选择开始时间"
            />
          )}
        </FormItem>,
        <FormItem key="frequency" {...this.formLayout} label="调度周期">
          {form.getFieldDecorator('frequency', {
            initialValue: formVals.frequency,
          })(
            <Select style={{ width: '100%' }}>
              <Option value="month"></Option>
              <Option value="week"></Option>
            </Select>
          )}
        </FormItem>,
      ];
    }
    return [
      <FormItem key="name" {...this.formLayout} label="规则名称">
        {form.getFieldDecorator('name', {
          rules: [{ required: true, message: '请输入规则名称!' }],
          initialValue: formVals.name,
        })(
          <Input placeholder="请输入" />
        )}
      </FormItem>,
      <FormItem key="desc" {...this.formLayout} label="规则描述">
        {form.getFieldDecorator('desc', {
          rules: [{ required: true, message: '请输入至少五个字符的规则描述!', min: 5 }],
          initialValue: formVals.desc,
        })(
          <TextArea rows={4} placeholder="请输入至少五个字符" />
        )}
      </FormItem>,
    ];
  }
  renderFooter = (currentStep) => {
    const { handleUpdateModalVisible } = this.props;
    if (currentStep === 1) {
      return [
        <Button key="back" style={{ float: 'left' }} onClick={this.backward}>上一步</Button>,
        <Button key="cancel" onClick={() => handleUpdateModalVisible()}>取消</Button>,
        <Button key="forward" type="primary" onClick={() => this.handleNext(currentStep)}>
          下一步
        </Button>,
      ];
    }
    if (currentStep === 2) {
      return [
        <Button key="back" style={{ float: 'left' }} onClick={this.backward}>上一步</Button>,
        <Button key="cancel" onClick={() => handleUpdateModalVisible()}>取消</Button>,
        <Button key="submit" type="primary" onClick={() => this.handleNext(currentStep)}>
          完成
        </Button>,
      ];
    }
    return [
      <Button key="cancel" onClick={() => handleUpdateModalVisible()}>取消</Button>,
      <Button key="forward" type="primary" onClick={() => this.handleNext(currentStep)}>
        下一步
      </Button>,
    ];
  }
  render() {
    const { updateModalVisible, handleUpdateModalVisible } = this.props;
    const { currentStep, formVals } = this.state;

    return (
      <Modal
        width={640}
        bodyStyle={{ padding: '32px 40px 48px' }}
        destroyOnClose
        title="规则配置"
        visible={updateModalVisible}
        footer={this.renderFooter(currentStep)}
        onCancel={() => handleUpdateModalVisible()}
      >
ddcat1115's avatar
ddcat1115 committed
224
        <Steps style={{ marginBottom: 28 }} size="small" current={currentStep}>
ddcat1115's avatar
ddcat1115 committed
225 226 227 228 229 230 231 232 233 234 235
          <Step title="基本信息" />
          <Step title="配置规则属性" />
          <Step title="设定调度周期" />
        </Steps>
        { this.renderContent(currentStep, formVals) }
      </Modal>
    );
  }
}

/* eslint react/no-multi-comp:0 */
Andreas Cederström's avatar
Andreas Cederström committed
236 237 238
@connect(({ rule, loading }) => ({
  rule,
  loading: loading.models.rule,
239 240 241 242 243
}))
@Form.create()
export default class TableList extends PureComponent {
  state = {
    modalVisible: false,
ddcat1115's avatar
ddcat1115 committed
244
    updateModalVisible: false,
245 246 247
    expandForm: false,
    selectedRows: [],
    formValues: {},
ddcat1115's avatar
ddcat1115 committed
248
    stepFormValues: {},
249 250 251 252 253 254 255 256 257
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'rule/fetch',
    });
  }

ddcat1115's avatar
ddcat1115 committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  columns = [
    {
      title: '规则名称',
      dataIndex: 'name',
    },
    {
      title: '描述',
      dataIndex: 'desc',
    },
    {
      title: '服务调用次数',
      dataIndex: 'callNo',
      sorter: true,
      align: 'right',
      render: val => `${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) {
        return <Badge status={statusMap[val]} text={status[val]} />;
      },
    },
    {
      title: '上次调度时间',
      dataIndex: 'updatedAt',
      sorter: true,
      render: val => <span>{moment(val).format('YYYY-MM-DD HH:mm:ss')}</span>,
    },
    {
      title: '操作',
      render: (text, record) => (
        <Fragment>
          <a onClick={() => this.handleUpdateModalVisible(true, record)}>配置</a>
          <Divider type="vertical" />
          <a href="">订阅警报</a>
        </Fragment>
      ),
    },
  ];

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    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 = {
      currentPage: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
      ...filters,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
      type: 'rule/fetch',
      payload: params,
    });
  }

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
348 349 350
    this.setState({
      formValues: {},
    });
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    dispatch({
      type: 'rule/fetch',
      payload: {},
    });
  }

  toggleForm = () => {
    this.setState({
      expandForm: !this.state.expandForm,
    });
  }

  handleMenuClick = (e) => {
    const { dispatch } = this.props;
    const { selectedRows } = this.state;

    if (!selectedRows) return;
    switch (e.key) {
      case 'remove':
        dispatch({
          type: 'rule/remove',
          payload: {
ddcat1115's avatar
ddcat1115 committed
373
            key: selectedRows.map(row => row.key),
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
          },
          callback: () => {
            this.setState({
              selectedRows: [],
            });
          },
        });
        break;
      default:
        break;
    }
  }

  handleSelectRows = (rows) => {
    this.setState({
      selectedRows: rows,
    });
  }

  handleSearch = (e) => {
    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: 'rule/fetch',
        payload: values,
      });
    });
  }

  handleModalVisible = (flag) => {
    this.setState({
      modalVisible: !!flag,
    });
  }

ddcat1115's avatar
ddcat1115 committed
423 424 425 426 427 428 429
  handleUpdateModalVisible = (flag, record) => {
    this.setState({
      updateModalVisible: !!flag,
      stepFormValues: record || {},
    });
  }

valleykid's avatar
valleykid committed
430
  handleAdd = (fields) => {
431 432 433
    this.props.dispatch({
      type: 'rule/add',
      payload: {
ddcat1115's avatar
ddcat1115 committed
434
        desc: fields.desc,
435 436 437 438
      },
    });

    message.success('添加成功');
ddcat1115's avatar
ddcat1115 committed
439 440 441 442 443 444 445 446 447 448 449
    this.handleModalVisible();
  }

  handleUpdate = (fields) => {
    this.props.dispatch({
      type: 'rule/update',
      payload: {
        name: fields.name,
        desc: fields.desc,
        key: fields.key,
      },
450
    });
ddcat1115's avatar
ddcat1115 committed
451 452 453

    message.success('配置成功');
    this.handleUpdateModalVisible();
454 455
  }

afc163's avatar
afc163 committed
456 457 458 459
  renderSimpleForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
460 461
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
ddcat1115's avatar
ddcat1115 committed
462 463
            <FormItem label="规则名称">
              {getFieldDecorator('name')(
afc163's avatar
afc163 committed
464 465 466 467
                <Input placeholder="请输入" />
              )}
            </FormItem>
          </Col>
468
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
469 470 471 472 473 474 475 476 477
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
478
          <Col md={8} sm={24}>
FOCUS's avatar
FOCUS committed
479
            <span className={styles.submitButtons}>
afc163's avatar
afc163 committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
              <Button type="primary" htmlType="submit">查询</Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
              <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
                展开 <Icon type="down" />
              </a>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  renderAdvancedForm() {
    const { getFieldDecorator } = this.props.form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
afc163's avatar
afc163 committed
496
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
497
          <Col md={8} sm={24}>
ddcat1115's avatar
ddcat1115 committed
498 499
            <FormItem label="规则名称">
              {getFieldDecorator('name')(
afc163's avatar
afc163 committed
500 501 502 503
                <Input placeholder="请输入" />
              )}
            </FormItem>
          </Col>
504
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
505 506 507 508 509 510 511 512 513
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
514
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
515 516 517 518 519 520 521
            <FormItem label="调用次数">
              {getFieldDecorator('number')(
                <InputNumber style={{ width: '100%' }} />
              )}
            </FormItem>
          </Col>
        </Row>
522 523
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
524 525 526 527 528 529
            <FormItem label="更新日期">
              {getFieldDecorator('date')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入更新日期" />
              )}
            </FormItem>
          </Col>
530
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
531 532 533 534 535 536 537 538 539
            <FormItem label="使用状态">
              {getFieldDecorator('status3')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
540
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
541 542 543 544 545 546 547 548 549 550
            <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
551
        <div style={{ overflow: 'hidden' }}>
552
          <span style={{ float: 'right', marginBottom: 24 }}>
afc163's avatar
afc163 committed
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
            <Button type="primary" htmlType="submit">查询</Button>
            <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>重置</Button>
            <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
              收起 <Icon type="up" />
            </a>
          </span>
        </div>
      </Form>
    );
  }

  renderForm() {
    return this.state.expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
  }

568
  render() {
Andreas Cederström's avatar
Andreas Cederström committed
569
    const { rule: { data }, loading } = this.props;
ddcat1115's avatar
ddcat1115 committed
570
    const { selectedRows, modalVisible, updateModalVisible, stepFormValues } = this.state;
571 572 573
    const menu = (
      <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
        <Menu.Item key="remove">删除</Menu.Item>
574
        <Menu.Item key="approval">批量审批</Menu.Item>
575 576 577
      </Menu>
    );

578 579 580 581
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
    };
ddcat1115's avatar
ddcat1115 committed
582 583 584 585
    const updateMethods = {
      handleUpdateModalVisible: this.handleUpdateModalVisible,
      handleUpdate: this.handleUpdate,
    };
586
    return (
niko's avatar
niko committed
587
      <PageHeaderLayout title="查询表格">
588
        <Card bordered={false}>
589 590
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>
afc163's avatar
afc163 committed
591
              {this.renderForm()}
592 593
            </div>
            <div className={styles.tableListOperator}>
afc163's avatar
afc163 committed
594 595 596
              <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>
                新建
              </Button>
597
              {
afc163's avatar
afc163 committed
598 599 600 601 602 603 604 605 606 607
                selectedRows.length > 0 && (
                  <span>
                    <Button>批量操作</Button>
                    <Dropdown overlay={menu}>
                      <Button>
                        更多操作 <Icon type="down" />
                      </Button>
                    </Dropdown>
                  </span>
                )
608
              }
609 610 611
            </div>
            <StandardTable
              selectedRows={selectedRows}
Andreas Cederström's avatar
Andreas Cederström committed
612
              loading={loading}
613
              data={data}
ddcat1115's avatar
ddcat1115 committed
614
              columns={this.columns}
615 616 617 618 619
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
620
        <CreateForm
valleykid's avatar
valleykid committed
621
          {...parentMethods}
622 623
          modalVisible={modalVisible}
        />
ddcat1115's avatar
ddcat1115 committed
624 625 626 627 628 629 630 631 632 633
        {
          stepFormValues && Object.keys(stepFormValues).length ?
            (
              <UpdateForm
                {...updateMethods}
                updateModalVisible={updateModalVisible}
                values={stepFormValues}
              />
            ) : null
        }
634 635 636 637
      </PageHeaderLayout>
    );
  }
}