Skip to content
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

feat : 닉네임 유효성 검사 #346

Merged
merged 6 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/app/(default)/mypage/profile/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useToast } from '@/hooks/use-toast';
export default function UserProfile() {
const { data: userData, refetch: refetchUserInfo } = useUserInfo();
const { data: categoryData } = useFetchAllCategories();
const toast = useToast();
const { toast } = useToast();

// 상태 설정
const [nickname, setNickname] = useState('');
Expand All @@ -36,15 +36,38 @@ export default function UserProfile() {

// 닉네임 변경
const handleNicknameChange = () => {
// 닉네임 길이 검증
if (nickname.length < 4 || nickname.length > 12) {
toast({
duration: 1000,
description: '닉네임은 4~12자 사이여야 합니다.',
});
return;
}

// 닉네임 패턴 검증
const nicknamePattern = /^[a-zA-Z가-힣0-9]+( [a-zA-Z가-힣0-9]+)*$/;
if (!nicknamePattern.test(nickname)) {
toast({
duration: 1000,
description: '닉네임은 영어, 한글, 숫자 및 단일 공백만 허용됩니다.',
});
return;
}

// 서버로 닉네임 변경 요청
updateUserInfoMutation.mutate(
{ nickname },
{
onSuccess: () => {
toast.toast({ description: '닉네임이 성공적으로 변경되었습니다.' });
toast({
duration: 1000,
description: '닉네임이 성공적으로 변경되었습니다.',
});
refetchUserInfo();
},
onError: () => {
toast.toast({ description: '닉네임을 변경하지 못했어요.' });
toast({ duration: 1000, description: '닉네임을 변경하지 못했어요.' });
setNickname(userData?.data.nickname || '');
},
},
Expand All @@ -57,11 +80,14 @@ export default function UserProfile() {
{ username },
{
onSuccess: () => {
toast.toast({ description: '이름이 성공적으로 변경되었습니다.' });
toast({
duration: 1000,
description: '이름이 성공적으로 변경되었습니다.',
});
refetchUserInfo();
},
onError: () => {
toast.toast({ description: '이름을 변경하지 못했어요.' });
toast({ duration: 1000, description: '이름을 변경하지 못했어요.' });
setUsername(userData?.data.username || '');
},
},
Expand All @@ -88,7 +114,8 @@ export default function UserProfile() {
return [...prevSelected, categoryId];
}

toast.toast({
toast({
duration: 1000,
description: '카테고리는 최대 5개까지 선택 가능합니다.',
});

Expand All @@ -112,13 +139,15 @@ export default function UserProfile() {
{ categories: selectedCategories },
{
onSuccess: () => {
toast.toast({
toast({
duration: 1000,
description: '카테고리가 성공적으로 변경되었습니다.',
});
refetchUserInfo();
},
onError: () => {
toast.toast({
toast({
duration: 1000,
description: '카테고리를 변경하지 못했습니다.',
});
setSelectedCategories(
Expand Down