index.jsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { useState, useEffect } from 'react'
  2. import { Select, Spin, Table, Button, Form, Input, Divider, Typography, Popconfirm, notification } from 'antd'
  3. import NavLink from 'umi/navlink'
  4. import { fetchList, apis, fetch } from '@/utils/request'
  5. import Search from '../components/Search'
  6. import List from '../components/List'
  7. import Editor from './components/Editor'
  8. const listTel = fetch(apis.announcement.getTelList)
  9. const deleteTel = fetch(apis.announcement.deleteTel)
  10. const saveTel = fetch(apis.announcement.saveTel)
  11. const updateTel = fetch(apis.announcement.updateTel)
  12. const Condition = props => {
  13. return (
  14. <Search
  15. onSearch={props.onSearch}
  16. onReset={props.onReset}
  17. render={form => {
  18. const { getFieldDecorator, setFieldsValue } = form
  19. return (
  20. <>
  21. <Form.Item label="名称">
  22. {
  23. getFieldDecorator('name')(<Input placeholder="名称" />)
  24. }
  25. </Form.Item>
  26. <Form.Item label="电话">
  27. {
  28. getFieldDecorator('tel')(<Input placeholder="电话" />)
  29. }
  30. </Form.Item>
  31. </>
  32. )
  33. }}
  34. />
  35. )
  36. }
  37. export default props => {
  38. const [loading, setLoading] = useState(false)
  39. const [listData, setListData] = useState([])
  40. const [pagination, setPagination] = useState({})
  41. const [queryParams, setQueryParams] = useState({ pageNum: 1, pageSize: 10 })
  42. const [showEditor, setShowEditor] = useState(false)
  43. const [currentRow, setCurrentRow] = useState()
  44. const handleSearch = vals => {
  45. setQueryParams({
  46. ...queryParams,
  47. ...vals,
  48. pageNum: 1,
  49. })
  50. }
  51. const handlePageChange = (pageNum, pageSize) => {
  52. setQueryParams({
  53. ...queryParams,
  54. pageNum,
  55. pageSize,
  56. })
  57. }
  58. const triggerShowEditor = row => {
  59. setShowEditor(!showEditor)
  60. setCurrentRow({...(row || {})})
  61. }
  62. const handleEditor = vals => {
  63. if (vals.id) {
  64. updateTel({data: vals}).then(res => {
  65. notification.success({message: '信息更新成功'})
  66. triggerShowEditor()
  67. setQueryParams({...queryParams})
  68. })
  69. } else {
  70. saveTel({data: vals}).then(res => {
  71. notification.success({message: '信息保存成功'})
  72. triggerShowEditor()
  73. setQueryParams({...queryParams})
  74. })
  75. }
  76. }
  77. const handleDeleteRow = row => {
  78. deleteTel({data: [row.id]}).then(res => {
  79. notification.success({ message: '删除成功' })
  80. setQueryParams({...queryParams})
  81. })
  82. }
  83. useEffect(() => {
  84. setLoading(true)
  85. listTel({params: queryParams}).then(res => {
  86. const {list, ...pagi} = res
  87. setListData(list)
  88. setPagination(pagi)
  89. setLoading(false)
  90. }).catch(e => setLoading(false))
  91. }, [queryParams])
  92. return (
  93. <div>
  94. <Condition onSearch={handleSearch} onReset={handleSearch} />
  95. <div style={{ margin: '24px 0' }}>
  96. <Button type="primary" onClick={() => triggerShowEditor()}>添加</Button>
  97. </div>
  98. <List dataSource={listData} loading={loading} pagination={pagination} onPageChange={handlePageChange} rowKey="id">
  99. <Table.Column title="名称" dataIndex="name" key="name" />
  100. <Table.Column title="电话" dataIndex="tel" key="tel" />
  101. <Table.Column title="物业类型" dataIndex="type" key="type" render={(item, row) => (
  102. <span>{item == "prop" ? "物业电话" : "公共服务电话"}</span>
  103. )}/>
  104. <Table.Column title="备注" dataIndex="remark" key="remark" />
  105. <Table.Column title="操作" dataIndex="id" key="id" width="200px" render={(_, row) => (
  106. <>
  107. <Popconfirm
  108. title="确认进行删除操作?"
  109. onConfirm={() => handleDeleteRow(row)}
  110. okText="删除"
  111. cancelText="取消"
  112. >
  113. <Button type="link">删除</Button>
  114. </Popconfirm>
  115. <Divider type="vertical" />
  116. <Button type="link" onClick={() => triggerShowEditor(row)}>编辑</Button>
  117. </>
  118. )} />
  119. </List>
  120. <Editor visible={showEditor} dataSource={currentRow} onSubmit={handleEditor} onCancel={triggerShowEditor} />
  121. </div>
  122. )
  123. }