Skip to content

Commit

Permalink
implement the useValidateNickname hook
Browse files Browse the repository at this point in the history
  • Loading branch information
sonsurim committed Jan 15, 2024
1 parent 8121ef9 commit 37510a3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 37 deletions.
45 changes: 45 additions & 0 deletions src/hooks/useValidateNickname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useCheckValidNicknameMutation } from '@apis'
import { VALID_NICKNAME_MESSAGE } from '@constants'

export const useValidateNickname = () => {
const checkValidNickname = useCheckValidNicknameMutation()

const validateNickname = async (nickname: string) => {
if (nickname.length === 0) {
return {
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.EMPTY_ERROR
}
}

if (nickname.length < 2) {
return {
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.MIN_LENGTH_ERROR
}
}

try {
const { duplicate } = await checkValidNickname.mutateAsync(nickname)

if (duplicate) {
return {
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.DUPLICATED_ERROR
}
} else {
return {
isSuccess: true,
message: VALID_NICKNAME_MESSAGE.SUCCESS
}
}
} catch (e) {
return {
isSuccess: false,
message: 'An error occurred during nickname validation.'
}
}
}

return validateNickname
}
46 changes: 9 additions & 37 deletions src/pages/shop/view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { useRouter } from 'next/router'
import { useState } from 'react'
import { Styled } from './styled'
import { pageTabs, tabList } from '../pageTabs'
import { VALID_NICKNAME_MESSAGE } from '@constants/message'
import { useValidateNickname } from '@hooks/useValidateNickname'
import {
useCheckValidNicknameMutation,
useCreateUploadImagesMutation,
useGetProfileQuery,
useUpdateMyProfileMutation
Expand Down Expand Up @@ -34,48 +33,21 @@ export const ShopPageView = ({ memberId, currentTab }: ShopPageViewProps) => {

const profile = useGetProfileQuery(memberId)
const createUploadImage = useCreateUploadImagesMutation()
const checkValidNickname = useCheckValidNicknameMutation()
const updateMyProfile = useUpdateMyProfileMutation()

const hasToken = !isNumber(memberId)
const isLogin = !isNumber(memberId)
const profileModal = useModal()
const router = useRouter()
const validateNickname = useValidateNickname()

const handleChangePage = (code: TradeActivityCodes) => (): void => {
router.push(`${router.pathname}?tab=${code}`)
setCurrentPage(code)
}

const handleValidateNickname = async (nickname: string) => {
if (nickname.length === 0) {
setEditProfileValidate({
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.EMPTY_ERROR
})
return
}

if (nickname.length < 2) {
setEditProfileValidate({
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.MIN_LENGTH_ERROR
})
return
}

const { duplicate } = await checkValidNickname.mutateAsync(nickname)

if (duplicate) {
setEditProfileValidate({
isSuccess: false,
message: VALID_NICKNAME_MESSAGE.DUPLICATED_ERROR
})
} else {
setEditProfileValidate({
isSuccess: true,
message: VALID_NICKNAME_MESSAGE.SUCCESS
})
}
const validate = await validateNickname(nickname)
setEditProfileValidate(validate)
}
const handleChangeProfileImage = async (image: EditProfileForm['image']) => {
if (!image.file) {
Expand Down Expand Up @@ -112,7 +84,7 @@ export const ShopPageView = ({ memberId, currentTab }: ShopPageViewProps) => {
<Styled.Layout>
<Tabs.List>
{pageTabs
.filter(pageTab => (hasToken ? true : pageTab.tab.code !== 'buy'))
.filter(pageTab => (isLogin ? true : pageTab.tab.code !== 'buy'))
.map(({ tab }) => (
<Styled.Tab
key={`${tab.code}-tab`}
Expand All @@ -127,16 +99,16 @@ export const ShopPageView = ({ memberId, currentTab }: ShopPageViewProps) => {
<Styled.Layout>
<Styled.TabPanels>
{pageTabs
.filter(pageTab => (hasToken ? true : pageTab.tab.code !== 'buy'))
.filter(pageTab => (isLogin ? true : pageTab.tab.code !== 'buy'))
.map(({ tab, panel }) => (
<Tabs.Panel key={`${tab.code}-panel`}>
<Styled.TabPanelContent>
<ProfileBox
{...profile.data}
hasToken={hasToken}
isLogin={isLogin}
onClickEditButton={profileModal.openModal}
/>
{panel({ hasToken, memberId })}
{panel({ isLogin, memberId })}
</Styled.TabPanelContent>
</Tabs.Panel>
))}
Expand Down

0 comments on commit 37510a3

Please sign in to comment.