-
Notifications
You must be signed in to change notification settings - Fork 0
feat: nickname setup flow #250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0899b2c
fix fix client side
YoonJeongLulu 9f7b095
fix login structure
YoonJeongLulu a8e4e4e
make test skip
YoonJeongLulu e31bda6
wrap suspense to fix bugs
YoonJeongLulu 3c4cde0
feature nickname setup flow
createhb21 91e742b
fix setToken when user has additionalInfo already
createhb21 1433f31
refactor semantic namings
createhb21 699518c
refactor ν
λΆλ¦¬
createhb21 e1bfdef
feature updateUser API Authorization
createhb21 edce901
remove unused components
createhb21 720463d
refactor zustand μ΅μ ν
createhb21 bb930ee
fix μ€νμ
createhb21 2ef2d3b
fix renderIndicatorIcon naming
createhb21 b1eefb2
apply code review
createhb21 0716ca6
resolve conflicts
createhb21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
services/one-app/src/app/(site)/login/_lib/checkNickname.ts
This file contains hidden or 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,45 @@ | ||
| import { z } from 'zod'; | ||
| import axios from 'axios'; | ||
|
|
||
| import { apiClient } from '@/app/api'; | ||
| import { sleep } from '@/common/utils'; | ||
| import { APIResponseCode, RESPONSE_MESSAGES } from '@/common/constants/api'; | ||
|
|
||
| const CheckNicknameResponseSchema = z.object({ | ||
| code: z.literal(APIResponseCode.SUCCESS), | ||
| message: z.literal(RESPONSE_MESSAGES[APIResponseCode.SUCCESS]), | ||
| result: z.object({ | ||
| available: z.boolean(), | ||
| }), | ||
| }); | ||
|
|
||
| type CheckNicknameResponse = z.infer<typeof CheckNicknameResponseSchema>; | ||
|
|
||
| export const checkNickname = async (nickname: string) => { | ||
| try { | ||
| const [res] = await Promise.all([ | ||
| apiClient.post<CheckNicknameResponse>('/members/check-nickname', { | ||
| nickname, | ||
| }), | ||
| sleep(300), | ||
| ]); | ||
|
|
||
| return CheckNicknameResponseSchema.parse(res.data); | ||
| } catch (error) { | ||
| if (axios.isAxiosError(error)) { | ||
| // Axios μ€λ₯ μ²λ¦¬ | ||
| throw new Error( | ||
| `Sign in failed: ${error.response?.data?.message || error.message}`, | ||
| ); | ||
| } else if (error instanceof z.ZodError) { | ||
| // Zod μ€λ₯ μ²λ¦¬ | ||
| throw new Error( | ||
| `Validation failed: ${error.errors.map((e) => e.message).join(', ')}`, | ||
| ); | ||
| } else { | ||
| // κΈ°ν μ€λ₯ μ²λ¦¬ | ||
| console.error('Unexpected error during sign in:', error); | ||
| throw new Error('An unexpected error occurred during sign in.'); | ||
| } | ||
| } | ||
| }; | ||
91 changes: 91 additions & 0 deletions
91
services/one-app/src/app/(site)/login/_lib/useCheckNickname.ts
createhb21 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,91 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState } from 'react'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
|
|
||
| import { useDebounce } from '@/common/hooks/useDebounce'; | ||
| import { checkNickname } from '@/app/(site)/login/_lib/checkNickname'; | ||
|
|
||
| const MAX_LENGTH = 10; | ||
| const MIN_LENGTH = 2; | ||
|
|
||
| enum ErrorStatus { | ||
| TOO_SHORT = `λλ€μμ ${MIN_LENGTH}μ μ΄μ μ λ ₯ν΄μ£ΌμΈμ.`, | ||
| TOO_LONG = `νκΈ,μλ¬Έ ${MAX_LENGTH}μ μ΄νλ‘ μ λ ₯ν΄μ£ΌμΈμ.`, | ||
| DUPLICATED_NAME = 'μ€λ³΅μΈ λλ€μμ΄λΌ μ¬μ©ν μ μμ΅λλ€.', | ||
| INVALID_FORMAT = 'μ§μνμ§ μλ νμμ λλ€.', | ||
| } | ||
|
|
||
| export const useCheckNickname = () => { | ||
| const { mutateAsync: nicknameChecking, status } = useMutation({ | ||
| mutationFn: checkNickname, | ||
| }); | ||
|
|
||
| const [nickname, setNickname] = useState(''); | ||
| const [isTouched, setIsTouched] = useState(false); | ||
| const [errorMessage, setErrorMessage] = useState(''); | ||
YoonJeongLulu marked this conversation as resolved.
Show resolved
Hide resolved
createhb21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const isNicknameChecking = status === 'pending'; | ||
| const isValidateOk = nickname.length >= MIN_LENGTH && errorMessage === ''; | ||
| const isValidateError = | ||
| isTouched && (nickname.length < MIN_LENGTH || errorMessage !== ''); | ||
|
|
||
| const disabled = | ||
| errorMessage !== '' || | ||
| nickname.length < MIN_LENGTH || | ||
| nickname.length > MAX_LENGTH; | ||
|
|
||
| const lengthIndicator = `${nickname.length} / ${MAX_LENGTH}`; | ||
|
|
||
| const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
| const value = e.target.value.slice(0, MAX_LENGTH); | ||
| setNickname(value); | ||
| if (!isTouched) setIsTouched(true); | ||
| checkNicknameValidity(value); | ||
| }; | ||
|
|
||
| const checkNicknameValidity = useDebounce(async (value: string) => { | ||
| if (value.length < MIN_LENGTH) { | ||
| setErrorMessage(ErrorStatus.TOO_SHORT); | ||
| return; | ||
| } | ||
| if (value.length > MAX_LENGTH) { | ||
| setErrorMessage(ErrorStatus.TOO_LONG); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| const res = await nicknameChecking(value); | ||
| if (!res.result.available) { | ||
| setErrorMessage(ErrorStatus.DUPLICATED_NAME); | ||
| } else { | ||
| setErrorMessage(''); | ||
| } | ||
| } catch (error) { | ||
| setErrorMessage(ErrorStatus.INVALID_FORMAT); | ||
| } | ||
| }, 500); | ||
|
|
||
| const nickNameStatusMessage = (() => { | ||
| if (errorMessage) { | ||
| return errorMessage; | ||
| } | ||
| if (isValidateOk) { | ||
| return 'μ¬μ©ν μ μλ λλ€μ μ λλ€.'; | ||
| } | ||
| return `λλ€μμ ${MIN_LENGTH}μ μ΄μ μ λ ₯ν΄μ£ΌμΈμ.`; | ||
| })(); | ||
|
|
||
| return { | ||
| nickname, | ||
| disabled, | ||
| errorMessage, | ||
| lengthIndicator, | ||
| isTouched, | ||
| isValidateOk, | ||
| isValidateError, | ||
| isNicknameChecking, | ||
| nickNameStatusMessage, | ||
| handleInputChange, | ||
| }; | ||
| }; | ||
15 changes: 15 additions & 0 deletions
15
services/one-app/src/app/(site)/login/_lib/utilityFunctions.tsx
This file contains hidden or 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,15 @@ | ||
| import CheckIcon from '@/common/assets/icons/check'; | ||
| import AlertCircleIcon from '@/common/assets/icons/alert-circle'; | ||
|
|
||
| export const renderIndicatorIcon = ( | ||
| isValidateOk: boolean, | ||
| isValidateError: boolean, | ||
| ) => { | ||
| if (isValidateOk) { | ||
| return <CheckIcon />; | ||
| } | ||
| if (isValidateError) { | ||
| return <AlertCircleIcon />; | ||
| } | ||
| return null; | ||
| }; |
This file contains hidden or 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
128 changes: 128 additions & 0 deletions
128
services/one-app/src/app/(site)/login/nickname/page.tsx
This file contains hidden or 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,128 @@ | ||
| 'use client'; | ||
|
|
||
| import React from 'react'; | ||
| import { useRouter } from 'next/navigation'; | ||
| import { useMutation } from '@tanstack/react-query'; | ||
| import { useShallow } from 'zustand/shallow'; | ||
|
|
||
| import { updateUser } from '../../my/_lib/updateUser'; | ||
| import { useCheckNickname } from '../_lib/useCheckNickname'; | ||
| import { renderIndicatorIcon } from '../_lib/utilityFunctions'; | ||
| import { useTemporaryAuthStore } from '@/store/auth'; | ||
| import { AuthService } from '@/common/service/AuthService'; | ||
| import { cn } from '@/common/utils/cn'; | ||
| import ArrowLeftIcon from '@/common/assets/icons/arrow-left'; | ||
| import SpinnerIcon from '@/common/assets/icons/loading-spinner'; | ||
|
|
||
| const NicknameSetup = () => { | ||
| const router = useRouter(); | ||
| const { | ||
| nickname, | ||
| disabled, | ||
| lengthIndicator, | ||
| isTouched, | ||
| isValidateOk, | ||
| isValidateError, | ||
| isNicknameChecking, | ||
| nickNameStatusMessage, | ||
| handleInputChange, | ||
| } = useCheckNickname(); | ||
|
|
||
| const { auth, reset: removeTemporaryAuth } = useTemporaryAuthStore( | ||
| useShallow((state) => ({ | ||
| auth: state.auth, | ||
| reset: state.reset, | ||
| })), | ||
| ); | ||
| const { mutate: updateUserAndTryLoginProcessDone, status } = useMutation({ | ||
createhb21 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mutationFn: updateUser, | ||
| onSuccess: () => { | ||
| if (!auth) return; | ||
|
|
||
| const { accessToken, refreshToken } = auth; | ||
| AuthService.setToken(accessToken, refreshToken); | ||
| removeTemporaryAuth(); | ||
| router.replace('/'); | ||
| }, | ||
| }); | ||
|
|
||
| const handleSubmit = () => { | ||
| if (disabled || !auth) return; | ||
| updateUserAndTryLoginProcessDone({ nickname, auth }); | ||
| }; | ||
|
|
||
| const isUpdating = status === 'pending'; | ||
| const isProcessing = isNicknameChecking || isUpdating; | ||
|
|
||
| return ( | ||
| <main className="relative min-h-screen bg-black pt-9 px-5"> | ||
| <div className="flex items-center mb-8"> | ||
| <button | ||
| onClick={() => router.back()} | ||
| className="p-2 -ml-2 text-white hover:bg-white/10 rounded-full" | ||
| > | ||
| <ArrowLeftIcon /> | ||
| </button> | ||
| <h2 className="ml-2 text-white">νμκ°μ </h2> | ||
| </div> | ||
|
|
||
| <h1 className="pb-8 text-white text-2xl font-semibold"> | ||
| <strong className="text-[#2ACF6C]">λλ€μ</strong>μ μ€μ ν΄μ£ΌμΈμ | ||
| </h1> | ||
|
|
||
| <div className="w-full space-y-2"> | ||
| <div className="relative"> | ||
| <input | ||
| value={nickname} | ||
| onChange={handleInputChange} | ||
| className={cn( | ||
| 'w-full h-12 bg-white/10 border-0 px-3 text-white placeholder:text-gray-500', | ||
| 'focus:outline-none focus:bg-white/15', | ||
| isValidateError && 'ring-2 ring-red-500', | ||
| isValidateOk && 'ring-2 ring-[#2ACF6C]', | ||
| )} | ||
| placeholder="λλ€μμ μ λ ₯ν΄μ£ΌμΈμ" | ||
| /> | ||
| <div className="absolute right-3 top-1/2 -translate-y-1/2"> | ||
| {renderIndicatorIcon(isValidateOk, isValidateError)} | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="flex justify-between items-center px-1"> | ||
| <span | ||
| className={cn( | ||
| 'text-sm', | ||
| isValidateError && 'text-red-500', | ||
| isValidateOk && 'text-[#2ACF6C]', | ||
| !isTouched && 'text-gray-500', | ||
| )} | ||
| > | ||
| {nickNameStatusMessage} | ||
| </span> | ||
| <span className="text-sm text-gray-500">{lengthIndicator}</span> | ||
| </div> | ||
| </div> | ||
|
|
||
| <button | ||
| disabled={disabled} | ||
| className={cn( | ||
| 'w-full h-12 mt-8 text-white', | ||
| 'bg-[#2ACF6C] hover:bg-[#2ACF6C]/90', | ||
| 'disabled:bg-gray-600 disabled:cursor-not-allowed', | ||
| isProcessing && 'cursor-events-none', | ||
| )} | ||
| onClick={handleSubmit} | ||
| > | ||
| {isProcessing ? ( | ||
| <span className="flex items-center justify-center"> | ||
| <SpinnerIcon className="animate-spin mr-2" /> | ||
| </span> | ||
| ) : ( | ||
| 'μλ£' | ||
| )} | ||
| </button> | ||
| </main> | ||
| ); | ||
| }; | ||
|
|
||
| export default NicknameSetup; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.