|
@@ -1,9 +1,7 @@
|
|
import {addRole, editRole, getRoleInfo} from '@apis';
|
|
import {addRole, editRole, getRoleInfo} from '@apis';
|
|
import {yupResolver} from '@hookform/resolvers/yup';
|
|
import {yupResolver} from '@hookform/resolvers/yup';
|
|
-import {useQueryDataInfo} from '@hooks';
|
|
|
|
|
|
+import {usePutData, useQueryDataInfo} from '@hooks';
|
|
import {AddRoleParams} from '@models';
|
|
import {AddRoleParams} from '@models';
|
|
-import {useMutation} from '@tanstack/react-query';
|
|
|
|
-import {message} from 'antd';
|
|
|
|
import {useEffect} from 'react';
|
|
import {useEffect} from 'react';
|
|
import {useForm, useFormContext} from 'react-hook-form';
|
|
import {useForm, useFormContext} from 'react-hook-form';
|
|
import {object, string} from 'yup';
|
|
import {object, string} from 'yup';
|
|
@@ -17,34 +15,6 @@ const validate = object({
|
|
roleName: string().required('请输入角色名称'),
|
|
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({
|
|
export function useFormState({
|
|
visible,
|
|
visible,
|
|
onFetch,
|
|
onFetch,
|
|
@@ -72,9 +42,12 @@ export function useFormState({
|
|
[clearErrors, visible],
|
|
[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 onSubmit = handleSubmit(function ({roleName, roleRemarks}) {
|
|
const params: AddRoleParams = {remarks: roleRemarks, roleName};
|
|
const params: AddRoleParams = {remarks: roleRemarks, roleName};
|