Browse Source

feat: 人员详情刷新逻辑变更

lanjianrong 3 years ago
parent
commit
c97432f7aa

+ 9 - 21
src/pages/Institutions/Company/Detail/components/Staff.tsx

@@ -39,12 +39,11 @@ const Staff: React.FC<ListProps> = ({ schema, dataID, dispatch, accountTypeList
     params: {
       search: null,
       accountType: '1',
-      dataID: dataID
+      dataID
     },
     visible: false,
-    institutionDisable: false,
     currentModalType: ModalType.ADD,
-    defaultFormData: null
+    defaultFormData: {}
   })
   const columns = [
     {
@@ -122,21 +121,13 @@ const Staff: React.FC<ListProps> = ({ schema, dataID, dispatch, accountTypeList
                 ...state,
                 visible: true,
                 currentModalType: ModalType.UPDATE,
-                institutionDisable: true,
                 defaultFormData: {
-                  institution: null,
-                  ID: record.ID,
-                  account: record.account,
-                  name: record.name,
-                  gender: record.gender,
-                  phone: record.phone,
+                  dataID: record.ID,
                   institutionID: record.institution.ID,
-                  accountType: record.accountType,
-                  dataID: record.institutionID
+                  institutionDisable: true
                 }
               })
-            }}
-          >
+            }}>
             编辑
           </div>
         </div>
@@ -177,10 +168,9 @@ const Staff: React.FC<ListProps> = ({ schema, dataID, dispatch, accountTypeList
                   visible: true,
                   currentModalType: ModalType.ADD,
                   institutionDisable: true,
-                  defaultFormData: { ...state.params, structureType: '1', institutionID: dataID }
+                  defaultFormData: { institutionID: dataID, institutionDisable: true }
                 })
-              }}
-            >
+              }}>
               添加人员
             </Button>
           ]
@@ -188,14 +178,12 @@ const Staff: React.FC<ListProps> = ({ schema, dataID, dispatch, accountTypeList
       />
       <AnimateContent
         visible={state.visible}
-        onVisibleChange={visible => setState({ ...state, visible })}
-      >
+        onVisibleChange={visible => setState({ ...state, visible })}>
         <StaffDetail
           visible={state.visible}
           onVisibleChange={(visible: boolean) => setState({ ...state, visible })}
-          institutionDisable={state.institutionDisable}
           type={state.currentModalType}
-          defaultFormData={{ ...state.defaultFormData, dataID }}
+          defaultFormData={{ ...state.defaultFormData }}
           accountTypeList={accountTypeList}
           reload={() => tRef.current?.reload()}
         />

+ 47 - 114
src/pages/Institutions/Staff/components/StaffDetail.tsx

@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from 'react'
+import React, { useEffect } from 'react'
 import { message, Button, Form } from 'antd'
 import { connect, useRequest } from 'umi'
 import consts from '@/utils/consts'
@@ -26,12 +26,10 @@ type StaffModalProps = ConnectProps & {
   onVisibleChange: (visible: boolean) => void
   institutionDisable: boolean
   type: ModalType
-  dataId: string
   defaultFormData?: {
-    ID: string
-    name: string
-    accountType: string
-    dataID: string
+    dataID?: string
+    institutionID?: string
+    institutionDisable?: boolean
   }
   accountTypeList: API.AccountType
   organizationList: API.OrganizationalStructureListItem[]
@@ -41,21 +39,15 @@ type StaffModalProps = ConnectProps & {
 
 const StaffDrawer: React.FC<StaffModalProps> = ({
   visible,
-  institutionDisable,
   onVisibleChange,
   schema,
   dispatch,
   type,
-  dataId,
   defaultFormData,
   accountTypeList,
   reload
 }) => {
   const form = useForm()
-
-  const [state, setState] = useState({
-    accountDetail: {}
-  })
   const { run: tryUpdateAccount } = useRequest(updateAccount, {
     manual: true,
     onSuccess: () => {
@@ -70,13 +62,6 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
     }
   })
 
-  const { run: tryGetAccountDetail } = useRequest((ID: string) => queryAccountDetail(ID), {
-    manual: true,
-    onSuccess: result => {
-      setState({ ...state, accountDetail: result })
-    }
-  })
-
   useEffect(() => {
     if (visible) {
       if (!schema) {
@@ -88,29 +73,7 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
         })
       }
     }
-    // if (defaultFormData?.institutionID && !organizationList?.length) {
-    //   dispatch({
-    //     type: 'institutions/queryOrganizationList',
-    //     payload: {
-    //       dataID: defaultFormData?.institutionID,
-    //       structureType: '1'
-    //     }
-    //   })
-    // }
-    // }, [defaultFormData?.institutionID, visible])
   }, [visible])
-  // useEffect(() => {
-  //   if (organizationList?.length) {
-  //     form.setSchemaByPath('organizationalStructureID', {
-  //       type: 'string',
-  //       widget: 'treeSelect',
-  //       props: {
-  //         treeDefaultExpandAll: true,
-  //         treeData: organizationList
-  //       }
-  //     })
-  //   }
-  // }, [organizationList?.length])
 
   const queryInstitutionOptions = async params => {
     const { code = -1, data = {} } = await queryInstitutionList({
@@ -136,78 +99,65 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
     )
   }
 
-  const onMount = () => {
-    if (!defaultFormData?.dataID) {
-      if (type === ModalType.UPDATE) {
-        form.setValues({ ...defaultFormData })
+  const onMount = async () => {
+    const { dataID, institutionID, institutionDisable = false } = defaultFormData
+    !institutionID &&
+      form.setSchemaByPath('organizationalStructureID', {
+        type: 'string',
+        widget: 'treeSelect',
+        hidden: true
+      })
+    if (institutionID && type !== ModalType.PREVIEW) {
+      const { code = -1, data: list = [] } = await queryOrganizationalStructureList({
+        dataID: institutionID,
+        structureType: '1'
+      })
+      if (code === consts.RET_CODE.SUCCESS) {
+        form.setSchemaByPath('organizationalStructureID', {
+          type: 'string',
+          widget: 'treeSelect',
+          props: {
+            treeDefaultExpandAll: true,
+            treeData: list
+          }
+        })
+      }
+    }
+    if (dataID) {
+      const { code = -1, data = {} } = await queryAccountDetail({ ID: dataID })
+      if (code === consts.RET_CODE.SUCCESS) {
+        const account = { ...data, institution: null, organizationalStructure: null }
+        if (type === ModalType.PREVIEW) {
+          account.institutionID = data.institution.name
+          account.organizationalStructureID = data.organizationalStructure.name
+        }
+        form.setValues({
+          ...account
+        })
       }
     } else {
-      form.setValues(
-        type === ModalType.ADD
-          ? { institutionID: defaultFormData?.institutionID }
-          : { ...defaultFormData }
-      )
+      if (type === ModalType.ADD && institutionID) {
+        form.setValues({ institutionID })
+      }
     }
     delay(80).then(() => {
       form.setSchemaByPath('accountType', {
         type: 'string',
         widget: 'select',
-        // props: {
-        //   options: accountTypeList
-        // }
         enum: accountTypeList.map(item => item.value),
         enumNames: accountTypeList.map(item => item.label)
       })
-      if (institutionDisable) {
-        form.setSchemaByPath('institutionID', {
-          type: 'string',
-          widget: 'site',
-          disabled: true
-        })
-      } else {
-        form.setSchemaByPath('institutionID', {
-          type: 'string',
-          widget: 'site'
-        })
-      }
+      form.setSchemaByPath('institutionID', {
+        type: 'string',
+        widget: 'site',
+        disabled: institutionDisable
+      })
       if (type === ModalType.UPDATE) {
         form.setSchemaByPath('account', {
           disabled: true
         })
       }
     })
-    if (dataId) {
-      queryAccountDetail({ ID: dataId }).then(accountDetail => {
-        if (accountDetail) {
-          if (type === ModalType.PREVIEW) {
-            form.setValues({ ...accountDetail.data })
-          }
-        }
-      })
-    }
-    if (defaultFormData?.institutionID) {
-      queryOrganizationalStructureList({
-        dataID: defaultFormData?.institutionID,
-        structureType: '1'
-      }).then(organizationList => {
-        if (organizationList) {
-          form.setSchemaByPath('organizationalStructureID', {
-            type: 'string',
-            widget: 'treeSelect',
-            props: {
-              treeDefaultExpandAll: true,
-              treeData: organizationList.data
-            }
-          })
-        }
-      })
-    } else {
-      form.setSchemaByPath('organizationalStructureID', {
-        type: 'string',
-        widget: 'treeSelect',
-        hidden: true
-      })
-    }
   }
 
   const watch = {
@@ -231,23 +181,6 @@ const StaffDrawer: React.FC<StaffModalProps> = ({
       }
     }
   }
-  // const handleOnFinish = () => {
-  //   ref.current?.validateFields().then(async values => {
-  //     try {
-  //       // 执行表单提交
-  //       if (type === ModalType.ADD) {
-  //         await tryAddAccount(values)
-  //       } else {
-  //         await tryUpdateAccount(values)
-  //       }
-  //       setVisible(false)
-  //       reloadTable()
-  //       ref.current?.resetFields()
-  //     } catch (error) {
-  //       message.error(error)
-  //     }
-  //   })
-  // }
   const onFinish = async (formData, errors) => {
     if (errors?.length) return
     try {

+ 14 - 40
src/pages/Institutions/Staff/index.tsx

@@ -50,25 +50,10 @@ const CompanyList: React.FC<ListProps> = ({ schema, dispatch, accountTypeList })
       institutionID: null
     },
     visible: false,
-    institutionDisable: false,
-    staffDetail: [],
-    ID: '',
-    dataId: '',
     currentModalType: ModalType.ADD,
-    defaultFormData: null
+    defaultFormData: {}
   })
 
-  // const { run: tryDelProject } = useRequest(delProject, {
-  //   manual: true,
-  //   onSuccess: () => tRef.current?.reload()
-  // })
-  // const { run: tryAccountDetail } = useRequest(() => queryAccountDetail(), {
-  //   manual: true,
-  //   onSuccess: result => {
-  //     setState({ ...state, staffDetail: result })
-  //   }
-  // })
-  // useEffect(() => {}, [])
   const columns: ProColumnType<API.AccountListItem>[] = [
     {
       dataIndex: 'account',
@@ -82,18 +67,11 @@ const CompanyList: React.FC<ListProps> = ({ schema, dispatch, accountTypeList })
               ...state,
               visible: true,
               currentModalType: ModalType.PREVIEW,
-              dataId: record.ID
-              // defaultFormData: {
-              //   // ...record,
-              //   institution: null,
-              //   ID: record.ID,
-              //   account: record.account,
-              //   name: record.name,
-              //   gender: record.gender,
-              //   phone: record.phone,
-              //   institutionID: record.institution.ID,
-              //   accountType: record.accountType
-              // }
+              defaultFormData: {
+                dataID: record.ID,
+                institutionID: record.institution.ID,
+                institutionDisable: true
+              }
             })
           }}>
           {name}
@@ -170,15 +148,9 @@ const CompanyList: React.FC<ListProps> = ({ schema, dispatch, accountTypeList })
                 visible: true,
                 currentModalType: ModalType.UPDATE,
                 defaultFormData: {
-                  // ...record,
-                  institution: record.institutionID,
-                  ID: record.ID,
-                  account: record.account,
-                  name: record.name,
-                  gender: record.gender,
-                  phone: record.phone,
-                  institutionID: record.institution.ID,
-                  accountType: record.accountType
+                  dataID: record.ID,
+                  institutionID: record.institutionID,
+                  institutionDisable: true
                 }
               })
             }}>
@@ -230,7 +202,11 @@ const CompanyList: React.FC<ListProps> = ({ schema, dispatch, accountTypeList })
                 setState({
                   ...state,
                   visible: true,
-                  currentModalType: ModalType.ADD
+                  currentModalType: ModalType.ADD,
+                  defaultFormData: {
+                    ...state.defaultFormData,
+                    institutionDisable: false
+                  }
                 })
               }>
               新建账号
@@ -246,8 +222,6 @@ const CompanyList: React.FC<ListProps> = ({ schema, dispatch, accountTypeList })
           type={state.currentModalType}
           defaultFormData={state.defaultFormData}
           visible={state.visible}
-          dataId={state.dataId}
-          institutionDisable={state.institutionDisable}
           reload={() => tRef.current?.reload()}
           onVisibleChange={(visible: boolean) => setState({ ...state, visible })}
         />