DictDatasModal.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useEffect, useState } from 'react'
  21. import {
  22. Modal,
  23. Button,
  24. Col,
  25. Table,
  26. Row,
  27. Divider,
  28. Popconfirm,
  29. message
  30. } from 'antd'
  31. import { IDictData } from '../types'
  32. import { ColumnProps } from 'antd/lib/table'
  33. import ButtonGroup from 'antd/es/button/button-group'
  34. import api from 'utils/api'
  35. import request from 'utils/request'
  36. import DictDataFormModal from 'containers/DataManagerDictionary/components/DictDataFormModal'
  37. interface IDictDatasModalProps {
  38. visible: boolean
  39. dictType: string
  40. onCancel: () => void
  41. }
  42. const DictDatasModal = (props: IDictDatasModalProps) => {
  43. const { visible, onCancel, dictType } = props
  44. const [tableLoading, setTableLoading] = useState(false)
  45. const [dictDatas, setDictDatas] = useState<IDictData[]>([])
  46. const [ddFormVisible, setDdFormVisible] = useState(false)
  47. const [ddFormLoading, setDdFormLoading] = useState(false)
  48. const [ddFormView, setDdFormView] = useState<IDictData>({})
  49. const tableColumns: Array<ColumnProps<IDictData>> = [
  50. {
  51. title: '字典编码',
  52. dataIndex: 'dictCode'
  53. },
  54. {
  55. title: '字典标签',
  56. dataIndex: 'dictLabel'
  57. },
  58. {
  59. title: '字典键值',
  60. dataIndex: 'dictValue'
  61. },
  62. {
  63. title: '字典排序',
  64. dataIndex: 'dictSort'
  65. },
  66. {
  67. title: '状态',
  68. dataIndex: 'status',
  69. render: (text) => (
  70. <>
  71. {text === 0 && (
  72. <span
  73. style={{ padding: '2px 4px', background: 'green', color: '#fff' }}
  74. >
  75. 正常
  76. </span>
  77. )}
  78. {(text === 1 || text === '') && (
  79. <span
  80. style={{ padding: '2px 4px', background: 'red', color: '#fff' }}
  81. >
  82. 停用
  83. </span>
  84. )}
  85. </>
  86. )
  87. },
  88. {
  89. title: '备注',
  90. dataIndex: 'remark'
  91. },
  92. {
  93. title: '创建时间'
  94. // dataIndex: 'isDefault'
  95. },
  96. {
  97. title: '操作',
  98. render: (_, data) => {
  99. return (
  100. <>
  101. <a
  102. onClick={() => {
  103. setDdFormVisible(true)
  104. setDdFormView(data)
  105. }}
  106. >
  107. 编辑
  108. </a>
  109. <Divider type="vertical" />
  110. <Popconfirm
  111. title="确定删除?"
  112. placement="bottom"
  113. onConfirm={() => handleDeleteDictData(data)}
  114. >
  115. <a>删除</a>
  116. </Popconfirm>
  117. </>
  118. )
  119. }
  120. }
  121. ]
  122. const queryDictDatas = async () => {
  123. if (!dictType) {
  124. return
  125. }
  126. try {
  127. setTableLoading(true)
  128. const data = await request(api.dictDatas + `?dictType=${dictType}`, {
  129. method: 'GET'
  130. })
  131. // @ts-ignore
  132. setDictDatas(data?.payload ?? [])
  133. } finally {
  134. setTableLoading(false)
  135. }
  136. }
  137. const handleDeleteDictData = async (data: IDictData) => {
  138. try {
  139. setTableLoading(true)
  140. const result = await request(`${api.deleteDictData}${data.dictCode}`, {
  141. method: 'delete'
  142. })
  143. // @ts-ignore
  144. if (result.header.code === 200) {
  145. message.success({ content: '删除成功' })
  146. queryDictDatas()
  147. } else {
  148. // @ts-ignore
  149. // tslint:disable-next-line:no-unused-expression
  150. result.header.msg && message.success({ content: result.header.msg })
  151. }
  152. } finally {
  153. setTableLoading(false)
  154. }
  155. }
  156. const handleSaveDDForm = async (form: IDictData) => {
  157. try {
  158. setDdFormLoading(true)
  159. const url = ddFormView?.dictCode
  160. ? api.updateDictData + ddFormView.dictCode
  161. : api.createDictData
  162. const result = await request(url, {
  163. method: ddFormView?.dictCode ? 'PUT' : 'POST',
  164. headers: {
  165. 'Content-Type': 'application/json'
  166. },
  167. data: { ...ddFormView, dictType, ...form, status: form.status ? 0 : 1 }
  168. })
  169. // @ts-ignore
  170. if (result.header.code === 200) {
  171. message.success({
  172. content: (ddFormView?.dictCode ? '修改' : '新增') + '成功'
  173. })
  174. setDdFormVisible(false)
  175. queryDictDatas()
  176. } else {
  177. // @ts-ignore
  178. // tslint:disable-next-line:no-unused-expression
  179. result.header.msg && message.success({ content: result.header.msg })
  180. }
  181. } finally {
  182. setDdFormLoading(false)
  183. }
  184. }
  185. useEffect(() => {
  186. queryDictDatas()
  187. }, [dictType])
  188. return (
  189. <>
  190. <Modal
  191. title="数据字典数据"
  192. // wrapClassName='ant-modal-small'
  193. visible={visible}
  194. footer={null}
  195. onCancel={onCancel}
  196. destroyOnClose
  197. width={'80%'}
  198. >
  199. <div style={{ padding: '10px 0' }}>
  200. <Button
  201. onClick={() => {
  202. setDdFormVisible(true)
  203. setDdFormView({})
  204. }}
  205. >
  206. 新增
  207. </Button>
  208. </div>
  209. <Row>
  210. <Col span={24}>
  211. <Table
  212. bordered
  213. rowKey="dictCode"
  214. loading={tableLoading}
  215. dataSource={dictDatas}
  216. columns={tableColumns}
  217. pagination={false}
  218. // onChange={this.tableChange}
  219. />
  220. </Col>
  221. </Row>
  222. </Modal>
  223. <DictDataFormModal
  224. visible={ddFormVisible}
  225. fromView={ddFormView}
  226. onSave={handleSaveDDForm}
  227. onCancel={() => setDdFormVisible(false)}
  228. loading={ddFormLoading}
  229. />
  230. </>
  231. )
  232. }
  233. export default DictDatasModal