StaffDrawer.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, { useEffect } from 'react'
  2. import { message, Button, Drawer } from 'antd'
  3. import { connect, useRequest } from 'umi'
  4. // import consts from '@/utils/consts'
  5. import { addAccount, updateAccount } from '@/services/api/institution'
  6. // import DebounceSelect from './DebounceSelect'
  7. import { delay } from '@/utils/util'
  8. import FormRender, { useForm } from 'form-render'
  9. import { BaseMenuEnum } from '@/pages/Schema/Base'
  10. import type { SchemaBaseModelState } from '@/pages/Schema/Base/model'
  11. import type { ConnectProps } from 'umi'
  12. import type { InstitutionsModelState } from '../../model'
  13. export enum ModalType {
  14. ADD = 0,
  15. UPDATE = 1
  16. }
  17. type StaffModalProps = ConnectProps & {
  18. visible: boolean
  19. setVisible: (visible: boolean) => void
  20. type: ModalType
  21. defaultFormData?: {
  22. ID: string
  23. name: string
  24. accountType: string
  25. dataID: string
  26. }
  27. accountTypeList: API.AccountType
  28. organizationList: API.OrganizationalStructureListItem
  29. reload: () => void
  30. schema?: Record<string, any> | null
  31. }
  32. const StaffDrawer: React.FC<StaffModalProps> = ({
  33. visible,
  34. setVisible,
  35. schema,
  36. dispatch,
  37. type,
  38. defaultFormData,
  39. accountTypeList,
  40. organizationList,
  41. reload
  42. }) => {
  43. console.log(organizationList)
  44. const form = useForm()
  45. useEffect(() => {
  46. if (visible) {
  47. if (!schema) {
  48. dispatch({
  49. type: 'schemaBase/querySchema',
  50. payload: {
  51. columnType: BaseMenuEnum.STAFF
  52. }
  53. })
  54. }
  55. if (!organizationList?.length) {
  56. dispatch({
  57. type: 'institutions/queryOrganizationList',
  58. payload: {
  59. dataID: defaultFormData?.dataID,
  60. structureType: '1'
  61. }
  62. })
  63. }
  64. }
  65. }, [visible])
  66. // useEffect(() => {
  67. // defaultFormData && ref.current?.setFieldsValue({ ...defaultFormData })
  68. // }, [defaultFormData])
  69. const { run: tryUpdateAccount } = useRequest(updateAccount, {
  70. manual: true,
  71. onSuccess: () => {
  72. message.success('更新成功')
  73. }
  74. })
  75. const { run: tryAddAccount } = useRequest(addAccount, {
  76. manual: true,
  77. onSuccess: () => {
  78. message.success('创建成功')
  79. }
  80. })
  81. const onMount = () => {
  82. // console.log(defaultFormData)
  83. form.setValues({ ...defaultFormData })
  84. delay(80).then(() => {
  85. form.setSchemaByPath('accountType', {
  86. enum: accountTypeList.map(item => item.value),
  87. enumNames: accountTypeList.map(item => item.label)
  88. })
  89. form.setSchemaByPath('institution', {
  90. enum: organizationList.map(item => item.value),
  91. enumNames: organizationList.map(item => item.label)
  92. })
  93. })
  94. }
  95. // const handleOnFinish = () => {
  96. // ref.current?.validateFields().then(async values => {
  97. // try {
  98. // // 执行表单提交
  99. // if (type === ModalType.ADD) {
  100. // await tryAddAccount(values)
  101. // } else {
  102. // await tryUpdateAccount(values)
  103. // }
  104. // setVisible(false)
  105. // reloadTable()
  106. // ref.current?.resetFields()
  107. // } catch (error) {
  108. // message.error(error)
  109. // }
  110. // })
  111. // }
  112. const onFinish = async (formData, errors) => {
  113. console.log('formData:', formData, 'errors', errors)
  114. try {
  115. // 执行表单提交
  116. if (type === ModalType.ADD) {
  117. await tryAddAccount(formData)
  118. } else {
  119. await tryUpdateAccount(formData)
  120. }
  121. setVisible(false)
  122. reload()
  123. } catch (error) {
  124. message.error(error)
  125. }
  126. }
  127. // const queryInstitutionOptions = async search => {
  128. // const { code = -1, data = {} } = await queryInstitutionList({
  129. // search,
  130. // current: 1,
  131. // pageSize: 100
  132. // })
  133. // if (code === consts.RET_CODE.SUCCESS) {
  134. // return data.items.map(item => ({ label: item.name, value: item.ID }))
  135. // }
  136. // return []
  137. // }
  138. // const { institution } = defaultFormData || {}
  139. return (
  140. <Drawer
  141. width="50vw"
  142. visible={visible}
  143. onClose={() => {
  144. // ref.current?.resetFields()
  145. setVisible(false)
  146. }}
  147. title={type === ModalType.ADD ? '新增账号' : '编辑账号'}>
  148. {schema && (
  149. <FormRender form={form} schema={JSON.parse(schema)} onFinish={onFinish} onMount={onMount} />
  150. )}
  151. <div>
  152. <Button onClick={form.submit}>提交</Button>
  153. </div>
  154. </Drawer>
  155. )
  156. }
  157. export default connect(
  158. ({
  159. institutions,
  160. schemaBase
  161. }: {
  162. institutions: InstitutionsModelState
  163. schemaBase: SchemaBaseModelState
  164. }) => ({
  165. accountTypeList: institutions.accountType.map(item => ({
  166. label: item.name,
  167. value: item.value
  168. })),
  169. organizationList: institutions.organizationType.map(item => ({
  170. label: item.name,
  171. value: item.dataID
  172. })),
  173. schema: schemaBase.base[BaseMenuEnum.STAFF]?.schema
  174. })
  175. )(StaffDrawer)