lanjianrong 3 年之前
父節點
當前提交
29026f6c6b

+ 6 - 8
src/pages/Institutions/Company/Add/index.tsx

@@ -11,14 +11,12 @@ type CompanyAddProps = ConnectProps & {
 }
 const CompanyAdd: React.FC<CompanyAddProps> = ({ dispatch, schema }) => {
   useEffect(() => {
-    if (!schema) {
-      dispatch({
-        type: 'schemaBase/querySchema',
-        payload: {
-          columnType: BaseMenuEnum.COMPANY
-        }
-      })
-    }
+    dispatch({
+      type: 'schemaBase/querySchema',
+      payload: {
+        columnType: BaseMenuEnum.COMPANY
+      }
+    })
   }, [])
   const form = useForm()
   return (

+ 41 - 16
src/pages/Schema/Base/detail.tsx

@@ -1,36 +1,60 @@
 import Generator from 'fr-generator'
-import React, { useRef } from 'react'
+import React, { useRef, useState, useEffect } from 'react'
 import { PageContainer } from '@ant-design/pro-layout'
 import { connect, useRequest } from 'umi'
+import type { ConnectProps } from 'umi'
 import type { RouteComponentProps } from 'react-router'
 import type { SchemaBaseModelState } from './model'
 import { updateSchema } from '@/services/api/schema'
 import { message } from 'antd'
+import { menuOptions } from '.'
 
-type DetailProps = RouteComponentProps
+type DetailProps = RouteComponentProps & ConnectProps
 
-const Detail: React.FC<DetailProps> = ({ base, location }) => {
-  const genRef = useRef(null)
+const Detail: React.FC<DetailProps> = ({ dispatch, base, location }) => {
   const {
-    query: { columnType },
-    state: { ID }
+    query: { columnType, ID }
   } = location
-  console.log(columnType)
+
+  const genRef = useRef(null)
+
+  // const [schema, setSchema] = useState(
+  //   () => (base?.[columnType] && JSON.parse(base?.[columnType]?.schema)) || {}
+  // )
+  // console.log(base?.[columnType]?.schema)
 
   const { run: tryUpdateSchema } = useRequest(updateSchema, {
     manual: true,
-    onSuccess: () => {
+    onSuccess: result => {
       message.success('更新成功')
+      // console.log(genRef.current?.getValue())
+      dispatch({
+        type: 'schemaBase/changeSchemaByColumnType',
+        payload: {
+          ID,
+          columnType,
+          schema: result.schema
+        }
+      })
     }
   })
 
-  const schema = base?.[columnType]?.schema
+  useEffect(() => {
+    if (columnType && !base[columnType]) {
+      dispatch({
+        type: 'schemaBase/querySchema',
+        payload: {
+          columnType
+        }
+      })
+    }
+  }, [columnType])
   return (
-    <PageContainer title={false}>
-      <div className="bg-white shadow-card p-4" style={{ height: 'calc(100vh - 146px)' }}>
+    <PageContainer title={menuOptions[columnType]?.label + '数据模型'}>
+      <div className="bg-white shadow-card p-4" style={{ height: 'calc(100vh - 196px)' }}>
         <Generator
           ref={genRef}
-          defaultValue={(schema && JSON.parse(schema)) || {}}
+          defaultValue={(base?.[columnType] && JSON.parse(base?.[columnType]?.schema)) || {}}
           controlButtons={[true, true]}
           extraButtons={[
             true,
@@ -40,12 +64,13 @@ const Detail: React.FC<DetailProps> = ({ base, location }) => {
             {
               text: '提交',
               type: 'primary',
-              onClick: () => {
-                // console.log(schema)
-                tryUpdateSchema({
+              onClick: async () => {
+                const newSchema = JSON.stringify(genRef.current?.getValue())
+
+                await tryUpdateSchema({
                   ID,
                   columnType,
-                  schema: JSON.stringify(genRef.current?.getValue())
+                  schema: newSchema
                 })
               }
             }

+ 10 - 9
src/pages/Schema/Base/index.tsx

@@ -12,6 +12,11 @@ export enum BaseMenuEnum {
   STAFF = '3'
 }
 
+export const menuOptions = [
+  { label: '项目信息', value: BaseMenuEnum.PROJECT },
+  { label: '企事业单位信息', value: BaseMenuEnum.COMPANY },
+  { label: '人员信息', value: BaseMenuEnum.STAFF }
+]
 type BaseProps = ConnectProps & {
   base: SchemaBaseModelState
 }
@@ -34,17 +39,11 @@ const Index: React.FC<BaseProps> = ({ base, dispatch }) => {
       })
     }
   }, [state.activeKey])
-  const menuOptions = [
-    { label: '项目信息', value: BaseMenuEnum.PROJECT },
-    { label: '企事业单位信息', value: BaseMenuEnum.COMPANY },
-    { label: '人员信息', value: BaseMenuEnum.STAFF }
-  ]
 
   const gotoDetail = (columnType, ID) => {
     history.push({
       pathname: '/work-setting/schema/detail',
-      query: { columnType },
-      state: { ID }
+      query: { columnType, ID }
     })
   }
 
@@ -58,10 +57,12 @@ const Index: React.FC<BaseProps> = ({ base, dispatch }) => {
           onChange={key => setState({ ...state, activeKey: key })}
         />
         <div className="w-max-3/4 ml-8 bg-white p-4 shadow-card relative ">
-          <div className="text-right">
+          <div className="text-right mb-4">
             <Button onClick={() => gotoDetail(state.activeKey, currentSchema?.ID)}>编辑</Button>
           </div>
-          <div>{state.schema && <FormRender form={form} schema={state.schema} />}</div>
+          <div>
+            {currentSchema && <FormRender form={form} schema={JSON.parse(currentSchema?.schema)} />}
+          </div>
         </div>
       </div>
     </PageContainer>

+ 6 - 0
src/pages/Schema/Base/model.ts

@@ -44,6 +44,12 @@ const SchemaBaseModel: SchemaBaseType = {
           }
         })
       }
+    },
+    *changeSchemaByColumnType({ payload }, { put }) {
+      yield put({
+        type: 'save',
+        payload
+      })
     }
   },
   reducers: {