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

페이지 기능: 회원가입 페이지 쿼리훅 제작 #70

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
63 changes: 27 additions & 36 deletions src/app/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable max-len */
/* eslint-disable @typescript-eslint/no-misused-promises */

'use client';

import { useForm } from 'react-hook-form';

// import VALIDATION_MESSAGE_MAP from '@constants/validationMessage';
import classNames from 'classnames/bind';

import Button from '@components/shared/button/Button';
Expand All @@ -15,47 +14,39 @@ import Spacing from '@components/shared/spacing/Spacing';
import Text from '@components/shared/text/Text';
import TextField from '@components/shared/text-field/TextField';
import Title from '@components/shared/title/Title';
import VALIDATION_MESSAGE_MAP from '@constants/validationMessage';
import { ISignUp } from '@remote/api/types/auth';
import useSignup from '@remote/queries/auth/useSignup';

import styles from './page.module.scss';

const cx = classNames.bind(styles);

const VALIDATION_MESSAGE_MAP: {
[key: string]: {
value?: RegExp,
message: string
}
} = {
id: {
value: /^(?=.*[a-z])(?=.*\d)[a-z\d]{8,}$/,
message: '영문 소문자, 숫자 조합 8자 이상 입력해주세요',
},
email: {
value: /^[_a-zA-Z0-9-.]+@[.a-zA-Z0-9-]+\.[a-zA-Z]+$/,
message: '이메일 형식을 확인해주세요',
},
password: {
value: /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{8,16}$/,
message: '8~16자의 영문 대/소문자, 숫자, 특수문자를 사용해 주세요.',
},
confirmingPassword: {
message: '비밀번호를 확인해주세요.',
},
};
type SignUpFormType = {
confirmPassword: string
} & ISignUp;

function SignupPage() {
const {
register, handleSubmit, formState: { errors }, watch,
} = useForm({
register, handleSubmit, formState: { errors, isValid }, watch,
} = useForm<SignUpFormType>({
mode: 'onBlur',
});

const onSubmit = () => {
// console.log(data);
const { mutate } = useSignup();

const onSubmit = (data: SignUpFormType) => {
const {
id, password, email, gender, age,
} = data;
mutate({
id, password, email, gender, age,
});
};

// TODO: 아이디 textfield onBlur 시 중복된 아이디 검사

return (
// eslint-disable-next-line @typescript-eslint/no-misused-promises
<form onSubmit={handleSubmit(onSubmit)}>
<Header displayLogo={false} />
<Spacing size={20} />
Expand Down Expand Up @@ -88,17 +79,17 @@ function SignupPage() {
required
placeholder="비밀번호 확인"
isPasswordType
{...register('confirmingPassword', {
{...register('confirmPassword', {
required: true,
// eslint-disable-next-line consistent-return
validate: (confirmingPassword: string) => {
if (watch('password') !== confirmingPassword) {
validate: (confirmPassword: string) => {
if (watch('password') !== confirmPassword) {
return false;
}
return true;
},
})}
hasError={!!errors.confirmingPassword}
helpMessage={VALIDATION_MESSAGE_MAP.confirmingPassword.message}
hasError={!!errors.confirmPassword}
helpMessage={VALIDATION_MESSAGE_MAP.confirmPassword.message}
/>
<TextField
label="이메일"
Expand Down Expand Up @@ -128,7 +119,7 @@ function SignupPage() {
<Radio type="ageGroup" label="60대 이상" value="60" {...register('age')} />
</div>
<Spacing size={50} />
<Button type="submit" size="medium" full>약관 동의하러 가기</Button>
<Button type="submit" disabled={!isValid} size="medium" full>약관 동의하러 가기</Button>
<Spacing size={20} />
</form>
);
Expand Down
6 changes: 3 additions & 3 deletions src/constants/validationMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const VALIDATION_MESSAGE_MAP: {
message: '이메일 형식을 확인해주세요',
},
password: {
value: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d!@#$%^&*()_+]{8,}$/,
value: /^(?=.*[a-zA-Z])(?=.*[!@#$%^*+=-])(?=.*[0-9]).{8,16}$/,
message: '8~16자의 영문 대/소문자, 숫자, 특수문자를 사용해 주세요.',
},
confirmingPassword: {
confirmPassword: {
message: '비밀번호를 확인해주세요.',
},
};
} as const;

export default VALIDATION_MESSAGE_MAP;
9 changes: 9 additions & 0 deletions src/mocks/authHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { http, HttpResponse } from 'msw';

export const authHandlers = [
/* ----- 회원가입 api ----- */
http.post(`${process.env.NEXT_PUBLIC_BASE_URL}/member/join`, () => {
return HttpResponse.json('회원가입 성공!!');
}),
];
7 changes: 2 additions & 5 deletions src/mocks/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { http, HttpResponse } from 'msw';
import { authHandlers } from './authHandler';

export const handlers = [
http.get('http://localhost:3030/test', () => {
return HttpResponse.json(['사과', '배']);
}),
...authHandlers,
];
1 change: 1 addition & 0 deletions src/remote/api/instance.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { Axios } from 'axios';

export const axiosInstance: Axios = axios.create({
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
withCredentials: true,
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
Expand Down
12 changes: 12 additions & 0 deletions src/remote/api/requests/auth/auth.post.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ISignUp } from '../../types/auth';
import { postRequest } from '../requests.api';

export const signup = async ({
id, password, email, gender, age,
}: ISignUp) => {
const response = postRequest<null, ISignUp>('/member/join', {
id, password, email, gender, age,
});

return response;
};
10 changes: 10 additions & 0 deletions src/remote/api/types/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface ISignIn {
id: string
password: string
}

export interface ISignUp extends ISignIn {
email: string
gender: string | null
age: string | null
}
9 changes: 9 additions & 0 deletions src/remote/queries/auth/useSignup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useMutation } from '@tanstack/react-query';

import { signup } from '@remote/api/requests/auth/auth.post.api';

function useSignup() {
return useMutation({ mutationFn: signup });
}

export default useSignup;
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
],
"@mocks/*": [
"src/mocks/*"
],
"@types/*": [
"src/types/*"
]
}
},
Expand Down
Loading