3
3
import { useState } from 'react' ;
4
4
import { updateProfilePic } from '@/app/actions/update-profile-pic' ;
5
5
import { useToast } from '@/components/ui/use-toast' ;
6
+ import { Button } from '@/components/ui/button' ;
6
7
7
8
export default function ProfilePicUpload ( { userId } : { userId : string } ) {
8
9
const [ file , setFile ] = useState < File | null > ( null ) ;
9
- const [ error , setError ] = useState < string | null > ( null ) ;
10
+ const [ loading , setLoading ] = useState < boolean > ( false ) ;
11
+
10
12
const { toast } = useToast ( ) ;
11
13
12
14
const handleSubmit = async ( e : React . FormEvent < HTMLFormElement > ) => {
13
15
e . preventDefault ( ) ;
14
- setError ( null ) ;
15
16
if ( ! file ) return ;
17
+ setLoading ( true ) ;
16
18
17
19
const formData = new FormData ( ) ;
18
20
formData . append ( 'file' , file ) ;
@@ -31,6 +33,7 @@ export default function ProfilePicUpload({ userId }: { userId: string }) {
31
33
32
34
if ( data . success ) {
33
35
console . log ( 'File uploaded successfully:' , data . filename ) ;
36
+ setFile ( null ) ;
34
37
const result = await updateProfilePic ( userId , data . filename ) ;
35
38
console . log ( 'Update profile pic result:' , result ) ;
36
39
if ( result . success ) {
@@ -45,24 +48,36 @@ export default function ProfilePicUpload({ userId }: { userId: string }) {
45
48
}
46
49
} catch ( err ) {
47
50
console . error ( 'Error:' , err ) ;
48
- setError (
49
- err instanceof Error ? err . message : 'An unknown error occurred'
50
- ) ;
51
+ toast ( {
52
+ title : err instanceof Error ? err . message : 'An unknown error occurred' ,
53
+ variant : 'destructive' ,
54
+ } ) ;
55
+ } finally {
56
+ setLoading ( false ) ;
51
57
}
52
58
} ;
53
59
54
60
return (
55
- < form onSubmit = { handleSubmit } >
61
+ < form onSubmit = { handleSubmit } className = "space-y-4" >
56
62
< input
57
63
type = "file"
58
64
accept = "image/*"
59
65
onChange = { e => setFile ( e . target . files ?. [ 0 ] || null ) }
60
- className = "cursor-pointer rounded border border-sky-600"
66
+ className = "cursor-pointer block w-full text-sm text-gray-500
67
+ file:mr-4 file:py-2 file:px-4
68
+ file:rounded-full file:border-0
69
+ file:text-sm file:font-semibold
70
+ file:bg-violet-50 file:text-violet-700
71
+ hover:file:bg-violet-100"
61
72
/>
62
- < button type = "submit" disabled = { ! file } >
63
- Upload Profile Picture
64
- </ button >
65
- { error && < p style = { { color : 'red' } } > { error } </ p > }
73
+ < Button
74
+ type = "submit"
75
+ disabled = { ! file || loading }
76
+ className = "w-full bg-violet-600 text-white py-4 rounded-lg
77
+ hover:bg-violet-700 disabled:opacity-50"
78
+ >
79
+ { loading ? 'Uploading...' : 'Upload Profile Picture' }
80
+ </ Button >
66
81
</ form >
67
82
) ;
68
83
}
0 commit comments