index.tsx 12.7 KB
Newer Older
jim's avatar
jim committed
1
import {
陈帅's avatar
陈帅 committed
2 3
  Badge,
  Button,
jim's avatar
jim committed
4
  Card,
陈帅's avatar
陈帅 committed
5 6 7 8
  Col,
  DatePicker,
  Divider,
  Dropdown,
jim's avatar
jim committed
9 10
  Form,
  Icon,
陈帅's avatar
陈帅 committed
11
  Input,
jim's avatar
jim committed
12
  InputNumber,
陈帅's avatar
陈帅 committed
13 14 15
  Menu,
  Row,
  Select,
jim's avatar
jim committed
16 17
  message,
} from 'antd';
陈帅's avatar
陈帅 committed
18 19
import React, { Component, Fragment } from 'react';

陈帅's avatar
陈帅 committed
20
import { Dispatch } from 'redux';
陈帅's avatar
陈帅 committed
21
import { FormComponentProps } from 'antd/es/form';
陈帅's avatar
陈帅 committed
22
import { PageHeaderWrapper } from '@ant-design/pro-layout';
陈帅's avatar
陈帅 committed
23 24 25
import { SorterResult } from 'antd/es/table';
import { connect } from 'dva';
import moment from 'moment';
陈帅's avatar
陈帅 committed
26 27
import { IStateType } from './model';
import CreateForm from './components/CreateForm';
陈帅's avatar
陈帅 committed
28 29 30 31
import UpdateForm, { IFormValsType } from './components/UpdateForm';
import { TableListItem, TableListPagination, TableListParams } from './data';
import StandardTable, { StandardTableColumnProps } from './components/StandardTable';
import styles from './style.less';
32 33

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

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

陈帅's avatar
陈帅 committed
44
interface TableListProps extends FormComponentProps {
陈帅's avatar
陈帅 committed
45
  dispatch: Dispatch<any>;
陈帅's avatar
陈帅 committed
46 47 48
  loading: boolean;
  BLOCK_NAME_CAMEL_CASE: IStateType;
}
ddcat1115's avatar
ddcat1115 committed
49

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

/* eslint react/no-multi-comp:0 */
陈帅's avatar
陈帅 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
@connect(
  ({
    BLOCK_NAME_CAMEL_CASE,
    loading,
  }: {
    BLOCK_NAME_CAMEL_CASE: IStateType;
    loading: {
      models: {
        [key: string]: boolean;
      };
    };
  }) => ({
    BLOCK_NAME_CAMEL_CASE,
    loading: loading.models.rule,
陈帅's avatar
陈帅 committed
74
  }),
陈帅's avatar
陈帅 committed
75 76 77
)
class TableList extends Component<TableListProps, TableListState> {
  state: TableListState = {
78 79 80 81 82 83 84 85
    modalVisible: false,
    updateModalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
    stepFormValues: {},
  };

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

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

陈帅's avatar
陈帅 committed
154 155 156
  handleStandardTableChange = (
    pagination: Partial<TableListPagination>,
    filtersArg: Record<keyof TableListItem, string[]>,
陈帅's avatar
陈帅 committed
157
    sorter: SorterResult<TableListItem>,
陈帅's avatar
陈帅 committed
158
  ) => {
159 160 161 162 163 164 165 166 167
    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
168
    const params: Partial<TableListParams> = {
169 170 171 172 173 174 175 176 177 178
      currentPage: pagination.current,
      pageSize: pagination.pageSize,
      ...formValues,
      ...filters,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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