index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import queryString from 'query-string';
  4. import { connect } from 'dva';
  5. import { routerRedux } from 'dva/router';
  6. import { Card } from 'antd';
  7. import TerminalTableList from './table';
  8. import TerminalSearch from './search';
  9. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  10. @connect(state => ({ terminal: state.terminal }))
  11. export default class TerminalList extends Component {
  12. static propTypes = {
  13. Terminal: PropTypes.object,
  14. location: PropTypes.object,
  15. dispatch: PropTypes.func,
  16. };
  17. render() {
  18. const { dispatch, location, terminal } = this.props;
  19. location.query = queryString.parse(location.search);
  20. const { query, pathname } = location;
  21. const { field, keyword, ...filters } = query;
  22. Object.keys(filters).map(key => { filters[key] ? null : delete filters[key] });
  23. if (field && keyword) {
  24. filters.field = field;
  25. filters.keyword = keyword;
  26. }
  27. const { list, listLoading, pagination } = terminal;
  28. const searchProps = {
  29. field,
  30. keyword,
  31. onSearch: (payload) => {
  32. if (!payload.keyword.length) {
  33. delete payload.field;
  34. delete payload.keyword;
  35. }
  36. dispatch(routerRedux.push({
  37. pathname,
  38. search: queryString.stringify({
  39. ...payload
  40. })
  41. }));
  42. },
  43. onAdd: () => {
  44. dispatch(routerRedux.push({
  45. pathname: '/terminal/user/add',
  46. state: filters,
  47. }));
  48. }
  49. };
  50. const listProps = {
  51. curStatus: filters.status,
  52. dataSource: list,
  53. loading: listLoading,
  54. pagination,
  55. location,
  56. onChange: (pagination, filterArgs) => {
  57. const getValue = obj => Object.keys(obj).map(key => obj[key]).join(',');
  58. const tableFilters = Object.keys(filterArgs).reduce((obj, key) => {
  59. const newObj = { ...obj };
  60. newObj[key] = getValue(filterArgs[key]);
  61. return newObj;
  62. }, {});
  63. const data = { ...filters, ...tableFilters };
  64. Object.keys(data).map(key => data[key] ? null : delete data[key]);
  65. dispatch(routerRedux.push({
  66. pathname,
  67. search: queryString.stringify({
  68. ...data,
  69. pageNo: pagination.current,
  70. pageSize: pagination.pageSize,
  71. }),
  72. }));
  73. },
  74. onEditItem: (item) => {
  75. dispatch(routerRedux.push({
  76. pathname: `/terminal/user/edit/${item.id}`,
  77. state: filters,
  78. record: item,
  79. }));
  80. },
  81. onDeleteItem: (id) => {
  82. dispatch({
  83. type: 'terminal/delete',
  84. payload: id,
  85. callback: () => {
  86. dispatch(
  87. routerRedux.push({
  88. pathname,
  89. search: queryString.stringify(filters),
  90. })
  91. );
  92. }
  93. });
  94. },
  95. onRecoverItem: (payload) => {
  96. dispatch({
  97. type: 'terminal/recover',
  98. payload,
  99. callback: () => {
  100. dispatch(
  101. routerRedux.push({
  102. pathname,
  103. search: queryString.stringify(filters),
  104. })
  105. );
  106. }
  107. });
  108. }
  109. };
  110. return (
  111. <PageHeaderLayout>
  112. <Card>
  113. <TerminalSearch { ...searchProps } />
  114. <TerminalTableList { ...listProps } />
  115. </Card>
  116. </PageHeaderLayout>
  117. );
  118. }
  119. }