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
import { StateType } from './model';
陈帅's avatar
陈帅 committed
27
import CreateForm from './components/CreateForm';
陈帅's avatar
陈帅 committed
28
import StandardTable, { StandardTableColumnProps } from './components/StandardTable';
陈帅's avatar
陈帅 committed
29 30 31
import UpdateForm, { FormValsType } from './components/UpdateForm';
import { TableListItem, TableListPagination, TableListParams } from './data.d';

陈帅's avatar
陈帅 committed
32
import styles from './style.less';
33 34

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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