list.js 3.7 KB
Newer Older
vipcxj's avatar
vipcxj committed
1 2 3
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
4
import { Button } from 'antd';
vipcxj's avatar
vipcxj committed
5
import { connect } from 'dva';
vipcxj's avatar
vipcxj committed
6
import TableEx from '../../../../components/table/index';
vipcxj's avatar
vipcxj committed
7
import config from '../../../../utils/config';
8
import { thisPush } from '../../../../services/route';
vipcxj's avatar
vipcxj committed
9 10 11 12 13 14
import styles from './list.less';

const columns = [{
  title: '流程',
  dataIndex: 'pName',
  key: 'pName',
vipcxj's avatar
vipcxj committed
15
  filterType: 'text',
vipcxj's avatar
vipcxj committed
16 17 18 19
}, {
  title: '任务',
  dataIndex: 'nName',
  key: 'nName',
vipcxj's avatar
vipcxj committed
20
  filterType: 'text',
vipcxj's avatar
vipcxj committed
21 22 23 24
}, {
  title: '状态',
  dataIndex: 'state',
  key: 'state',
vipcxj's avatar
vipcxj committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  filterType: 'enum',
  filterEnums: [{
    text: '状态1',
    value: '状态1',
  }, {
    text: '状态2',
    value: '状态2',
  }, {
    text: '状态3',
    value: '状态3',
  }, {
    text: '状态4',
    value: '状态4',
  }, {
    text: '状态5',
    value: '状态5',
  }],
vipcxj's avatar
vipcxj committed
42 43 44 45
}, {
  title: '日期',
  dataIndex: 'date',
  key: 'date',
vipcxj's avatar
vipcxj committed
46
  filterType: 'date',
vipcxj's avatar
vipcxj committed
47 48 49 50 51 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
  render(date) {
    return date.format(config.defaultDateTimeFormat);
  },
}, {
  title: '期限',
  dataIndex: 'deadline',
  key: 'deadline',
  render(deadline) {
    const now = moment();
    const late = deadline.diff(now, 'days', true);
    if (late < 0) {
      const style = {
        color: '#f04134',
      };
      return <span style={style}>{ `超时 ${deadline.from(now, true)}` }</span>;
    } else if (late < 1) {
      const style = {
        color: '#ffbf00',
      };
      return <span style={style}>{ `仅剩 ${deadline.to(now, true)}` }</span>;
    } else {
      const style = {
        color: '#00a854',
      };
      return <span style={style}>{ `还剩 ${deadline.to(now, true)}` }</span>;
    }
  },
}];


class List extends React.Component {

  constructor(props, context) {
    super(props, context);
vipcxj's avatar
vipcxj committed
81 82 83 84 85 86 87
    this.loadData = this::this.loadData;
    this.getCurrent = this::this.getCurrent;
    this.state = {
      filters: [],
      current: 1,
      pageSize: 10,
    };
vipcxj's avatar
vipcxj committed
88 89
  }
  componentDidMount() {
vipcxj's avatar
vipcxj committed
90 91 92 93 94
    this.loadData();
  }
  getCurrent() {
    const { num } = this.props.task;
    const pageNum = ((num / this.state.pageSize) | 0) + 1;
95
    return this.state.current > pageNum ? pageNum : this.state.current;
vipcxj's avatar
vipcxj committed
96
  }
vipcxj's avatar
vipcxj committed
97 98 99 100 101 102 103 104 105 106
  loadData() {
    const filters0 = this.state.filters
      .filter(({ filter }) => !!filter)
      .map(({ key, filter }) => ([
        `f-${key}`,
        filter,
      ]));
    const psz = this.state.pageSize; // eslint-disable-line no-shadow
    const pst = (this.state.current - 1) * psz;
    this.props.dispatch({ type: 'task/fetchTasks', payload: { pst, psz, filters: filters0 } });
vipcxj's avatar
vipcxj committed
107 108
  }
  render() {
vipcxj's avatar
vipcxj committed
109
    const { list, num } = this.props.task;
vipcxj's avatar
vipcxj committed
110 111 112
    const tableProps = {
      dataSource: list,
      columns,
vipcxj's avatar
vipcxj committed
113
      filters: this.state.filters.map(filter => filter.filter),
vipcxj's avatar
vipcxj committed
114 115
      loading: this.props.loading.effects['task/fetchTasks'],
      pagination: {
vipcxj's avatar
vipcxj committed
116
        current: this.state.current,
vipcxj's avatar
vipcxj committed
117
        total: num,
vipcxj's avatar
vipcxj committed
118 119 120 121 122 123 124 125 126
        pageSize: this.state.pageSize,
      },
      onChange: (pagination) => {
        this.setState({
          current: pagination.current,
          pageSize: pagination.pageSize,
        }, () => {
          this.loadData();
        });
vipcxj's avatar
vipcxj committed
127
      },
vipcxj's avatar
vipcxj committed
128 129 130 131 132 133 134
      onFilter: (filters) => {
        this.setState({
          filters,
          current: 1,
        }, () => {
          this.loadData();
        });
vipcxj's avatar
vipcxj committed
135 136 137 138 139
      },
    };
    return (
      <div className={styles.wrapper}>
        <div className={styles.container}>
140
          <Button onClick={() => { thisPush(this, { pathname: '../detail', state: { a: 1, b: 2, c: 3 } }); }}>detail</Button>
vipcxj's avatar
vipcxj committed
141
          <TableEx {...tableProps} />
vipcxj's avatar
vipcxj committed
142 143 144 145 146 147 148 149 150 151 152 153
        </div>
      </div>
    );
  }

}

List.propTypes = {
  task: PropTypes.object.isRequired,
};

export default connect(({ task, loading }) => ({ task, loading }))(List);