Detail.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { connect, useRequest } from 'umi'
  2. import { useEffect, useRef, useState } from 'react'
  3. import { message, Tabs } from 'antd'
  4. import { getApprovalList, setApproval } from '@/services/api/project'
  5. import { queryAcountList } from '@/services/api/institution'
  6. import { delay } from '@/utils/util'
  7. import FormRender, { useForm } from 'form-render'
  8. import { BaseMenuEnum } from '@/pages/Schema/Base'
  9. import type { SchemaBaseModelState } from '@/pages/Schema/Base/model'
  10. import type { ConnectProps } from 'umi'
  11. import type { ProjectModelState } from '../../model'
  12. import ProForm, { ProFormSelect } from '@ant-design/pro-form'
  13. export enum ModalType {
  14. ADD = 0,
  15. UPDATE = 1
  16. }
  17. type ProjectModalProps = ConnectProps & {
  18. visible: boolean
  19. onVisibleChange: (visible: boolean) => void
  20. readOnly: boolean
  21. type: ModalType
  22. defaultFormData?: {
  23. ID: string
  24. name: string
  25. }
  26. reloadTable: () => void
  27. schema?: Record<string, any> | null
  28. }
  29. const DetailModal: React.FC<ProjectModalProps> = ({
  30. visible,
  31. onVisibleChange,
  32. readOnly,
  33. dispatch,
  34. schema,
  35. defaultFormData,
  36. pTypeList,
  37. reloadTable
  38. }) => {
  39. // console.log(defaultFormData)
  40. const form = useForm()
  41. const ref = useRef<FormInstance>(null)
  42. const { TabPane } = Tabs
  43. const [state, setState] = useState({
  44. acountList: [],
  45. approvalList: [],
  46. activeKey: ''
  47. })
  48. const { run: tryAcountList } = useRequest(() => queryAcountList(), {
  49. manual: true,
  50. onSuccess: result => {
  51. setState({ ...state, acountList: result.items })
  52. }
  53. })
  54. const { run: tryApprovalList } = useRequest(() => getApprovalList(), {
  55. manual: true,
  56. onSuccess: result => {
  57. setState({ ...state, approvalList: result.items })
  58. }
  59. })
  60. const { run: trySetApproval } = useRequest(setApproval, {
  61. manual: true,
  62. onSuccess: () => {
  63. message.success('提交成功')
  64. }
  65. })
  66. useEffect(() => {
  67. if (visible && !schema) {
  68. dispatch({
  69. type: 'schemaBase/querySchema',
  70. payload: {
  71. columnType: BaseMenuEnum.PROJECT
  72. }
  73. })
  74. }
  75. if (state.activeKey === '2') {
  76. const TabFormData = {
  77. accountID: defaultFormData.approval.name,
  78. approvalID: defaultFormData.reportAccount.name
  79. }
  80. ref.current?.setFieldsValue({ ...TabFormData })
  81. }
  82. }, [visible, state.activeKey])
  83. const onMount = () => {
  84. const currentFormData = { ...defaultFormData }
  85. const keys = Object.keys(currentFormData)
  86. keys.forEach(key => {
  87. if (currentFormData[key] instanceof Object) {
  88. const targetMap = currentFormData[key]
  89. delete currentFormData[key]
  90. currentFormData[`${key}ID`] = targetMap.ID
  91. }
  92. })
  93. // console.log(currentFormData)
  94. form.setValues({ ...currentFormData })
  95. // form.setValues({ ...defaultFormData })
  96. delay(80).then(() => {
  97. form.setSchemaByPath('projectTypeID', {
  98. enum: pTypeList.map(item => item.value),
  99. enumNames: pTypeList.map(item => item.label)
  100. })
  101. })
  102. }
  103. const onChange = key => {
  104. setState({ ...state, activeKey: key })
  105. if (key === '2') {
  106. if (!state.acountList?.length) {
  107. tryAcountList()
  108. }
  109. if (!state.approvalList?.length) {
  110. tryApprovalList()
  111. }
  112. }
  113. }
  114. const onFinish = async (formData, value) => {
  115. await trySetApproval({ ...formData, ...value, ID: defaultFormData.ID })
  116. onVisibleChange(false)
  117. reloadTable()
  118. }
  119. return (
  120. <Tabs onChange={onChange}>
  121. <TabPane tab="项目信息" key="1">
  122. {schema && <FormRender form={form} schema={schema} onMount={onMount} readOnly={readOnly} />}
  123. </TabPane>
  124. <TabPane tab="审批流程" key="2">
  125. <ProForm
  126. formRef={ref}
  127. submitter={{ resetButtonProps: { style: { display: 'none' } } }}
  128. onFinish={onFinish}>
  129. <ProFormSelect
  130. name="accountID"
  131. label={'上报人'}
  132. placeholder="请选择上报人"
  133. options={state.acountList.map(item => ({
  134. label: item.name,
  135. value: item.ID
  136. }))}
  137. />
  138. <ProFormSelect
  139. name="approvalID"
  140. label={'审批流程'}
  141. placeholder="请选择审批流程"
  142. options={state.approvalList.map(item => ({
  143. label: item.name,
  144. value: item.ID
  145. }))}
  146. />
  147. </ProForm>
  148. </TabPane>
  149. </Tabs>
  150. // <Drawer
  151. // width="50vw"
  152. // visible={visibles}
  153. // onClose={() => {
  154. // // ref.current?.resetFields()
  155. // setVisible(false)
  156. // }}
  157. // title={defaultFormData?.name}>
  158. // <Tabs>
  159. // <TabPane tab="项目信息" key="1">
  160. // {schema && (
  161. // <FormRender form={form} schema={schema} onMount={onMount} readOnly={readOnly} />
  162. // )}
  163. // </TabPane>
  164. // <TabPane tab="审批流程" key="2">
  165. // <ProForm
  166. // formRef={ref}
  167. // submitter={{ resetButtonProps: { style: { display: 'none' } } }}
  168. // onFinish={onFinish}>
  169. // <ProFormSelect
  170. // name="accountID"
  171. // label={'上报人'}
  172. // placeholder="请选择上报人"
  173. // options={state.acountList.map(item => ({
  174. // label: item.name,
  175. // value: item.ID
  176. // }))}
  177. // />
  178. // <ProFormSelect
  179. // name="approvalID"
  180. // label={'审批流程'}
  181. // placeholder="请选择审批流程"
  182. // options={state.approvalList.map(item => ({
  183. // label: item.name,
  184. // value: item.ID
  185. // }))}
  186. // />
  187. // </ProForm>
  188. // </TabPane>
  189. // </Tabs>
  190. // </Drawer>
  191. )
  192. }
  193. export default connect(
  194. ({ project, schemaBase }: { project: ProjectModelState; schemaBase: SchemaBaseModelState }) => ({
  195. pTypeList: project.projectTypeList.map(item => ({ label: item.name, value: item.ID })),
  196. schema: schemaBase.base[BaseMenuEnum.PROJECT]?.schema
  197. })
  198. )(DetailModal)