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

Feature/#199 비밀번호 초기화 api 연결 #201

Merged
merged 4 commits into from
Sep 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
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
'use client';

import { Form, Formik } from 'formik';
import * as Yup from 'yup';

import EmailTextInput from '@/components/Formik/EmailTextInput';
import TextInput from '@/components/Formik/TextInput';

const validationSchema = Yup.object().shape({
email: Yup.string()
.required('필수 입력란입니다. 이메일을 입력해주세요.')
.matches(/^((?!@).)*$/, '부산대 이메일만 가입 가능합니다. 이메일 도메인 부분을 삭제해주세요.')
.matches(/^[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*$/, '존재하는 이메일인지 확인해주세요.'),
name: Yup.string().required('필수 입력란입니다. 이름을 입력해주세요.'),
});
import { useResetPasswordMutation } from '@/lib/hooks/useApi';
import { toast } from 'react-toastify';
import { useRouter } from 'next/navigation';

interface FormType {
email: string;
Expand All @@ -25,15 +19,29 @@ const initialValues: FormType = {
};

const FindForm = () => {
const { mutate: resetPasswordMutation } = useResetPasswordMutation();
const router = useRouter();

const handleSubmitButtonClick = (values: FormType) => {
// TODO: 메일 발송 api 호출
console.log(JSON.stringify(values));
resetPasswordMutation(
{ email: values.email, name: values.name },
{
onSuccess(data, variables, context) {
toast.info('임시비밀번호 메일이 발송되었습니다.');
setTimeout(function () {
router.push('/sign-in');
}, 2000);
},
onError(error, variables, context) {
toast.error(error.message);
},
},
);
};

return (
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={(values, { setSubmitting }) => {
setSubmitting(false);
handleSubmitButtonClick(values);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/lib/hooks/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,13 @@ export function useSignInMutation() {
.catch((err) => Promise.reject(err)),
});
}

export function useResetPasswordMutation() {
return useAxiosMutation({
mutationFn: async ({ email, name }: { email: string; name: string }) =>
await client
.patch(`/sign-in/reset-password`, { email: email + '@pusan.ac.kr', name })
.then((res) => res.data)
.catch((err) => Promise.reject(err)),
});
}