@@ -7,62 +7,66 @@ import { Button } from '@/components/ui/button';
77import { Camera , X , Loader2 } from 'lucide-react' ;
88import { saveUserProfile } from '@/lib/indexedDB' ;
99import { postChatUpload } from '@/api/chatAPI' ;
10+ import Image from 'next/image' ;
1011
1112export default function ModelImageUpload ( ) {
1213 const [ user , setUser ] = useAtom ( userAtom ) ;
1314 const [ isLoading , setIsLoading ] = useState ( false ) ;
1415 const [ previewImage , setPreviewImage ] = useState < string | null > ( null ) ;
1516 const fileInputRef = useRef < HTMLInputElement > ( null ) ;
1617
17- const handleImageUpload = useCallback ( async ( event : React . ChangeEvent < HTMLInputElement > ) => {
18- const file = event . target . files ?. [ 0 ] ;
18+ const handleImageUpload = useCallback (
19+ async ( event : React . ChangeEvent < HTMLInputElement > ) => {
20+ const file = event . target . files ?. [ 0 ] ;
1921
20- if ( file && user ) {
21- setIsLoading ( true ) ;
22+ if ( file && user ) {
23+ setIsLoading ( true ) ;
2224
23- // 파일 크기 검증 (5MB 제한)
24- if ( file . size > 5 * 1024 * 1024 ) {
25- alert ( '파일 크기는 5MB 이하여야 합니다.' ) ;
26- setIsLoading ( false ) ;
27- return ;
28- }
29-
30- // 파일 타입 검증
31- if ( ! file . type . startsWith ( 'image/' ) ) {
32- alert ( '이미지 파일만 업로드 가능합니다.' ) ;
33- setIsLoading ( false ) ;
34- return ;
35- }
36-
37- try {
38- const formData = new FormData ( ) ;
39- formData . append ( 'file' , file ) ;
25+ // 파일 크기 검증 (5MB 제한)
26+ if ( file . size > 5 * 1024 * 1024 ) {
27+ alert ( '파일 크기는 5MB 이하여야 합니다.' ) ;
28+ setIsLoading ( false ) ;
29+ return ;
30+ }
4031
41- const response = await postChatUpload ( formData ) ;
42- if ( response . status === 'fail' ) {
43- console . error ( '사진 업로드 중 에러 ') ;
32+ // 파일 타입 검증
33+ if ( ! file . type . startsWith ( 'image/' ) ) {
34+ alert ( '이미지 파일만 업로드 가능합니다. ') ;
4435 setIsLoading ( false ) ;
4536 return ;
4637 }
4738
48- const result = response . data ;
49- setPreviewImage ( result ) ;
50-
51- const newProfile : User = {
52- ...user ,
53- modelImage : result ,
54- } ;
55-
56- setUser ( newProfile ) ;
57- await saveUserProfile ( newProfile ) ;
58- } catch ( error ) {
59- console . error ( '이미지 업로드 실패:' , error ) ;
60- alert ( '이미지 업로드에 실패했습니다.' ) ;
61- } finally {
62- setIsLoading ( false ) ;
39+ try {
40+ const formData = new FormData ( ) ;
41+ formData . append ( 'file' , file ) ;
42+
43+ const response = await postChatUpload ( formData ) ;
44+ if ( response . status === 'fail' ) {
45+ console . error ( '사진 업로드 중 에러' ) ;
46+ setIsLoading ( false ) ;
47+ return ;
48+ }
49+
50+ const result = response . data ;
51+ setPreviewImage ( result ) ;
52+
53+ const newProfile : User = {
54+ ...user ,
55+ modelImage : result ,
56+ } ;
57+
58+ setUser ( newProfile ) ;
59+ await saveUserProfile ( newProfile ) ;
60+ } catch ( error ) {
61+ console . error ( '이미지 업로드 실패:' , error ) ;
62+ alert ( '이미지 업로드에 실패했습니다.' ) ;
63+ } finally {
64+ setIsLoading ( false ) ;
65+ }
6366 }
64- }
65- } , [ user , setUser ] ) ;
67+ } ,
68+ [ user , setUser ] ,
69+ ) ;
6670
6771 const handleRemoveImage = useCallback ( async ( ) => {
6872 if ( user ) {
@@ -107,9 +111,11 @@ export default function ModelImageUpload() {
107111 { ! isLoading && ( user ?. modelImage || previewImage ) && (
108112 < div className = "space-y-3" >
109113 < div className = "relative group" >
110- < img
114+ < Image
111115 src = { previewImage || user ?. modelImage || '/model_image.jpg' }
112116 alt = "현재 설정된 모델 이미지"
117+ width = { 300 }
118+ height = { 300 }
113119 className = "w-full h-48 object-cover rounded-lg border border-gray-200 shadow-sm"
114120 />
115121 { /* 오버레이 버튼들 */ }
@@ -132,13 +138,7 @@ export default function ModelImageUpload() {
132138 </ div >
133139 </ div >
134140 { /* 숨겨진 파일 입력 */ }
135- < input
136- ref = { fileInputRef }
137- type = "file"
138- accept = "image/*"
139- onChange = { handleImageUpload }
140- className = "hidden"
141- />
141+ < input ref = { fileInputRef } type = "file" accept = "image/*" onChange = { handleImageUpload } className = "hidden" />
142142 </ div >
143143 < div className = "text-center" >
144144 < p className = "text-sm text-green-600 font-medium" > ✅ 모델 이미지가 설정되었습니다</ p >
@@ -153,13 +153,7 @@ export default function ModelImageUpload() {
153153 < Camera className = "h-12 w-12 text-gray-400 mx-auto mb-4" />
154154 < p className = "text-gray-500 mb-4" > 가상 피팅에 사용할 모델 이미지를 업로드하세요</ p >
155155 < p className = "text-xs text-gray-400 mb-4" > JPG, PNG, GIF 파일 (최대 5MB)</ p >
156- < input
157- ref = { fileInputRef }
158- type = "file"
159- accept = "image/*"
160- onChange = { handleImageUpload }
161- className = "hidden"
162- />
156+ < input ref = { fileInputRef } type = "file" accept = "image/*" onChange = { handleImageUpload } className = "hidden" />
163157 < Button
164158 onClick = { handleFileSelect }
165159 variant = "outline"
0 commit comments