mirror of
https://github.com/ershisan99/todolist_next.git
synced 2026-01-25 12:35:28 +00:00
initial commit, login page
This commit is contained in:
35
src/services/hooks.ts
Normal file
35
src/services/hooks.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { PostLoginArgs } from "./index";
|
||||
import { deleteMe, getMe, postLogin } from "./index";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
export const useLoginMutation = () => {
|
||||
return useMutation({
|
||||
mutationFn: (args: PostLoginArgs) => postLogin(args),
|
||||
});
|
||||
};
|
||||
|
||||
export const useMeQuery = () => {
|
||||
return useQuery({
|
||||
queryFn: () => getMe().then((res) => res.data),
|
||||
queryKey: ["me"],
|
||||
refetchInterval: 1000 * 60 * 60,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
refetchOnMount: false,
|
||||
refetchIntervalInBackground: false,
|
||||
retry: false,
|
||||
});
|
||||
};
|
||||
|
||||
export const useLogoutMutation = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const router = useRouter();
|
||||
return useMutation({
|
||||
mutationFn: () => deleteMe(),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries(["me"]);
|
||||
router.push("/login");
|
||||
},
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user