"ListTableList/src/index.tsx" did not exist on "293e55a1266bfa831ddfb2d7dfdb9d54a22e1e35"
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
import { FormComponentProps } from 'antd/es/form';
import { SorterResult } from 'antd/es/table';
陈帅's avatar
陈帅 committed
23 24
import { Dispatch } from 'redux';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
陈帅's avatar
陈帅 committed
25 26 27
import StandardTable, { StandardTableColumnProps } from './components/StandardTable';
import { TableListItem, TableListParams, TableListPagination } from './data';
import { IStateType } from './model';
28
import styles from './style.less';
陈帅's avatar
陈帅 committed
29 30
import UpdateForm, { IFormValsType } from './components/UpdateForm';
import CreateForm from './components/CreateForm';
31 32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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