diff --git a/data/user/user.api.ts b/data/user/user.api.ts index 1aa32d4..7608a4f 100644 --- a/data/user/user.api.ts +++ b/data/user/user.api.ts @@ -1,6 +1,6 @@ import axios from '~/shared/axios'; import { SuccessResponse } from '~/shared/types'; -import { UserData, NameRegistrationRequest } from './user.model'; +import { UserData } from './user.model'; export async function getUserInfo(userId: string) { const { data } = await axios.get>( @@ -14,12 +14,10 @@ export async function getUserInfo(userId: string) { return data; } -export async function postNameRegistration( - nameRegistrationRequest: NameRegistrationRequest, -) { +export async function postNameRegistration(userData: UserData) { const { data } = await axios.post( '/user/name_registration', - nameRegistrationRequest, + userData, ); return data; } diff --git a/data/user/user.hooks.ts b/data/user/user.hooks.ts index 07d20f9..7e87732 100644 --- a/data/user/user.hooks.ts +++ b/data/user/user.hooks.ts @@ -1,8 +1,13 @@ -import { useQuery, UseQueryOptions } from 'react-query'; +import { + UseMutationOptions, + useQuery, + UseQueryOptions, + useMutation, +} from 'react-query'; import { AxiosError } from 'axios'; import { SuccessResponse } from '~/shared/types'; import { UserData } from './user.model'; -import { getUserInfo } from './user.api'; +import { getUserInfo, postNameRegistration } from './user.api'; export function useUserInfo( userId: string, @@ -23,3 +28,16 @@ export function useUserInfo( }, ); } + +export function useNameRegistration( + options: UseMutationOptions, UserData>, +) { + return useMutation( + '/user/name_registration', + postNameRegistration, + { + retry: false, + ...options, + }, + ); +} diff --git a/data/user/user.model.ts b/data/user/user.model.ts index a7cfcb4..076706f 100644 --- a/data/user/user.model.ts +++ b/data/user/user.model.ts @@ -1,9 +1,4 @@ export interface UserData { userId: string; - userName: string; -} - -export interface NameRegistrationRequest { - userId: string; - name: string; + userName?: string; } diff --git a/mocks/handlers.ts b/mocks/handlers.ts index ba1ef46..d479bdf 100644 --- a/mocks/handlers.ts +++ b/mocks/handlers.ts @@ -1,11 +1,13 @@ import { rest, RestRequest } from 'msw'; import { WritingContentsRequest } from '~/data/services/services.model'; +import { UserData } from '~/data/user/user.model'; import { API_URL } from '~/shared/constants/environments'; import { ContentsFactory, ErrorResponseFactory, WriterFactory, } from './factories'; +import { camelizeKeys } from 'humps'; export const handlers = [ rest.get(`${API_URL}/services/main_contents`, (req, res, ctx) => { @@ -84,7 +86,37 @@ export const handlers = [ const errorResponse = ErrorResponseFactory.build(); return res(ctx.status(422), ctx.json(errorResponse)); } - console.log(writingContents); + + return res( + ctx.status(200), + ctx.json({ + msg: '응답 성공', + data: {}, + }), + ); + }, + ), + rest.post( + `${API_URL}/user/name_registration`, + (req: RestRequest, res, ctx) => { + const userData = camelizeKeys(req.body) as UserData; + console.log(userData); + + if (userData.userId === '') { + const errorResponse = ErrorResponseFactory.build(); + return res(ctx.status(422), ctx.json(errorResponse)); + } + + if (userData.userName === 'invalid') { + return res( + ctx.status(404), + ctx.json({ + msg: '이미 존재하는 필명입니다.', + data: {}, + }), + ); + } + return res( ctx.status(200), ctx.json({ diff --git a/pages/registration/penname.tsx b/pages/registration/username.tsx similarity index 57% rename from pages/registration/penname.tsx rename to pages/registration/username.tsx index 18ef836..4c28a1c 100644 --- a/pages/registration/penname.tsx +++ b/pages/registration/username.tsx @@ -1,9 +1,41 @@ +import { useState, ChangeEvent } from 'react'; import { NextPage } from 'next'; import { NextSeo } from 'next-seo'; import { Box, Text, Button } from '@nolmungshemung/ui-kits'; import { BasicInput } from '~/components/Input'; +import { useNameRegistration } from '~/data/user/user.hooks'; const PenName: NextPage = function () { + const [userName, setUserName] = useState(''); + const [showError, setShowError] = useState(false); + + const { mutate } = useNameRegistration({ + onSuccess() { + setShowError(false); + window.alert('필명이 등록(수정) 되었습니다!'); + }, + onError(error) { + const errorCode = error.response?.status; + if (errorCode === 404) { + setShowError(true); + } else if (errorCode === 422) { + setShowError(false); + window.alert('필명 등록(수정)에 실패했습니다. 관리자에게 문의하세요'); + } + }, + }); + + const handleChange = (e: ChangeEvent) => { + setUserName(e.target.value); + }; + + const onPenNameRegist = () => { + mutate({ + userId: '', + userName, + }); + }; + return ( <> + {showError && ( + + + 이미 사용 중인 필명입니다. 다른 필명을 사용해주세요. + + + )}