1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
"use client"
import { useState, useRef } from "react"
import { Loader2, Camera, Upload, Trash2 } from "lucide-react"
import { toast } from "sonner"
import { Session } from "next-auth"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"
interface ProfileImageProps {
user: {
name?: string | null
image?: string | null
}
update: (data?: { name?: string | null; email?: string | null; image?: string | null }) => Promise<Session | null>
}
export function ProfileImage({ user, update }: ProfileImageProps) {
const fileInputRef = useRef<HTMLInputElement>(null)
const [isImageLoading, setIsImageLoading] = useState(false)
const [profileImageUrl, setProfileImageUrl] = useState<string | null>(user.image || null)
const handleImageUpload = async (event: React.ChangeEvent<HTMLInputElement>) => {
const file = event.target.files?.[0]
if (!file) return
setIsImageLoading(true)
try {
const formData = new FormData()
formData.append('image', file)
const response = await fetch('/api/user/profile-image', {
method: 'POST',
body: formData,
})
const result = await response.json()
if (!response.ok) {
toast.error(result.error || 'Failed to upload image')
return
}
setProfileImageUrl(result.profileImage.url)
toast.success('Profile image uploaded successfully!')
await update({
image: result.profileImage.url
})
} catch {
toast.error('An unexpected error occurred')
} finally {
setIsImageLoading(false)
if (fileInputRef.current) {
fileInputRef.current.value = ''
}
}
}
const handleImageDelete = async () => {
setIsImageLoading(true)
try {
const response = await fetch('/api/user/profile-image', {
method: 'DELETE',
})
const result = await response.json()
if (!response.ok) {
toast.error(result.error || 'Failed to delete image')
return
}
setProfileImageUrl(null)
toast.success('Profile image deleted successfully!')
await update({
image: null
})
} catch {
toast.error('An unexpected error occurred')
} finally {
setIsImageLoading(false)
}
}
return (
<Card>
<CardHeader>
<CardTitle className="flex items-center">
<Camera className="mr-2 h-5 w-5" />
Profile Image
</CardTitle>
<CardDescription>
Upload or update your profile picture
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex items-center space-x-6">
<Avatar className="h-24 w-24">
<AvatarImage src={profileImageUrl || undefined} alt="Profile" />
<AvatarFallback className="text-lg">
{user.name?.charAt(0)?.toUpperCase() || 'U'}
</AvatarFallback>
</Avatar>
<div className="flex-1 space-y-3">
<div className="flex items-center space-x-3">
<Button
onClick={() => fileInputRef.current?.click()}
disabled={isImageLoading}
variant="outline"
>
{isImageLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
<Upload className="mr-2 h-4 w-4" />
{profileImageUrl ? 'Change Image' : 'Upload Image'}
</Button>
{profileImageUrl && (
<Button
onClick={handleImageDelete}
disabled={isImageLoading}
variant="outline"
className="text-destructive hover:text-destructive"
>
{isImageLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
<Trash2 className="mr-2 h-4 w-4" />
Remove
</Button>
)}
</div>
<p className="text-xs text-muted-foreground">
Supported formats: JPEG, PNG, WebP, GIF. Maximum size: 10MB.
Images will be resized to 400x400 pixels.
</p>
<input
ref={fileInputRef}
type="file"
accept="image/jpeg,image/jpg,image/png,image/webp,image/gif"
onChange={handleImageUpload}
className="hidden"
/>
</div>
</div>
</CardContent>
</Card>
)
}
|