-
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.
Browse files
Browse the repository at this point in the history
Feat/#76 user UI
- Loading branch information
Showing
9 changed files
with
179 additions
and
29 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
65 changes: 65 additions & 0 deletions
65
frontend/app/(page)/(needProtection)/mypage/component/ModifyNickname.tsx
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,65 @@ | ||
import { useAuth } from '@/app/hooks/useAuth' | ||
import React, { useState } from 'react' | ||
import { NameMessageInfo, NameMessageType } from '../lib/type.d' | ||
import { validateNickname } from '../lib/util' | ||
import clsx from 'clsx' | ||
import { patchNickname } from '../lib/api' | ||
|
||
const ModifyNickname = () => { | ||
const { | ||
userInfo: { nickname }, | ||
refetch, | ||
} = useAuth() | ||
|
||
const [nicknameStatus, setNicknameStatus] = useState<NameMessageType>('initial') | ||
|
||
const handleSubmitModification = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault() | ||
const formData = new FormData(e.currentTarget) | ||
const nickname = formData.get('nickname') as string | ||
|
||
const currentStatus = validateNickname(nickname ?? '') | ||
if (currentStatus !== 'valid') { | ||
setNicknameStatus(currentStatus) | ||
return | ||
} | ||
|
||
const patchNicknameResult = await patchNickname(nickname) | ||
if (patchNicknameResult === 'valid') { | ||
setNicknameStatus('valid') | ||
refetch() // Todo: nicknameStatus state도 초기화 안되는지 확인 필요. | ||
} else { | ||
setNicknameStatus('duplicate') | ||
} | ||
} | ||
|
||
return ( | ||
<form onSubmit={handleSubmitModification} className="flex flex-col justify-center gap-3"> | ||
<input | ||
type="text" | ||
defaultValue={nickname} | ||
name="nickname" | ||
placeholder="수정할 닉네임을 입력해주세요." | ||
className="w-full px-10 py-3 text-2xl font-bold text-center text-black border-b-2 px- border-lightGray2 focus:outline-none" | ||
/> | ||
<p | ||
className={clsx('font-medium', { | ||
'text-red-500': nicknameStatus !== 'valid', | ||
'text-green-500': nicknameStatus === 'valid', | ||
})} | ||
> | ||
{NameMessageInfo[nicknameStatus]} | ||
</p> | ||
<button type="submit" className={clsx(`py-4 text-white rounded bg-darkGray1 `, {})}> | ||
수정 | ||
</button> | ||
<ul className="text-darkGray1"> | ||
<li>* 10자 이상으로 작성해주세요.</li> | ||
<li>* 알파벳 소문자와 한글만 입력해주세요.</li> | ||
<li>* 특수문자로 _와 .만 입력해주세요.</li> | ||
</ul> | ||
</form> | ||
) | ||
} | ||
|
||
export default ModifyNickname |
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,22 @@ | ||
import { tokenInstance } from '@/app/axios' | ||
import { AxiosError } from 'axios' | ||
|
||
interface IPatchNicknameRes { | ||
msg: string | ||
data: { nickname: string } | ||
} | ||
|
||
export const patchNickname = async (nickname: string): Promise<'valid' | 'duplicated'> => { | ||
try { | ||
await tokenInstance.patch('/users/my-page/nickname', { nickname }) | ||
return 'valid' | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
console.error(error) | ||
return error.response?.status === 409 ? 'duplicated' : 'valid' | ||
} else { | ||
console.error(error) | ||
throw error // 예기치 않은 오류는 다시 던집니다. | ||
} | ||
} | ||
} |
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,16 @@ | ||
export type NameMessageType = | ||
| 'empty' | ||
| 'tooLong' | ||
| 'invalidChar' | ||
| 'duplicate' | ||
| 'valid' | ||
| 'initial' | ||
|
||
export enum NameMessageInfo { | ||
'initial' = '', | ||
'empty' = '닉네임이 비어있습니다.', | ||
'tooLong' = '닉네임은 최대 10자를 넘을 수 없습니다.', | ||
'invalidChar' = '유효하지 않은 문자 입력 입니다.', | ||
'duplicate' = '이미 사용중인 닉네임 입니다.', | ||
'valid' = '프로필이 수정되었습니다.', | ||
} |
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,18 @@ | ||
import { NameMessageType } from './type.d' | ||
|
||
export const validateNickname = (nickname: string): NameMessageType => { | ||
// :: 정규식 | ||
const tooLongPwReg = /^.{10,}$/ // 10자 이상 | ||
const validCharReg = /^[a-z0-9_.ㄱ-ㅎ|ㅏ-ㅣ|가-힣]+$/g // 공백이나 유효하지 않은 문자가 포함된 경우 | ||
|
||
// :: name 유효성 검사 | ||
if (nickname.length === 0) { | ||
return 'empty' | ||
} | ||
if (tooLongPwReg.test(nickname)) { | ||
return 'tooLong' | ||
} else if (!validCharReg.test(nickname)) { | ||
return 'invalidChar' | ||
} | ||
return 'valid' | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { useRouter } from 'next/navigation' | ||
import React from 'react' | ||
|
||
const GoBackButton = () => { | ||
const router = useRouter() | ||
const handleClickGoBack = () => { | ||
router.back() | ||
} | ||
return ( | ||
<button onClick={handleClickGoBack} className="nav-btn-mono"> | ||
돌아가기 | ||
</button> | ||
) | ||
} | ||
|
||
export default GoBackButton |
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