-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dep 167 Add flow of invitation #122
Changes from all commits
f1ef4b4
d1af0cf
0c213d0
3cd68bb
71fbfd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
|
||
import { | ||
useGetInvitationCodeIsValid, | ||
useGetUserInfo, | ||
usePostPlanetJoin, | ||
} from '~/api/domain/user.api'; | ||
import { Template } from '~/components/Template'; | ||
import { getUserIdClient } from '~/utils/auth/getUserId.client'; | ||
|
||
const title = '당신을 디프만 행성으로\n 초대합니다'; | ||
|
||
const InvitationPage = ({ params }: { params: { code: string } }) => { | ||
const router = useRouter(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useRouter 를 사용하기 위해서 client component 로 선언했습니다. |
||
const invitationCode = params.code; | ||
|
||
// TODO: 잘못된 행성 코드인 경우, 에러 처리(error.page) | ||
const { data: validPlanet, isLoading: isValidPlanetLoading } = useGetInvitationCodeIsValid( | ||
params.code, | ||
); | ||
const planetId = validPlanet?.planetId; | ||
const userId = getUserIdClient(); | ||
const { | ||
data: userInfo, | ||
isRefetching, | ||
isInitialLoading, | ||
} = useGetUserInfo({ | ||
enabled: !!(planetId && userId), | ||
}); | ||
const { mutateAsync } = usePostPlanetJoin(); | ||
|
||
const onClick = async () => { | ||
const searchParams = new URLSearchParams({ | ||
redirectUri: `/invitation/${invitationCode}`, | ||
}).toString(); | ||
if (planetId && userId) { | ||
await mutateAsync({ planetId, userId }); | ||
if (userInfo?.isCharacterCreated) { | ||
router.push(`/planet/${planetId}`); | ||
} else { | ||
router.push(`/onboarding?${searchParams}`); | ||
} | ||
} else { | ||
router.push(`/auth/signin?${searchParams}`); | ||
} | ||
}; | ||
Comment on lines
+34
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (isValidPlanetLoading) return null; | ||
|
||
return ( | ||
<Template> | ||
<Template.Title className="text-grey-900"> | ||
<h1>{title}</h1> | ||
</Template.Title> | ||
<Template.Content /> | ||
<Template.Button disabled={isInitialLoading || isRefetching} onClick={onClick}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용자의 데이터를 가져오는 동안 button을 비활성화 합니다. |
||
시작하기 | ||
</Template.Button> | ||
</Template> | ||
); | ||
}; | ||
|
||
export default InvitationPage; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,17 @@ export type UserInfoModel = { | |
gender: string; | ||
ageRange: string; | ||
profileImageUrl: string; | ||
isCharacterCreated: boolean; | ||
planetIds: number[]; | ||
}; | ||
|
||
export type CharacterCreateModel = CharacterNameModel; | ||
|
||
export type InvitationCodeValidationModel = { | ||
planetId: number; | ||
}; | ||
|
||
export type PlanetJoinModel = { | ||
userId: number; | ||
planetId: number; | ||
}; | ||
Comment on lines
+16
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아직 api 명세가 나오지 않아 임의로 구현해 놓았습니다. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// user.d.ts 정리 후 절대경로 적용 | ||
import { CharacterCreateModel } from './model.type'; | ||
import { CharacterCreateModel, PlanetJoinModel } from './model.type'; | ||
import { UserInfoResponse } from './response.type'; | ||
|
||
export type UserInfoRequest = Omit<UserInfoResponse, 'userId'>; | ||
|
||
export type CharacterCreateRequest = CharacterCreateModel; | ||
|
||
export type PlanetJoinRequest = PlanetJoinModel; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { UserInfoModel } from './model.type'; | ||
import { InvitationCodeValidationModel, UserInfoModel } from './model.type'; | ||
|
||
export type UserInfoResponse = UserInfoModel; | ||
|
||
export type InvitationCodeValidationResponse = InvitationCodeValidationModel; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import { UserIdNotFoundError } from './error'; | ||
import { getAuthTokensByCookie } from './tokenHandlers'; | ||
|
||
export const getUserIdClient = (): number => { | ||
export const getUserIdClient = (): number | undefined => { | ||
try { | ||
if (typeof document === 'undefined') throw new UserIdNotFoundError(); | ||
const { userId } = getAuthTokensByCookie(document.cookie); | ||
if (userId !== undefined) return userId; | ||
throw new UserIdNotFoundError(); | ||
throw new UserIdNotFoundError(); // 비로그인한 사용자의 경우 | ||
} catch (e) { | ||
return -1; | ||
return undefined; | ||
Comment on lines
-4
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용자의 로그인 여부를 에러 없이 순수하게 알아내기 위해 사용합니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 로그인 여부를
이것보다 아래와 같이 사용하는 게 편하다고 생각했습니다.
cookie에서 가져오는 값이기 때문에 저의 개인적인 생각일 뿐입니다! |
||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public api가 한 파일(api.ts)에서 private api와 함께 선언되고 서버 컴포넌트에서 함께 사용(invitation page)되어도 문제없는 것을 확인했습니다.