index.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import React, { useEffect, useState } from 'react'
  2. import { Row, Col, Table, Button, Divider, Switch, Modal, Input, Popconfirm, message } from 'antd'
  3. import BaseTmp from '@/components/BaseTmp'
  4. import api from '@/services/api/index'
  5. import { PlusOutlined } from '@ant-design/icons'
  6. import { descText } from '@/common/MessageManage'
  7. const {
  8. getQuestionBank,
  9. addQuestionBank,
  10. delQuestionBank,
  11. putQuestionBank
  12. } = api.question
  13. const QuestionBank: React.FC = () => {
  14. const [ dataSource, setDataSource ] = useState([])
  15. const [ isModalVisible, setIsModalVisible ] = useState<boolean>(false)
  16. /**modal loading */
  17. const [ confirmLoading, setConfirmLoading ] = useState<boolean>(false)
  18. /**table loading */
  19. const [ loading, setLoading ] = useState<boolean>(false)
  20. /**编辑还是新增 */
  21. const [ type, setType ] = useState<'edit' | 'add'>('add')
  22. const [ currentDataSource, setCurrentDataSource ] = useState<{ id: string, label: string }>({id: '', label: ''})
  23. useEffect( () => {
  24. _getQuestionBank()
  25. }, [])
  26. const columns = [
  27. {
  28. title: '学段名',
  29. dataIndex: 'label',
  30. key: 'label',
  31. },
  32. {
  33. title: '教材数',
  34. dataIndex: 'textBookNum',
  35. key: 'textBookNum',
  36. },
  37. {
  38. title: '卡片数',
  39. dataIndex: 'cardNum',
  40. key: 'cardNum',
  41. },
  42. {
  43. title: '状态',
  44. dataIndex: 'state',
  45. key: 'state',
  46. render: () => <Switch checkedChildren="开启" unCheckedChildren="关闭" defaultChecked />
  47. },
  48. {
  49. title: '操作',
  50. dataIndex: 'action',
  51. key: 'action',
  52. render: (action: any, record: any) => (
  53. <>
  54. <a onClick={() => openModel('edit', record)} >编辑</a>
  55. <Divider type="vertical" />
  56. <Popconfirm
  57. title="确定要删除这个学段吗?"
  58. onConfirm={() => onConfirm(action, record)}
  59. okText="是"
  60. cancelText="我点错了"
  61. >
  62. <a >删除</a>
  63. </Popconfirm>
  64. </>
  65. )
  66. }
  67. ]
  68. const modelTitle = type === 'add' ? '新增学段' : '编辑学段'
  69. const handleOk = () => {
  70. setConfirmLoading(true)
  71. if (type === 'add') {
  72. _addQuestionBank()
  73. } else {
  74. _putQuestionBank()
  75. }
  76. }
  77. const onInput = (e: InputEvent) => {
  78. setCurrentDataSource({
  79. ...currentDataSource,
  80. label: e.target.value
  81. })
  82. }
  83. /**删除学段 */
  84. const onConfirm = (action: any, records: any) => {
  85. _delQuestionBank(records.id)
  86. }
  87. /**修改题库 */
  88. const _putQuestionBank = async () => {
  89. const { status, result } = await putQuestionBank(currentDataSource)
  90. setIsModalVisible(false)
  91. setConfirmLoading(false)
  92. if (status === 200) {
  93. console.log(result);
  94. setCurrentDataSource({id: '', label: ''})
  95. _getQuestionBank()
  96. }
  97. }
  98. /**删除题库 */
  99. const _delQuestionBank = async (id: string | number) => {
  100. const { status, result } = await delQuestionBank(id)
  101. if ( status === 200 ) {
  102. console.log(result);
  103. message.success(descText.delTextSuccess)
  104. _getQuestionBank()
  105. }
  106. }
  107. /**增加题库 */
  108. const _addQuestionBank = async () => {
  109. setCurrentDataSource({id: '', label: ''})
  110. const { status, result} = await addQuestionBank({
  111. bankName: currentDataSource.label
  112. })
  113. setConfirmLoading(false)
  114. if (status === 200) {
  115. console.log(result, 'result');
  116. message.success(descText.addTextSuccess)
  117. setIsModalVisible(false)
  118. _getQuestionBank()
  119. }
  120. }
  121. /**打开弹窗 */
  122. const openModel = (type: 'edit' | 'add', record?: any) => {
  123. setType(type)
  124. setIsModalVisible(true)
  125. if (type === 'edit' ) {
  126. setCurrentDataSource(record)
  127. }
  128. }
  129. /**关闭弹窗 */
  130. const closeModel = () => {
  131. setIsModalVisible(false)
  132. }
  133. /** 查询学段 */
  134. const _getQuestionBank = async () => {
  135. setLoading(true)
  136. const { status, result } = await getQuestionBank()
  137. setLoading(false)
  138. if ( status === 200 ) {
  139. console.log("result:", result);
  140. setDataSource(result)
  141. }
  142. }
  143. return (
  144. <BaseTmp>
  145. <Row>
  146. <Col span={24} style={{ display: 'flex', justifyContent: 'flex-end' }} >
  147. <Button type="primary" icon={<PlusOutlined />} onClick={() => openModel('add')} >创建学段</Button>
  148. </Col>
  149. <Col span={24} style={{marginTop: '20px'}} >
  150. <Table
  151. dataSource={dataSource}
  152. columns={columns}
  153. loading={loading}
  154. />
  155. </Col>
  156. </Row>
  157. <Modal
  158. title={modelTitle}
  159. confirmLoading={confirmLoading}
  160. visible={isModalVisible}
  161. onOk={handleOk}
  162. onCancel={closeModel}
  163. >
  164. <Input placeholder='请输入学段名' value={currentDataSource.label} onInput={onInput} />
  165. </Modal>
  166. </BaseTmp>
  167. )
  168. }
  169. export default QuestionBank