Skip to content

Commit

Permalink
feat: 필명 등록 API 연동
Browse files Browse the repository at this point in the history
mock api를 이용한 필명등록 API 기능 개발.
error code에 따라 중복필명처리, 시스템 alert 처리 추가
- login 기능 추가 후 userId 처리 추가

close #53
  • Loading branch information
ppark2ya committed Feb 14, 2022
1 parent 4cdfc0c commit 21935df
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 14 deletions.
8 changes: 3 additions & 5 deletions data/user/user.api.ts
Original file line number Diff line number Diff line change
@@ -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<SuccessResponse<UserData>>(
Expand All @@ -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<SuccessResponse>(
'/user/name_registration',
nameRegistrationRequest,
userData,
);
return data;
}
Expand Down
22 changes: 20 additions & 2 deletions data/user/user.hooks.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -23,3 +28,16 @@ export function useUserInfo(
},
);
}

export function useNameRegistration(
options: UseMutationOptions<SuccessResponse, AxiosError<unknown>, UserData>,
) {
return useMutation<SuccessResponse, AxiosError, UserData>(
'/user/name_registration',
postNameRegistration,
{
retry: false,
...options,
},
);
}
7 changes: 1 addition & 6 deletions data/user/user.model.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
export interface UserData {
userId: string;
userName: string;
}

export interface NameRegistrationRequest {
userId: string;
name: string;
userName?: string;
}
34 changes: 33 additions & 1 deletion mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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<UserData>, 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({
Expand Down
50 changes: 50 additions & 0 deletions pages/registration/penname.tsx → pages/registration/username.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement>) => {
setUserName(e.target.value);
};

const onPenNameRegist = () => {
mutate({
userId: '',
userName,
});
};

return (
<>
<NextSeo
Expand Down Expand Up @@ -46,6 +78,8 @@ const PenName: NextPage = function () {
>
<BasicInput
type="text"
value={userName}
onChange={handleChange}
placeholder="필명"
css={{
backgroundColor: '#EEE',
Expand All @@ -57,12 +91,28 @@ const PenName: NextPage = function () {
}}
/>
</Box>
{showError && (
<Box
css={{
marginBottom: '$sp-20',
}}
>
<Text
css={{
color: 'rgba(222, 14, 14, 0.9)',
}}
>
이미 사용 중인 필명입니다. 다른 필명을 사용해주세요.
</Text>
</Box>
)}
<Button
color="primary"
size="lg"
css={{
cursor: 'pointer',
}}
onClick={onPenNameRegist}
>
등록하기
</Button>
Expand Down

0 comments on commit 21935df

Please sign in to comment.