TableList.js 11.1 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';
jim's avatar
jim committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Select,
  Icon,
  Button,
  Dropdown,
  Menu,
  InputNumber,
  DatePicker,
  Modal,
  message,
  Badge,
  Divider,
} from 'antd';
niko's avatar
niko committed
22
import StandardTable from 'components/StandardTable';
23 24 25 26 27
import PageHeaderLayout from '../../layouts/PageHeaderLayout';

import styles from './TableList.less';

const FormItem = Form.Item;
afc163's avatar
afc163 committed
28
const { Option } = Select;
jim's avatar
jim committed
29 30 31 32
const getValue = obj =>
  Object.keys(obj)
    .map(key => obj[key])
    .join(',');
ddcat1115's avatar
ddcat1115 committed
33 34
const statusMap = ['default', 'processing', 'success', 'error'];
const status = ['关闭', '运行中', '已上线', '异常'];
35

jim's avatar
jim committed
36
const CreateForm = Form.create()(props => {
valleykid's avatar
valleykid committed
37
  const { modalVisible, form, handleAdd, handleModalVisible } = props;
38
  const okHandle = () => {
valleykid's avatar
valleykid committed
39
    form.validateFields((err, fieldsValue) => {
40
      if (err) return;
jim's avatar
jim committed
41
      form.resetFields();
valleykid's avatar
valleykid committed
42
      handleAdd(fieldsValue);
43 44 45 46 47 48 49
    });
  };
  return (
    <Modal
      title="新建规则"
      visible={modalVisible}
      onOk={okHandle}
valleykid's avatar
valleykid committed
50
      onCancel={() => handleModalVisible()}
51
    >
jim's avatar
jim committed
52
      <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="描述">
53 54
        {form.getFieldDecorator('desc', {
          rules: [{ required: true, message: 'Please input some description...' }],
jim's avatar
jim committed
55
        })(<Input placeholder="请输入" />)}
56 57 58 59 60
      </FormItem>
    </Modal>
  );
});

Andreas Cederström's avatar
Andreas Cederström committed
61 62 63
@connect(({ rule, loading }) => ({
  rule,
  loading: loading.models.rule,
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
}))
@Form.create()
export default class TableList extends PureComponent {
  state = {
    modalVisible: false,
    expandForm: false,
    selectedRows: [],
    formValues: {},
  };

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

  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,
    });
jim's avatar
jim committed
105
  };
106 107 108 109

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
110 111 112
    this.setState({
      formValues: {},
    });
113 114 115 116
    dispatch({
      type: 'rule/fetch',
      payload: {},
    });
jim's avatar
jim committed
117
  };
118 119

  toggleForm = () => {
120
    const { expandForm } = this.state;
121
    this.setState({
122
      expandForm: !expandForm,
123
    });
jim's avatar
jim committed
124
  };
125

jim's avatar
jim committed
126
  handleMenuClick = e => {
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    const { dispatch } = this.props;
    const { selectedRows } = this.state;

    if (!selectedRows) return;

    switch (e.key) {
      case 'remove':
        dispatch({
          type: 'rule/remove',
          payload: {
            no: selectedRows.map(row => row.no).join(','),
          },
          callback: () => {
            this.setState({
              selectedRows: [],
            });
          },
        });
        break;
      default:
        break;
    }
jim's avatar
jim committed
149
  };
150

jim's avatar
jim committed
151
  handleSelectRows = rows => {
152 153 154
    this.setState({
      selectedRows: rows,
    });
jim's avatar
jim committed
155
  };
156

jim's avatar
jim committed
157
  handleSearch = e => {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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,
      });
    });
jim's avatar
jim committed
179
  };
180

jim's avatar
jim committed
181
  handleModalVisible = flag => {
182 183 184
    this.setState({
      modalVisible: !!flag,
    });
jim's avatar
jim committed
185
  };
186

jim's avatar
jim committed
187
  handleAdd = fields => {
188 189
    const { dispatch } = this.props;
    dispatch({
190 191
      type: 'rule/add',
      payload: {
valleykid's avatar
valleykid committed
192
        description: fields.desc,
193 194 195 196 197 198 199
      },
    });

    message.success('添加成功');
    this.setState({
      modalVisible: false,
    });
jim's avatar
jim committed
200
  };
201

afc163's avatar
afc163 committed
202
  renderSimpleForm() {
203 204
    const { form } = this.props;
    const { getFieldDecorator } = form;
afc163's avatar
afc163 committed
205 206
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
207 208
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
209
            <FormItem label="规则编号">
jim's avatar
jim committed
210
              {getFieldDecorator('no')(<Input placeholder="请输入" />)}
afc163's avatar
afc163 committed
211 212
            </FormItem>
          </Col>
213
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
214 215 216 217 218 219 220 221 222
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
223
          <Col md={8} sm={24}>
FOCUS's avatar
FOCUS committed
224
            <span className={styles.submitButtons}>
jim's avatar
jim committed
225 226 227 228 229 230
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
afc163's avatar
afc163 committed
231 232 233 234 235 236 237 238 239 240 241
              <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
                展开 <Icon type="down" />
              </a>
            </span>
          </Col>
        </Row>
      </Form>
    );
  }

  renderAdvancedForm() {
242 243
    const { form } = this.props;
    const { getFieldDecorator } = form;
afc163's avatar
afc163 committed
244 245
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
afc163's avatar
afc163 committed
246
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
247
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
248
            <FormItem label="规则编号">
jim's avatar
jim committed
249
              {getFieldDecorator('no')(<Input placeholder="请输入" />)}
afc163's avatar
afc163 committed
250 251
            </FormItem>
          </Col>
252
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
253 254 255 256 257 258 259 260 261
            <FormItem label="使用状态">
              {getFieldDecorator('status')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
262
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
263
            <FormItem label="调用次数">
jim's avatar
jim committed
264
              {getFieldDecorator('number')(<InputNumber style={{ width: '100%' }} />)}
afc163's avatar
afc163 committed
265 266 267
            </FormItem>
          </Col>
        </Row>
268 269
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
270 271 272 273 274 275
            <FormItem label="更新日期">
              {getFieldDecorator('date')(
                <DatePicker style={{ width: '100%' }} placeholder="请输入更新日期" />
              )}
            </FormItem>
          </Col>
276
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
277 278 279 280 281 282 283 284 285
            <FormItem label="使用状态">
              {getFieldDecorator('status3')(
                <Select placeholder="请选择" style={{ width: '100%' }}>
                  <Option value="0">关闭</Option>
                  <Option value="1">运行中</Option>
                </Select>
              )}
            </FormItem>
          </Col>
286
          <Col md={8} sm={24}>
afc163's avatar
afc163 committed
287 288 289 290 291 292 293 294 295 296
            <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
297
        <div style={{ overflow: 'hidden' }}>
陈帅's avatar
陈帅 committed
298
          <div style={{ float: 'right', marginBottom: 24 }}>
jim's avatar
jim committed
299 300 301 302 303 304
            <Button type="primary" htmlType="submit">
              查询
            </Button>
            <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
              重置
            </Button>
afc163's avatar
afc163 committed
305 306 307
            <a style={{ marginLeft: 8 }} onClick={this.toggleForm}>
              收起 <Icon type="up" />
            </a>
陈帅's avatar
陈帅 committed
308
          </div>
afc163's avatar
afc163 committed
309 310 311 312 313 314
        </div>
      </Form>
    );
  }

  renderForm() {
315 316
    const { expandForm } = this.state;
    return expandForm ? this.renderAdvancedForm() : this.renderSimpleForm();
afc163's avatar
afc163 committed
317 318
  }

319
  render() {
陈帅's avatar
陈帅 committed
320 321 322 323
    const {
      rule: { data },
      loading,
    } = this.props;
valleykid's avatar
valleykid committed
324
    const { selectedRows, modalVisible } = this.state;
325

ddcat1115's avatar
ddcat1115 committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    const columns = [
      {
        title: '规则编号',
        dataIndex: 'no',
      },
      {
        title: '描述',
        dataIndex: 'description',
      },
      {
        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,
          },
        ],
        onFilter: (value, record) => record.status.toString() === value,
        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: () => (
          <Fragment>
            <a href="">配置</a>
            <Divider type="vertical" />
            <a href="">订阅警报</a>
          </Fragment>
        ),
      },
    ];

388 389 390
    const menu = (
      <Menu onClick={this.handleMenuClick} selectedKeys={[]}>
        <Menu.Item key="remove">删除</Menu.Item>
391
        <Menu.Item key="approval">批量审批</Menu.Item>
392 393 394
      </Menu>
    );

395 396 397 398 399
    const parentMethods = {
      handleAdd: this.handleAdd,
      handleModalVisible: this.handleModalVisible,
    };

400
    return (
niko's avatar
niko committed
401
      <PageHeaderLayout title="查询表格">
402
        <Card bordered={false}>
403
          <div className={styles.tableList}>
jim's avatar
jim committed
404
            <div className={styles.tableListForm}>{this.renderForm()}</div>
405
            <div className={styles.tableListOperator}>
afc163's avatar
afc163 committed
406 407 408
              <Button icon="plus" type="primary" onClick={() => this.handleModalVisible(true)}>
                新建
              </Button>
jim's avatar
jim committed
409 410 411 412 413 414 415 416 417 418
              {selectedRows.length > 0 && (
                <span>
                  <Button>批量操作</Button>
                  <Dropdown overlay={menu}>
                    <Button>
                      更多操作 <Icon type="down" />
                    </Button>
                  </Dropdown>
                </span>
              )}
419 420 421
            </div>
            <StandardTable
              selectedRows={selectedRows}
Andreas Cederström's avatar
Andreas Cederström committed
422
              loading={loading}
423
              data={data}
ddcat1115's avatar
ddcat1115 committed
424
              columns={columns}
425 426 427 428 429
              onSelectRow={this.handleSelectRows}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
jim's avatar
jim committed
430
        <CreateForm {...parentMethods} modalVisible={modalVisible} />
431 432 433 434
      </PageHeaderLayout>
    );
  }
}