|
@@ -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 () {
|