Detail.tsx 5.0 KB

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