@@ -5,9 +5,7 @@ import { useState, useRef, useEffect } from "react";
55import Image from "next/image" ;
66import { FiUser , FiEdit2 } from "react-icons/fi" ;
77import BaseInput from "@/app/components/input/text/BaseInput" ;
8- import { useUser } from "@/hooks/useUser" ;
9- import axios from "axios" ;
10- import toast from "react-hot-toast" ;
8+ import { useUser } from "@/hooks/queries/user/me/useUser" ;
119import { useForm } from "react-hook-form" ;
1210import { zodResolver } from "@hookform/resolvers/zod" ;
1311import { z } from "zod" ;
@@ -28,10 +26,9 @@ const editMyProfileSchema = z.object({
2826type EditMyProfileFormData = z . infer < typeof editMyProfileSchema > ;
2927
3028const EditMyProfileModal = ( { isOpen, onClose, className } : EditMyProfileModalProps ) => {
31- const { user, refetch } = useUser ( ) ;
29+ const { user, updateProfile , isUpdating } = useUser ( ) ;
3230 const [ selectedFile , setSelectedFile ] = useState < File | null > ( null ) ;
3331 const [ previewUrl , setPreviewUrl ] = useState < string > ( "" ) ;
34- const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
3532 const fileInputRef = useRef < HTMLInputElement > ( null ) ;
3633
3734 const {
@@ -78,58 +75,19 @@ const EditMyProfileModal = ({ isOpen, onClose, className }: EditMyProfileModalPr
7875 } ;
7976
8077 const onSubmitHandler = async ( data : EditMyProfileFormData ) => {
81- if ( isSubmitting ) return ;
78+ if ( isUpdating ) return ;
8279
83- try {
84- setIsSubmitting ( true ) ;
85-
86- let imageUrl = user ?. imageUrl || "" ;
87-
88- if ( selectedFile ) {
89- const uploadFormData = new FormData ( ) ;
90- uploadFormData . append ( "image" , selectedFile ) ;
91-
92- const uploadResponse = await axios . post ( "/api/images/upload" , uploadFormData , {
93- withCredentials : true ,
94- } ) ;
95-
96- if ( uploadResponse . status === 201 && uploadResponse . data ?. url ) {
97- imageUrl = uploadResponse . data . url ;
98- } else {
99- throw new Error ( "์ด๋ฏธ์ง ์
๋ก๋์ ์คํจํ์ต๋๋ค." ) ;
100- }
101- }
102-
103- const updateData = {
80+ const success = await updateProfile (
81+ {
10482 name : data . name ,
10583 nickname : data . nickname ,
10684 phoneNumber : data . phone ,
107- imageUrl,
108- } ;
109-
110- const updateResponse = await axios . patch ( "/api/users/me" , updateData ) ;
111-
112- if ( updateResponse . status === 200 ) {
113- await refetch ( ) ; // React Query ์บ์ ๊ฐฑ์
114- toast . success ( "ํ๋กํ์ด ์ฑ๊ณต์ ์ผ๋ก ์์ ๋์์ต๋๋ค." ) ;
115- onClose ( ) ;
116- } else {
117- throw new Error ( "ํ๋กํ ์
๋ฐ์ดํธ์ ์คํจํ์ต๋๋ค." ) ;
118- }
119- } catch ( error ) {
120- if ( axios . isAxiosError ( error ) ) {
121- const errorMessage = error . response ?. data ?. message || "ํ๋กํ ์์ ์ ์คํจํ์ต๋๋ค." ;
122- console . error ( "Profile update error:" , {
123- status : error . response ?. status ,
124- data : error . response ?. data ,
125- } ) ;
126- toast . error ( errorMessage ) ;
127- } else {
128- console . error ( "Unexpected error:" , error ) ;
129- toast . error ( "ํ๋กํ ์์ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค." ) ;
130- }
131- } finally {
132- setIsSubmitting ( false ) ;
85+ } ,
86+ selectedFile
87+ ) ;
88+
89+ if ( success ) {
90+ onClose ( ) ;
13391 }
13492 } ;
13593
@@ -201,7 +159,7 @@ const EditMyProfileModal = ({ isOpen, onClose, className }: EditMyProfileModalPr
201159 variant = "white"
202160 size = "w-[327px] h-[54px] md:w-[640px] md:h-[64px]"
203161 wrapperClassName = "px-[14px] md:px-[20px]"
204- disabled = { isSubmitting }
162+ disabled = { isUpdating }
205163 errormessage = { errors [ field . name ] ?. message }
206164 />
207165 </ div >
@@ -213,17 +171,17 @@ const EditMyProfileModal = ({ isOpen, onClose, className }: EditMyProfileModalPr
213171 < button
214172 type = "button"
215173 onClick = { onClose }
216- disabled = { isSubmitting }
174+ disabled = { isUpdating }
217175 className = "text-grayscale-700 w-[158px] rounded-md border border-grayscale-300 bg-white px-4 py-2 text-sm font-semibold transition-colors hover:bg-grayscale-100 md:w-[314px] md:text-base"
218176 >
219177 ์ทจ์
220178 </ button >
221179 < button
222180 type = "submit"
223- disabled = { isSubmitting }
181+ disabled = { isUpdating }
224182 className = "w-[158px] rounded-md bg-primary-orange-300 px-4 py-2 text-sm font-semibold text-white transition-colors hover:bg-primary-orange-200 md:w-[314px] md:text-base"
225183 >
226- { isSubmitting ? "์์ ์ค..." : "์์ ํ๊ธฐ" }
184+ { isUpdating ? "์์ ์ค..." : "์์ ํ๊ธฐ" }
227185 </ button >
228186 </ div >
229187 </ form >
0 commit comments