@@ -87,34 +87,34 @@ export default function ProfileEditPage() {
8787 } ;
8888
8989 const handleAvatarUpload = async ( e : React . ChangeEvent < HTMLInputElement > ) => {
90- const file = e . target . files ?. [ 0 ] ;
91- if ( ! file || ! profile ?. id ) return ;
90+ const file = e . target . files ?. [ 0 ] ;
91+ if ( ! file || ! profile ?. id ) return ;
9292
93- try {
94- setUploadingAvatar ( true ) ;
95- const formDataUpload = new FormData ( ) ;
96- formDataUpload . append ( 'files' , file ) ;
97- formDataUpload . append ( 'ref' , 'api::profile.profile' ) ;
98- formDataUpload . append ( 'refId' , String ( profile . id ) ) ;
99- formDataUpload . append ( 'field' , 'avatar' ) ;
100-
101- const uploadResponse = await fetch ( '/api/upload' , {
102- method : 'POST' ,
103- body : formDataUpload ,
104- } ) ;
93+ try {
94+ setUploadingAvatar ( true ) ;
10595
106- if ( uploadResponse . ok ) {
107- const uploadedFiles = await uploadResponse . json ( ) ;
108- if ( uploadedFiles ?. [ 0 ] ) {
109- setProfile ( prev => prev ? { ...prev , avatar : uploadedFiles [ 0 ] } : null ) ;
110- }
111- }
112- } catch ( err ) {
113- console . error ( 'Error uploading avatar:' , err ) ;
114- } finally {
115- setUploadingAvatar ( false ) ;
96+ const formData = new FormData ( ) ;
97+ formData . append ( 'avatar' , file ) ;
98+
99+ // send the profile id in query or body
100+ const uploadResponse = await fetch ( `/api/profiles/avatar?userId=${ user . id } ` , {
101+ method : 'PUT' ,
102+ body : formData ,
103+ } ) ;
104+
105+ if ( ! uploadResponse . ok ) throw new Error ( 'Upload failed' ) ;
106+
107+ const uploadedFiles = await uploadResponse . json ( ) ;
108+ if ( uploadedFiles ?. [ 0 ] ) {
109+ setProfile ( prev => prev ? { ...prev , avatar : uploadedFiles [ 0 ] } : null ) ;
116110 }
117- } ;
111+ } catch ( err ) {
112+ console . error ( 'Error uploading avatar:' , err ) ;
113+ } finally {
114+ setUploadingAvatar ( false ) ;
115+ }
116+ } ;
117+
118118
119119 const handleSubmit = async ( e : React . FormEvent ) => {
120120 e . preventDefault ( ) ;
@@ -194,7 +194,7 @@ export default function ProfileEditPage() {
194194 </ Box >
195195 ) }
196196 </ Box >
197- { /* <Box>
197+ < Box >
198198 < Button
199199 variant = "secondary"
200200 startIcon = { < Upload /> }
@@ -212,7 +212,7 @@ export default function ProfileEditPage() {
212212 < Typography variant = "pi" textColor = "neutral600" marginTop = { 2 } >
213213 JPG, PNG or GIF. Max size 2MB.
214214 </ Typography >
215- </Box> */ }
215+ </ Box >
216216 </ Flex >
217217 < Flex gap = { 4 } wrap = "wrap" >
218218 < Box flex = "1" minWidth = "280px" >
0 commit comments