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

페이지 기능: 로그인 페이지 쿼리훅 제작 #79

Merged
merged 4 commits into from
Jan 17, 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
24 changes: 17 additions & 7 deletions src/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ import Header from '@components/shared/header/Header';
import Spacing from '@components/shared/spacing/Spacing';
import TextField from '@components/shared/text-field/TextField';
import Title from '@components/shared/title/Title';
import VALIDATION_MESSAGE_MAP from '@constants/validationMessage';
import { ISignIn } from '@remote/api/types/auth';
import useLogin from '@remote/queries/auth/useLogin';

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

const cx = classNames.bind(styles);

function LoginPage() {
const { register, handleSubmit } = useForm<ISignIn>();
const { register, handleSubmit, formState: { isValid } } = useForm<ISignIn>();
const { mutate } = useLogin();

// eslint-disable-next-line @typescript-eslint/require-await
const onSubmit = async () => {
// console.log(data);
const onSubmit = (data: ISignIn) => {
const {
id, password,
} = data;
mutate({
id, password,
});
};

// TODO: api return 값에 따라 error처리
return (
<main>
<Header displayLogo={false} />
Expand All @@ -35,16 +44,17 @@ function LoginPage() {
label="아이디"
required
placeholder="아이디"
{...register('id')}
{...register('id', { required: true })}
/>
<TextField
label="비밀번호"
required
placeholder="비밀번호"
{...register('password')}
{...register('password', { required: true })}
helpMessage={VALIDATION_MESSAGE_MAP.failedLogin.message}
/>
<Spacing size={30} />
<Button type="submit" size="medium" full>
<Button type="submit" disabled={!isValid} size="medium" full>
로그인
</Button>
<Spacing size={20} />
Expand Down
1 change: 1 addition & 0 deletions src/constants/validationMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const VALIDATION_MESSAGE_MAP: {
confirmPassword: {
message: '비밀번호를 확인해주세요.',
},
failedLogin: { message: '아이디 또는 비밀번호를 확인해주세요.' },
} as const;

export default VALIDATION_MESSAGE_MAP;
5 changes: 5 additions & 0 deletions src/mocks/authHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ export const authHandlers = [
http.post(`${process.env.NEXT_PUBLIC_BASE_URL}/member/join`, () => {
return HttpResponse.json('회원가입 성공!!');
}),

/* ----- 로그인 api ----- */
http.post(`${process.env.NEXT_PUBLIC_BASE_URL}/member/login`, () => {
return HttpResponse.json('로그인 성공!!');
}),
];
12 changes: 11 additions & 1 deletion src/remote/api/requests/auth/auth.post.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ISignUp } from '../../types/auth';
import { ISignIn, ISignUp } from '../../types/auth';
import { postRequest } from '../requests.api';

export const signup = async ({
Expand All @@ -10,3 +10,13 @@ export const signup = async ({

return response;
};

export const login = async ({
id, password,
}: ISignIn) => {
const response = await postRequest<null, ISignIn>('/member/login', {
id, password,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response에 따라 제네릭에 있는 null 값을 바꿔보죠!
아마 id, email, gender, age 정도 오지 않을까싶네요!

});

return response;
};
16 changes: 16 additions & 0 deletions src/remote/queries/auth/useLogin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useMutation } from '@tanstack/react-query';

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

function useLogin() {
return useMutation({
mutationFn: login,
// eslint-disable-next-line no-console
onSuccess: () => { console.log('요청 성공'); },
onError: () => { console.error('에러 발생'); },
// eslint-disable-next-line no-console
onSettled: () => { console.log('결과에 관계 없이 무언가 실행됨'); },
});
}

export default useLogin;
Loading