diff --git a/src/apis/dynamodb.ts b/src/apis/dynamodb.ts new file mode 100644 index 0000000..cf5e7b0 --- /dev/null +++ b/src/apis/dynamodb.ts @@ -0,0 +1,11 @@ +import { InviteResponse } from "@/screen/manage/ShareLink"; +import axios from "axios"; + +async function getInviteData(inviteDataId: string) { + const { data } = await axios.get( + `/api/dynamoDB?id=${inviteDataId}`, + ); + return data; +} + +export { getInviteData }; diff --git a/src/hooks/query/dynamodb.ts b/src/hooks/query/dynamodb.ts new file mode 100644 index 0000000..d1b0d67 --- /dev/null +++ b/src/hooks/query/dynamodb.ts @@ -0,0 +1,18 @@ +import { getInviteData } from "@/apis/dynamodb"; +import { myAtom } from "@/data/global"; +import { useQuery } from "@tanstack/react-query"; +import { useAtom } from "jotai"; + +function useGetInviteData() { + const [my] = useAtom(myAtom); + const inviteId = localStorage.getItem("inviteDataId"); + const { data, isSuccess } = useQuery({ + queryKey: ["getInviteData"], + queryFn: () => getInviteData(inviteId as string), + enabled: !(my?.id === undefined || my?.id === null || inviteId === null), + }); + + return { data, isSuccess }; +} + +export { useGetInviteData }; diff --git a/src/pages/oauth/redirect.tsx b/src/pages/oauth/redirect.tsx index 4668251..5271934 100644 --- a/src/pages/oauth/redirect.tsx +++ b/src/pages/oauth/redirect.tsx @@ -1,5 +1,6 @@ import { PostRelationBody } from "@/apis/relation"; import { myAtom } from "@/data/global"; +import { useGetInviteData } from "@/hooks/query/dynamodb"; import { useStaffJoin } from "@/hooks/query/relation"; import { InviteResponse } from "@/screen/manage/ShareLink"; import axios from "axios"; @@ -10,35 +11,22 @@ import { useEffect } from "react"; export default function Redirect() { const { push } = useRouter(); const [my] = useAtom(myAtom); + const { data: inviteResponse, isSuccess } = useGetInviteData(); const { mutate: staffJoinMutate } = useStaffJoin(); - async function getInviteData(inviteDataId: string) { - const response = await axios.get( - `/api/dynamoDB?id=${inviteDataId}`, - ); - return response.data; - } - useEffect(() => { - if (my?.id === undefined || my?.id === null) return; - if (localStorage.getItem("inviteDataId") !== null) { - const inviteDataId = String(localStorage.getItem("inviteDataId")); - getInviteData(inviteDataId).then(data => { - const body: PostRelationBody = { - role: "STAFF", - position: data.inviteData.position, - }; - staffJoinMutate({ - storeId: data.inviteData.storeId, - memberId: my?.id as string, - body, - inviteSchedule: data.inviteData.schedule, - }); + if (isSuccess && inviteResponse) { + const inviteData = inviteResponse.inviteData; + const body: PostRelationBody = { + role: "STAFF", + position: inviteData.position, + }; + staffJoinMutate({ + storeId: inviteData.storeId, + memberId: my?.id as string, + body, + inviteSchedule: inviteData.schedule, }); - } else if (my?.relationList.length === 0) { - push("/signup"); - } else { - push("/main"); } - }, [my]); + }, [isSuccess]); }