29 lines
682 B
TypeScript
29 lines
682 B
TypeScript
|
|
import { useMutation } from "@tanstack/react-query";
|
||
|
|
import { modifyFile, createFile } from "@/apis/file";
|
||
|
|
import { toast } from "sonner";
|
||
|
|
|
||
|
|
export const useSaveFile = (mutationOptions?: any) => {
|
||
|
|
const { mutateAsync: saveMutateAsync, isPending: saveIsPending } =
|
||
|
|
useMutation({
|
||
|
|
mutationFn: (values: any) => {
|
||
|
|
if (values.id) {
|
||
|
|
return modifyFile(values);
|
||
|
|
}
|
||
|
|
return createFile(values);
|
||
|
|
},
|
||
|
|
...(mutationOptions || {}),
|
||
|
|
});
|
||
|
|
|
||
|
|
const save = (values: any) => {
|
||
|
|
toast.promise(saveMutateAsync(values), {
|
||
|
|
loading: "提交中...",
|
||
|
|
success: "操作成功",
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
save,
|
||
|
|
saveIsPending,
|
||
|
|
};
|
||
|
|
};
|