diff --git a/src/api/authAPI.ts b/src/api/authAPI.ts new file mode 100644 index 0000000..2ad7185 --- /dev/null +++ b/src/api/authAPI.ts @@ -0,0 +1,10 @@ +import api from "@/lib/api"; + +export const signup = async (data: { email: string; nickname: string; password: string }) => { + const response = await api.post("/user", { + email: data.email, + name: data.nickname, + password: data.password, + }); + return response.data; +}; diff --git a/src/app/(auth)/components/SignupForm.tsx b/src/app/(auth)/components/SignupForm.tsx index 4c8b946..5c6900d 100644 --- a/src/app/(auth)/components/SignupForm.tsx +++ b/src/app/(auth)/components/SignupForm.tsx @@ -1,13 +1,13 @@ "use client"; -import { toast } from "react-toastify"; import { useEffect } from "react"; -import { useRouter } from "next/navigation"; import axios from "axios"; import { z } from "zod"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; +import { useSignup } from "@/hook/useSignup"; + import SubmitButton from "./SubmitButton"; import InputField from "./InputField"; @@ -16,67 +16,54 @@ type FormFields = "nickname" | "email" | "password" | "passwordConfirm"; type FormData = z.infer; export default function SignupForm() { - const router = useRouter(); - const { register, handleSubmit, formState: { errors, isSubmitting }, trigger, setError, - clearErrors, - reset, watch, } = useForm({ resolver: zodResolver(signupSchema), mode: "onChange", // 실시간 유효성 검사를 위해 추가 }); - useEffect(() => { - const firstInput = document.getElementById("nickname") as HTMLInputElement; - if (firstInput) { - firstInput.focus(); - } - }, []); + const signupMutation = useSignup(); const onSubmit = async (data: FormData) => { - try { - const response = await axios.post(`${process.env.NEXT_PUBLIC_API_URL}/user`, { - email: data.email, - name: data.nickname, - password: data.password, - }); - - if (response.data) { - clearErrors(); - reset(); - toast.success("회원가입이 완료되었습니다."); - router.push("/login"); - } - } catch (error) { - console.error("회원가입 서버 오류🚨", error); + signupMutation.mutate(data, { + onError: (error) => { + console.error("회원가입 서버 오류🚨", error); - if (axios.isAxiosError(error) && error.response) { - const errorCode = error.response.status; - const errorInfo = errorMessage[errorCode]; + if (axios.isAxiosError(error) && error.response) { + const errorCode = error.response.status; + const errorInfo = errorMessage[errorCode]; - if (errorCode) { - setError(errorInfo.field, { - type: "manual", - message: errorInfo.message, - }); + if (errorInfo) { + setError(errorInfo.field, { + type: "manual", + message: errorInfo.message, + }); + } } - } - } + }, + }); }; + useEffect(() => { + const firstInput = document.getElementById("nickname") as HTMLInputElement; + if (firstInput) { + firstInput.focus(); + } + }, []); + return (
브랜드
-

+

회원가입 -

+
diff --git a/src/hook/useSignup.tsx b/src/hook/useSignup.tsx new file mode 100644 index 0000000..0c85f9e --- /dev/null +++ b/src/hook/useSignup.tsx @@ -0,0 +1,22 @@ +import { useRouter } from "next/navigation"; +import { toast } from "react-toastify"; +import { useMutation } from "@tanstack/react-query"; + +import { signup } from "@/api/authAPI"; +import { ErrorType } from "@/api/goalAPI"; + +export const useSignup = () => { + const router = useRouter(); + console.log("1"); + + return useMutation({ + mutationFn: signup, + onSuccess: () => { + toast.success("회원가입이 완료되었습니다."); + router.push("/login"); + }, + onError: (error: ErrorType) => { + throw error; + }, + }); +};