ProjectModal.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { connect, useRequest } from 'umi'
  2. import { useEffect } from 'react'
  3. import { Button, Drawer, message } from 'antd'
  4. import { addProject, updateProject } 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. export enum ModalType {
  12. ADD = 0,
  13. UPDATE = 1
  14. }
  15. type ProjectModalProps = ConnectProps & {
  16. visible: boolean
  17. setVisible: (visible: boolean) => void
  18. type: ModalType
  19. defaultFormData?: {
  20. ID: string
  21. name: string
  22. projectTypeID: string
  23. }
  24. pTypeList: API.ProjectTypeList
  25. reloadTable: () => void
  26. schema?: Record<string, any> | null
  27. }
  28. const ProjectModal: React.FC<ProjectModalProps> = ({
  29. visible,
  30. setVisible,
  31. dispatch,
  32. schema,
  33. type,
  34. defaultFormData,
  35. pTypeList,
  36. reloadTable
  37. }) => {
  38. const form = useForm()
  39. useEffect(() => {
  40. if (visible && !schema) {
  41. dispatch({
  42. type: 'schemaBase/querySchema',
  43. payload: {
  44. columnType: BaseMenuEnum.PROJECT
  45. }
  46. })
  47. }
  48. }, [visible])
  49. const { run: tryUpdateProject } = useRequest(updateProject, {
  50. manual: true,
  51. onSuccess: () => {
  52. message.success('更新成功')
  53. }
  54. })
  55. const { run: tryAddProject } = useRequest(addProject, {
  56. manual: true,
  57. onSuccess: () => {
  58. message.success('创建成功')
  59. }
  60. })
  61. const onMount = () => {
  62. form.setValues({ ...defaultFormData })
  63. delay().then(() => {
  64. form.setSchemaByPath('projectTypeID', {
  65. enum: pTypeList.map(item => item.value),
  66. enumNames: pTypeList.map(item => item.label)
  67. })
  68. })
  69. }
  70. const onFinish = async (formData, errors) => {
  71. console.log('formData:', formData, 'errors', errors)
  72. try {
  73. // 执行表单提交
  74. if (type === ModalType.ADD) {
  75. await tryAddProject(formData)
  76. } else {
  77. await tryUpdateProject(formData)
  78. }
  79. setVisible(false)
  80. reloadTable()
  81. } catch (error) {
  82. message.error(error)
  83. }
  84. }
  85. return (
  86. <Drawer
  87. width="50vw"
  88. visible={visible}
  89. onClose={() => {
  90. // ref.current?.resetFields()
  91. setVisible(false)
  92. }}
  93. title={type === ModalType.ADD ? '新增项目' : '编辑项目'}>
  94. {schema && (
  95. <FormRender form={form} schema={JSON.parse(schema)} onFinish={onFinish} onMount={onMount} />
  96. )}
  97. <div>
  98. <Button onClick={form.submit}>提交</Button>
  99. </div>
  100. </Drawer>
  101. )
  102. }
  103. export default connect(
  104. ({ project, schemaBase }: { project: ProjectModelState; schemaBase: SchemaBaseModelState }) => ({
  105. pTypeList: project.projectTypeList.map(item => ({ label: item.name, value: item.ID })),
  106. schema: schemaBase.base[BaseMenuEnum.PROJECT]?.schema
  107. })
  108. )(ProjectModal)