-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from iOdiO89/97-직원초대링크-생성과-가입-플로우를-안정화한다
fix: 초대링크를 통한 가입시 storeId 받아올 수 있게 로직 변경
- Loading branch information
Showing
6 changed files
with
162 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { InviteSchedule } from "@/data/inviteSchedule"; | ||
import axios from "axios"; | ||
|
||
export interface InviteResponse { | ||
id: string; | ||
inviteData: InviteDataType; | ||
} | ||
interface InviteDataType { | ||
storeId: string; | ||
position: string; | ||
schedule: InviteSchedule; | ||
createdAt: string; | ||
} | ||
|
||
async function getInviteData(inviteDataId: string) { | ||
const { data } = await axios.get<InviteResponse>( | ||
`/api/dynamoDB?id=${inviteDataId}`, | ||
); | ||
return data; | ||
} | ||
|
||
async function postInviteData(body: InviteResponse) { | ||
const { data } = await axios.post("/api/dynamoDB", body); | ||
return data; | ||
} | ||
|
||
export { getInviteData, postInviteData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { InviteResponse, getInviteData, postInviteData } from "@/apis/dynamodb"; | ||
import { myAtom } from "@/data/global"; | ||
import { copyLink } from "@/libs/copy"; | ||
import { useMutation, 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 }; | ||
} | ||
|
||
function usePostInviteData(inviteId: string) { | ||
const { mutate, isSuccess } = useMutation({ | ||
mutationKey: ["getInviteData"], | ||
mutationFn: (body: InviteResponse) => postInviteData(body), | ||
onSuccess: () => copyLink(inviteId), | ||
}); | ||
|
||
return { mutate, isSuccess }; | ||
} | ||
|
||
export { useGetInviteData, usePostInviteData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,42 @@ | ||
import { PostRelationBody } from "@/apis/relation"; | ||
import { myAtom } from "@/data/global"; | ||
import { usePostPlanList } from "@/hooks/query/plan"; | ||
import { usePostRelation } from "@/hooks/query/relation"; | ||
import { InviteResponse, InviteDataType } from "@/screen/manage/ShareLink"; | ||
import axios from "axios"; | ||
import { useGetInviteData } from "@/hooks/query/dynamodb"; | ||
import { useStaffJoin } from "@/hooks/query/relation"; | ||
import { useAtom } from "jotai"; | ||
import { useRouter } from "next/router"; | ||
import { useEffect } from "react"; | ||
|
||
export default function Redirect() { | ||
const { push } = useRouter(); | ||
const router = useRouter(); | ||
|
||
const [my] = useAtom(myAtom); | ||
const { mutate: postRelationMutate } = usePostRelation(); | ||
const { mutate: postPlanListMutate } = usePostPlanList(); | ||
|
||
async function getInviteData(inviteDataId: string) { | ||
const response = await axios.get<InviteResponse>( | ||
`/api/dynamoDB?id=${inviteDataId}`, | ||
); | ||
return response.data; | ||
} | ||
const createNewEmployee = (inviteData: InviteDataType, memberId: string) => { | ||
const body: PostRelationBody = { | ||
role: "STAFF", | ||
position: inviteData.position, | ||
}; | ||
postRelationMutate( | ||
{ storeId: inviteData.storeId, memberId: my?.id as string, body }, | ||
{ | ||
onSuccess: () => | ||
postPlanListMutate({ | ||
storeId: inviteData.storeId, | ||
memberId, | ||
inviteSchedule: inviteData.schedule, | ||
}), | ||
onError: () => console.log("fail"), | ||
}, | ||
); | ||
}; | ||
const { data: inviteResponse, isSuccess } = useGetInviteData(); | ||
const { mutate: staffJoinMutate } = useStaffJoin(); | ||
|
||
useEffect(() => { | ||
if (my?.id === undefined || my?.id === null) return; | ||
if (localStorage.getItem("inviteDataId") !== null) { | ||
const inviteDataId = String(localStorage.getItem("inviteDataId")); | ||
getInviteData(inviteDataId) | ||
.then(data => { | ||
createNewEmployee(data.inviteData, my?.id); | ||
localStorage.removeItem("inviteDataId"); | ||
push("/main"); | ||
}) | ||
.catch(() => { | ||
push("/main"); | ||
}); | ||
const inviteId = localStorage.getItem("inviteDataId"); | ||
if (inviteId !== null) { | ||
// 1. 직원초대링크를 통해 들어온 새 직원 | ||
if (isSuccess && inviteResponse) { | ||
const body: PostRelationBody = { | ||
role: "STAFF", | ||
position: inviteResponse.inviteData.position, | ||
}; | ||
const inviteRequestBody = { | ||
storeId: inviteResponse.inviteData.storeId, | ||
memberId: my?.id as string, | ||
body, | ||
inviteSchedule: inviteResponse.inviteData.schedule, | ||
}; | ||
staffJoinMutate(inviteRequestBody); | ||
} | ||
} else if (my?.relationList.length === 0) { | ||
push("/signup"); | ||
// 2. 회원가입 | ||
router.push("/signup"); | ||
} else { | ||
push("/main"); | ||
// 3. 기존회원 로그인 | ||
router.push("/main"); | ||
} | ||
}, [my]); | ||
}, [isSuccess]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters