Skip to content

Commit

Permalink
REFACTOR: /redirect api들 한번씩만 호출되게 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
iOdiO89 committed Jul 12, 2024
1 parent a588b0a commit 8a6158c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
11 changes: 11 additions & 0 deletions src/apis/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InviteResponse } from "@/screen/manage/ShareLink";
import axios from "axios";

async function getInviteData(inviteDataId: string) {
const { data } = await axios.get<InviteResponse>(
`/api/dynamoDB?id=${inviteDataId}`,
);
return data;
}

export { getInviteData };
18 changes: 18 additions & 0 deletions src/hooks/query/dynamodb.ts
Original file line number Diff line number Diff line change
@@ -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 };
40 changes: 14 additions & 26 deletions src/pages/oauth/redirect.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<InviteResponse>(
`/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]);
}

0 comments on commit 8a6158c

Please sign in to comment.