From 3a22453ddade7e6684a2e839ac4db9b31d02d906 Mon Sep 17 00:00:00 2001 From: iOdiO89 <117376841+iOdiO89@users.noreply.github.com> Date: Sat, 13 Jul 2024 02:45:44 +0900 Subject: [PATCH] =?UTF-8?q?FIX:=20/redirect=20api=EB=93=A4=20=ED=95=9C?= =?UTF-8?q?=EB=B2=88=EC=94=A9=EB=A7=8C=20=ED=98=B8=EC=B6=9C=EB=90=98?= =?UTF-8?q?=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/dynamodb.ts | 11 +++++++++++ src/hooks/query/dynamodb.ts | 18 ++++++++++++++++++ src/pages/oauth/redirect.tsx | 33 ++++++++++++++------------------- 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 src/apis/dynamodb.ts create mode 100644 src/hooks/query/dynamodb.ts 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..cf68d05 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,6 +11,7 @@ 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) { @@ -20,25 +22,18 @@ export default function Redirect() { } 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]); }