-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathProfile.tsx
More file actions
535 lines (497 loc) · 18.1 KB
/
Profile.tsx
File metadata and controls
535 lines (497 loc) · 18.1 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { updateUserProfile, updateUserPassword } from "../utils/firebase";
import {doc, deleteDoc, collection, getDocs, query, where} from "firebase/firestore";
import { useAuth } from "../utils/auth";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { Textarea } from "@/components/ui/textarea";
import { toast } from "sonner";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import * as z from "zod";
import { db } from "../utils/firebase";
import { Trash2, User, Link as LinkIcon } from "lucide-react";
import { Separator } from "@/components/ui/separator";
import SocialMediaLinks from "@/components/SocialMedia";
import FriendsList from "@/components/FriendsList";
import PartnerProgress from "@/components/PartnerProgress";
const profileFormSchema = z.object({
username: z
.string()
.min(3, { message: "Username must be at least 3 characters" }),
gender: z.enum(["male", "female", "other", "prefer-not-to-say"]),
socialMedia: z
.object({
discord: z.string().optional(),
instagram: z.string().optional(),
other: z
.object({
name: z.string().optional(),
url: z
.string()
.url({ message: "Please enter a valid URL" })
.optional(),
})
.optional(),
})
.optional(),
});
const passwordFormSchema = z
.object({
currentPassword: z
.string()
.min(6, { message: "Current password is required" }),
newPassword: z
.string()
.min(6, { message: "Password must be at least 6 characters" }),
confirmPassword: z
.string()
.min(6, { message: "Please confirm your password" }),
})
.refine((data) => data.newPassword === data.confirmPassword, {
message: "Passwords do not match",
path: ["confirmPassword"],
});
type ProfileFormValues = z.infer<typeof profileFormSchema>;
type PasswordFormValues = z.infer<typeof passwordFormSchema>;
const Profile: React.FC = () => {
const [userInitials, setUserInitials] = useState("U");
const { currentUser, userProfile, isLoading } = useAuth();
const navigate = useNavigate();
const [updateLoading, setUpdateLoading] = useState(false);
const [passwordLoading, setPasswordLoading] = useState(false);
useEffect(() => {
if (!isLoading && !currentUser) {
navigate("/login");
}
}, [currentUser, isLoading, navigate]);
const profileForm = useForm<ProfileFormValues>({
resolver: zodResolver(profileFormSchema),
defaultValues: {
username: userProfile?.username || "",
gender: userProfile?.gender || "prefer-not-to-say",
socialMedia: {
discord: userProfile?.socialMedia?.discord || "",
instagram: userProfile?.socialMedia?.instagram || "",
other: {
name: userProfile?.socialMedia?.other?.name || "",
url: userProfile?.socialMedia?.other?.url || "",
},
},
},
});
useEffect(() => {
const fetchUserInitials = async () => {
if (currentUser && currentUser.email) {
try {
const usersRef = collection(db, "users");
const q = query(usersRef, where("email", "==", currentUser.email));
const querySnapshot = await getDocs(q);
console.log(q);
if (!querySnapshot.empty) {
const userDoc = querySnapshot.docs[0].data();
const firstname = userDoc.firstName || "";
const lastname = userDoc.lastName || "";
if (firstname || lastname) {
const initials =
(firstname.charAt(0).toUpperCase() || "") +
(lastname.charAt(0).toUpperCase() || "");
setUserInitials(initials);
} else {
setUserInitials("U");
}
} else {
setUserInitials("U");
}
} catch (error) {
console.error("Error fetching user initials:", error);
setUserInitials("U");
}
}
};
fetchUserInitials();
}, [currentUser]);
useEffect(() => {
if (userProfile) {
profileForm.reset({
username: userProfile.username || "",
gender: userProfile.gender || "prefer-not-to-say",
socialMedia: {
discord: userProfile.socialMedia?.discord || "",
instagram: userProfile.socialMedia?.instagram || "",
other: {
name: userProfile.socialMedia?.other?.name || "",
url: userProfile.socialMedia?.other?.url || "",
},
},
});
}
}, [userProfile]);
const passwordForm = useForm<PasswordFormValues>({
resolver: zodResolver(passwordFormSchema),
defaultValues: {
currentPassword: "",
newPassword: "",
confirmPassword: "",
},
});
const onProfileSubmit = async (data: ProfileFormValues) => {
if (!currentUser) return;
setUpdateLoading(true);
try {
const success = await updateUserProfile(currentUser.uid, {
username: data.username,
gender: data.gender,
socialMedia: {
discord: data.socialMedia?.discord,
instagram: data.socialMedia?.instagram,
other: {
name: data.socialMedia?.other?.name,
url: data.socialMedia?.other?.url,
},
},
});
if (success) {
toast.success("Profile updated successfully");
}
} catch (error) {
toast.error("Failed to update profile");
console.error("Profile update error:", error);
} finally {
setUpdateLoading(false);
}
};
const onPasswordSubmit = async (data: PasswordFormValues) => {
setPasswordLoading(true);
try {
const success = await updateUserPassword(
data.currentPassword,
data.newPassword
);
if (success) {
passwordForm.reset();
}
} catch (error) {
toast.error("Failed to update password");
console.error("Password update error:", error);
} finally {
setPasswordLoading(false);
}
};
if (isLoading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="animate-pulse text-lg">Loading profile...</div>
</div>
);
}
if (!currentUser || !userProfile) {
return (
<div className="min-h-screen flex flex-col items-center justify-center">
<div className="text-lg mb-4">
You need to be signed in to view your profile
</div>
<Button onClick={() => navigate("/login")}>Go to Login</Button>
</div>
);
}
const deleteAccount = async (userId: string) => {
try {
const userRef = doc(db, 'users', userId);
await deleteDoc(userRef);
const user = currentUser;
if (user) {
await user.delete();
}
toast.success("Account deleted successfully");
} catch (error) {
console.error("Error deleting account:", error);
toast.error("Failed to delete account");
}
};
return (
<div className="container max-w-4xl py-10">
<div className="mb-8">
<h1 className="text-3xl font-bold mb-2">Your Profile</h1>
<p className="text-muted-foreground">
Manage your account information and connected social media
</p>
</div>
<div className="flex flex-col md:flex-row gap-6">
<Card className="w-full md:w-1/3">
<CardHeader>
<div className="flex flex-col items-center">
<Avatar className="h-20 w-20 mb-4">
<AvatarFallback className="text-xl">
{userInitials}
</AvatarFallback>
</Avatar>
<CardTitle className="text-center">
{userProfile.firstName + " " + userProfile.lastName || "User"}
</CardTitle>
<CardDescription className="text-center mt-1">
{currentUser.email}
</CardDescription>
</div>
</CardHeader>
<CardContent>
<div className="grid gap-3">
<div className="flex items-center gap-2">
<User size={16} className="text-muted-foreground" />
<span>
Member since{" "}
{userProfile.joinedAt?.toDate().toLocaleDateString() || "N/A"}
</span>
</div>
</div>
</CardContent>
</Card>
<div className="w-full md:w-2/3">
<Tabs defaultValue="profile">
<TabsList className="grid w-full grid-cols-3 mb-4">
<TabsTrigger value="profile">Profile Settings</TabsTrigger>
<TabsTrigger value="security">Security</TabsTrigger>
<TabsTrigger value="friends">Friends</TabsTrigger>
</TabsList>
<TabsContent value="profile">
<Card>
<CardHeader>
<CardTitle>Profile Information</CardTitle>
<CardDescription>
Update your profile details and social media links
</CardDescription>
</CardHeader>
<CardContent>
<Form {...profileForm}>
<form
onSubmit={profileForm.handleSubmit(onProfileSubmit)}
className="space-y-6"
>
<FormField
control={profileForm.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="Your username" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={profileForm.control}
name="gender"
render={({ field }) => (
<FormItem>
<FormLabel>Gender</FormLabel>
<Select
onValueChange={field.onChange}
defaultValue={field.value}
>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="Select gender" />
</SelectTrigger>
</FormControl>
<SelectContent>
<SelectItem value="male">Male</SelectItem>
<SelectItem value="female">Female</SelectItem>
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
<div className="space-y-4">
<SocialMediaLinks userId={currentUser.uid} />
</div>
<Button
type="submit"
className="w-full"
disabled={
updateLoading || !profileForm.formState.isDirty
}
>
{updateLoading ? "Updating..." : "Save Profile Changes"}
</Button>
</form>
</Form>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="security">
<Card>
<CardHeader>
<CardTitle>Security Settings</CardTitle>
<CardDescription>
Update your password and security preferences
</CardDescription>
</CardHeader>
<CardContent>
<Form {...passwordForm}>
<form
onSubmit={passwordForm.handleSubmit(onPasswordSubmit)}
className="space-y-6"
>
<FormField
control={passwordForm.control}
name="currentPassword"
render={({ field }) => (
<FormItem>
<FormLabel>Current Password</FormLabel>
<FormControl>
<Input
type="password"
placeholder="Enter your current password"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={passwordForm.control}
name="newPassword"
render={({ field }) => (
<FormItem>
<FormLabel>New Password</FormLabel>
<FormControl>
<Input
type="password"
placeholder="Enter your new password"
{...field}
/>
</FormControl>
<FormDescription>
Password must be at least 6 characters
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={passwordForm.control}
name="confirmPassword"
render={({ field }) => (
<FormItem>
<FormLabel>Confirm Password</FormLabel>
<FormControl>
<Input
type="password"
placeholder="Confirm your new password"
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="submit"
className="w-full"
disabled={
passwordLoading || !passwordForm.formState.isDirty
}
>
{passwordLoading
? "Updating Password..."
: "Update Password"}
</Button>
</form>
</Form>
<Separator className="my-6" />
<CardContent>
<CardTitle>Danger Zone</CardTitle>
<CardDescription className="mt-2">
Permanently delete your account. This action cannot be
undone.
</CardDescription>
<div className="space-y-4 mt-3">
<p className="text-sm text-red-600">
Once you delete your account, there is no going back.
Please be certain.
</p>
<form
onSubmit={async (e) => {
e.preventDefault();
const formData = new FormData(e.currentTarget);
const confirmation = formData.get("confirmation");
if (confirmation !== "goodbye") {
toast.error("You must type 'goodbye' to confirm.");
return;
}
try {
await deleteAccount(currentUser.uid);
toast.success("Account deleted successfully.");
navigate("/goodbye");
} catch (error) {
toast.error("Failed to delete account.");
console.error("Account deletion error:", error);
}
}}
>
<div className="flex flex-col gap-4">
<Input
name="confirmation"
placeholder="Type 'goodbye' to confirm"
required
/>
<Button
type="submit"
variant="destructive"
color="error"
>
{" "}
<Trash2 />
Delete Account
</Button>
</div>
</form>
</div>
</CardContent>
</CardContent>
</Card>
</TabsContent>
<TabsContent value="friends">
<div className="space-y-6">
<FriendsList />
<PartnerProgress />
</div>
</TabsContent>
</Tabs>
</div>
</div>
</div>
);
};
export default Profile;