Kaynağa Gözat

chore: 调整公共新增修改hook

xyh 2 yıl önce
ebeveyn
işleme
95349c2b91

+ 7 - 34
packages/app/src/pages/pda-menu/table/modal/hooks.ts

@@ -1,8 +1,6 @@
 import {addPDAMenu, editMenu, getMenuInfo} from '@apis';
 import {yupResolver} from '@hookform/resolvers/yup';
-import {useQueryDataInfo} from '@hooks';
-import {useMutation} from '@tanstack/react-query';
-import {message} from 'antd';
+import {usePutData, useQueryDataInfo} from '@hooks';
 import {useEffect} from 'react';
 import {useForm, useFormContext} from 'react-hook-form';
 import {object, string} from 'yup';
@@ -19,34 +17,6 @@ const validate = object({
   menuOrderBy: string().required('请输入菜单排序'),
 });
 
-function useAdd(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(addPDAMenu, {
-    onSuccess(data) {
-      if (data.msg === '200') {
-        onClose();
-        onFetch();
-        message.success('新增成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
-function useEdit(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(editMenu, {
-    onSuccess(data) {
-      if (data.msg === '200') {
-        onClose();
-        onFetch();
-        message.success('修改成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
 export function useFormState({
   onClose,
   id,
@@ -69,9 +39,12 @@ export function useFormState({
     resolver: yupResolver(validate),
   });
   const {handleSubmit, clearErrors} = formInstance;
-  const [addIsLoading, addMutate] = useAdd(onClose, onFetch);
-  const [editIsLoaindg, editMutate] = useEdit(onClose, onFetch);
-  const isLoading = addIsLoading || editIsLoaindg;
+  const [isLoading, {addMutate, editMutate}] = usePutData({
+    addFn: addPDAMenu,
+    editFn: editMenu,
+    onClose,
+    onFetch,
+  });
 
   useEffect(
     function () {

+ 7 - 34
packages/app/src/pages/role/table/modal/hooks.ts

@@ -1,9 +1,7 @@
 import {addRole, editRole, getRoleInfo} from '@apis';
 import {yupResolver} from '@hookform/resolvers/yup';
-import {useQueryDataInfo} from '@hooks';
+import {usePutData, useQueryDataInfo} from '@hooks';
 import {AddRoleParams} from '@models';
-import {useMutation} from '@tanstack/react-query';
-import {message} from 'antd';
 import {useEffect} from 'react';
 import {useForm, useFormContext} from 'react-hook-form';
 import {object, string} from 'yup';
@@ -17,34 +15,6 @@ const validate = object({
   roleName: string().required('请输入角色名称'),
 });
 
-function useAdd(onFetch: () => void, onClose: () => void) {
-  const {isLoading, mutate} = useMutation(addRole, {
-    onSuccess({msg}) {
-      if (msg === '200') {
-        onFetch();
-        onClose();
-        message.success('新增成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
-function useEdit(onFetch: () => void, onClose: () => void) {
-  const {isLoading, mutate} = useMutation(editRole, {
-    onSuccess({msg}) {
-      if (msg === '200') {
-        onFetch();
-        onClose();
-        message.success('修改成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
 export function useFormState({
   visible,
   onFetch,
@@ -72,9 +42,12 @@ export function useFormState({
     [clearErrors, visible],
   );
 
-  const [addIsLoading, addMutate] = useAdd(onFetch, onClose);
-  const [editIsLoading, editMutate] = useEdit(onFetch, onClose);
-  const isLoading = addIsLoading || editIsLoading;
+  const [isLoading, {addMutate, editMutate}] = usePutData({
+    addFn: addRole,
+    editFn: editRole,
+    onClose,
+    onFetch,
+  });
 
   const onSubmit = handleSubmit(function ({roleName, roleRemarks}) {
     const params: AddRoleParams = {remarks: roleRemarks, roleName};

+ 7 - 34
packages/app/src/pages/storage/table/modal/hooks.ts

@@ -1,9 +1,7 @@
 import {addStorage, editStorage, getStorageInfo} from '@apis';
 import {yupResolver} from '@hookform/resolvers/yup';
-import {useQueryDataInfo} from '@hooks';
+import {usePutData, useQueryDataInfo} from '@hooks';
 import {AddStorageParams} from '@models';
-import {useMutation} from '@tanstack/react-query';
-import {message} from 'antd';
 import {useEffect} from 'react';
 import {useForm, useFormContext} from 'react-hook-form';
 import {object, string} from 'yup';
@@ -23,34 +21,6 @@ const validate = object({
   storageWarehouse: string().required('请选择所在仓库'),
 });
 
-function useAdd(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(addStorage, {
-    onSuccess({msg}) {
-      if (msg === '200') {
-        message.success('新增成功');
-        onClose();
-        onFetch();
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
-function useEdit(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(editStorage, {
-    onSuccess({msg}) {
-      if (msg === '200') {
-        message.success('修改成功');
-        onClose();
-        onFetch();
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
 export function useFormState({
   onClose,
   onFetch,
@@ -82,9 +52,12 @@ export function useFormState({
     [clearErrors, visible],
   );
 
-  const [isAddLoading, addMutate] = useAdd(onClose, onFetch);
-  const [isEditLoading, editMutate] = useEdit(onClose, onFetch);
-  const isLoading = isAddLoading || isEditLoading;
+  const [isLoading, {addMutate, editMutate}] = usePutData({
+    addFn: addStorage,
+    editFn: editStorage,
+    onClose,
+    onFetch,
+  });
 
   const onSubmit = handleSubmit(function ({
     storageLocationName,

+ 7 - 34
packages/app/src/pages/user/table/modal/hooks.ts

@@ -1,9 +1,7 @@
 import {addUser, editUser, getUserInfo} from '@apis';
 import {yupResolver} from '@hookform/resolvers/yup';
-import {useQueryDataInfo} from '@hooks';
+import {usePutData, useQueryDataInfo} from '@hooks';
 import {EditUserParams} from '@models';
-import {useMutation} from '@tanstack/react-query';
-import {message} from 'antd';
 import {useEffect} from 'react';
 import {useForm, useFormContext} from 'react-hook-form';
 import {object, string} from 'yup';
@@ -27,34 +25,6 @@ const validate = object({
   userRealName: string().required('请输入真实姓名'),
 });
 
-function useAdd(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(addUser, {
-    onSuccess(data) {
-      if (data.msg === '200') {
-        onClose();
-        onFetch();
-        message.success('新增成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
-function useEdit(onClose: () => void, onFetch: () => void) {
-  const {isLoading, mutate} = useMutation(editUser, {
-    onSuccess(data) {
-      if (data.msg === '200') {
-        onClose();
-        onFetch();
-        message.success('修改成功');
-      }
-    },
-  });
-
-  return [isLoading, mutate] as const;
-}
-
 export function useFormState({
   onClose,
   onFetch,
@@ -88,9 +58,12 @@ export function useFormState({
     [clearErrors, visible],
   );
 
-  const [isAddLoading, addMutate] = useAdd(onClose, onFetch);
-  const [isEditLoading, editMutate] = useEdit(onClose, onFetch);
-  const isLoading = isAddLoading || isEditLoading;
+  const [isLoading, {addMutate, editMutate}] = usePutData({
+    addFn: addUser,
+    editFn: editUser,
+    onFetch,
+    onClose,
+  });
 
   const onSubmit = handleSubmit(function ({
     userName,