From ca731fb137465408a3c1729c13d785f7857e67e0 Mon Sep 17 00:00:00 2001 From: schererleander Date: Fri, 26 Dec 2025 16:09:31 +0100 Subject: refactor: modularize settings form into smaller components --- src/app/settings/password-form.tsx | 182 ++++++++++++++++ src/app/settings/profile-form.tsx | 115 ++++++++++ src/app/settings/profile-image.tsx | 154 +++++++++++++ src/app/settings/settings-form.tsx | 430 +------------------------------------ 4 files changed, 457 insertions(+), 424 deletions(-) create mode 100644 src/app/settings/password-form.tsx create mode 100644 src/app/settings/profile-form.tsx create mode 100644 src/app/settings/profile-image.tsx (limited to 'src/app') diff --git a/src/app/settings/password-form.tsx b/src/app/settings/password-form.tsx new file mode 100644 index 0000000..2377408 --- /dev/null +++ b/src/app/settings/password-form.tsx @@ -0,0 +1,182 @@ +"use client" + +import { useState } from "react" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { Eye, EyeOff, Loader2, Lock, Save } from "lucide-react" +import { toast } from "sonner" + +import { Button } from "@/components/ui/button" +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" +import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { passwordChangeSchema, type PasswordChangeInput } from "@/lib/validation" + +export function PasswordForm() { + const [showCurrentPassword, setShowCurrentPassword] = useState(false) + const [showNewPassword, setShowNewPassword] = useState(false) + const [showConfirmPassword, setShowConfirmPassword] = useState(false) + const [isLoading, setIsLoading] = useState(false) + + const form = useForm({ + resolver: zodResolver(passwordChangeSchema), + defaultValues: { + currentPassword: "", + newPassword: "", + confirmPassword: "", + }, + }) + + const onSubmit = async (data: PasswordChangeInput) => { + setIsLoading(true) + + try { + const response = await fetch("/api/user/password", { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + currentPassword: data.currentPassword, + newPassword: data.newPassword, + }), + }) + + const result = await response.json() + + if (!response.ok) { + toast.error(result.error || "Failed to update password") + return + } + + toast.success("Password updated successfully!") + form.reset() + } catch { + toast.error("An unexpected error occurred") + } finally { + setIsLoading(false) + } + } + + return ( + + + + + Change Password + + + Update your password to keep your account secure + + + +
+ + ( + + Current Password + +
+ + +
+
+ +
+ )} + /> + ( + + New Password + +
+ + +
+
+ +
+ )} + /> + ( + + Confirm New Password + +
+ + +
+
+ +
+ )} + /> +
+ Password must contain at least 8 characters with uppercase, lowercase, and a number. +
+ + + +
+
+ ) +} diff --git a/src/app/settings/profile-form.tsx b/src/app/settings/profile-form.tsx new file mode 100644 index 0000000..6f532f0 --- /dev/null +++ b/src/app/settings/profile-form.tsx @@ -0,0 +1,115 @@ +"use client" + +import { useState } from "react" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { Loader2, User, Save } 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 { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" +import { Input } from "@/components/ui/input" +import { updateProfileSchema, type UpdateProfileInput } from "@/lib/validation" + +interface ProfileFormProps { + user: { + name?: string | null + email?: string | null + } + update: (data?: { name?: string | null; email?: string | null; image?: string | null }) => Promise +} + +export function ProfileForm({ user, update }: ProfileFormProps) { + const [isLoading, setIsLoading] = useState(false) + + const form = useForm({ + resolver: zodResolver(updateProfileSchema), + defaultValues: { + name: user.name || "", + email: user.email || "", + }, + }) + + const onSubmit = async (data: UpdateProfileInput) => { + setIsLoading(true) + + try { + const response = await fetch("/api/user/profile", { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data), + }) + + const result = await response.json() + + if (!response.ok) { + toast.error(result.error || "Failed to update profile") + return + } + + await update({ + name: data.name, + email: data.email, + }) + + toast.success("Profile updated successfully!") + } catch { + toast.error("An unexpected error occurred") + } finally { + setIsLoading(false) + } + } + + return ( + + + + + Profile Information + + + Update your personal information + + + +
+ + ( + + Full Name + + + + + + )} + /> + ( + + Email Address + + + + + + )} + /> + + + +
+
+ ) +} diff --git a/src/app/settings/profile-image.tsx b/src/app/settings/profile-image.tsx new file mode 100644 index 0000000..7b4114d --- /dev/null +++ b/src/app/settings/profile-image.tsx @@ -0,0 +1,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 +} + +export function ProfileImage({ user, update }: ProfileImageProps) { + const fileInputRef = useRef(null) + const [isImageLoading, setIsImageLoading] = useState(false) + const [profileImageUrl, setProfileImageUrl] = useState(user.image || null) + + const handleImageUpload = async (event: React.ChangeEvent) => { + 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 ( + + + + + Profile Image + + + Upload or update your profile picture + + + +
+ + + + {user.name?.charAt(0)?.toUpperCase() || 'U'} + + + +
+
+ + + {profileImageUrl && ( + + )} +
+ +

+ Supported formats: JPEG, PNG, WebP, GIF. Maximum size: 10MB. + Images will be resized to 400x400 pixels. +

+ + +
+
+
+
+ ) +} diff --git a/src/app/settings/settings-form.tsx b/src/app/settings/settings-form.tsx index 2417919..477473d 100644 --- a/src/app/settings/settings-form.tsx +++ b/src/app/settings/settings-form.tsx @@ -1,30 +1,10 @@ "use client" -import { useState, useRef } from "react" import { useSession } from "next-auth/react" -import { zodResolver } from "@hookform/resolvers/zod" -import { useForm } from "react-hook-form" -import { z } from "zod" -import { Eye, EyeOff, Loader2, User, Lock, Save, Camera, Upload, Trash2 } from "lucide-react" -import { toast } from "sonner" - -import { Button } from "@/components/ui/button" -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" -import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form" -import { Input } from "@/components/ui/input" import { Separator } from "@/components/ui/separator" -import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar" -import { updateProfileSchema, updatePasswordSchema, type UpdateProfileInput } from "@/lib/validation" - -const passwordChangeSchema = updatePasswordSchema.extend({ - confirmPassword: z.string() -}).refine((data) => data.newPassword === data.confirmPassword, { - message: "Passwords don't match", - path: ["confirmPassword"], -}) - -type ProfileFormData = UpdateProfileInput -type PasswordFormData = z.infer +import { ProfileForm } from "./profile-form" +import { ProfileImage } from "./profile-image" +import { PasswordForm } from "./password-form" interface SettingsFormProps { user: { @@ -36,163 +16,6 @@ interface SettingsFormProps { export function SettingsForm({ user }: SettingsFormProps) { const { update } = useSession() - const fileInputRef = useRef(null) - const [showCurrentPassword, setShowCurrentPassword] = useState(false) - const [showNewPassword, setShowNewPassword] = useState(false) - const [showConfirmPassword, setShowConfirmPassword] = useState(false) - const [isLoading, setIsLoading] = useState(false) - const [isImageLoading, setIsImageLoading] = useState(false) - const [profileImageUrl, setProfileImageUrl] = useState(user.image || null) - - const profileForm = useForm({ - resolver: zodResolver(updateProfileSchema), - defaultValues: { - name: user.name || "", - email: user.email || "", - }, - }) - - const passwordForm = useForm({ - resolver: zodResolver(passwordChangeSchema), - defaultValues: { - currentPassword: "", - newPassword: "", - confirmPassword: "", - }, - }) - - const onProfileSubmit = async (data: ProfileFormData) => { - setIsLoading(true) - - try { - const response = await fetch("/api/user/profile", { - method: "PATCH", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(data), - }) - - const result = await response.json() - - if (!response.ok) { - toast.error(result.error || "Failed to update profile") - return - } - - // Update the session with new data - await update({ - name: data.name, - email: data.email, - }) - - toast.success("Profile updated successfully!") - - } catch { - toast.error("An unexpected error occurred") - } finally { - setIsLoading(false) - } - } - - const onPasswordSubmit = async (data: PasswordFormData) => { - setIsLoading(true) - - try { - const response = await fetch("/api/user/password", { - method: "PATCH", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - currentPassword: data.currentPassword, - newPassword: data.newPassword, - }), - }) - - const result = await response.json() - - if (!response.ok) { - toast.error(result.error || "Failed to update password") - return - } - - toast.success("Password updated successfully!") - passwordForm.reset() - - } catch { - toast.error("An unexpected error occurred") - } finally { - setIsLoading(false) - } - } - - const handleImageUpload = async (event: React.ChangeEvent) => { - 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!') - - // Update session with new image - await update({ - image: result.profileImage.url - }) - - } catch { - toast.error('An unexpected error occurred') - } finally { - setIsImageLoading(false) - // Reset file input - 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!') - - // Update session to remove image - await update({ - image: null - }) - - } catch { - toast.error('An unexpected error occurred') - } finally { - setIsImageLoading(false) - } - } return (
@@ -204,252 +27,11 @@ export function SettingsForm({ user }: SettingsFormProps) {

- {/* Profile Information */} - - - - - Profile Information - - - Update your personal information - - - -
- - ( - - Full Name - - - - - - )} - /> - ( - - Email Address - - - - - - )} - /> - - - -
-
- + - - {/* Profile Image */} - - - - - Profile Image - - - Upload or update your profile picture - - - -
- - - - {user.name?.charAt(0)?.toUpperCase() || 'U'} - - - -
-
- - - {profileImageUrl && ( - - )} -
- -

- Supported formats: JPEG, PNG, WebP, GIF. Maximum size: 10MB. - Images will be resized to 400x400 pixels. -

- - -
-
-
-
- + - - {/* Password Change */} - - - - - Change Password - - - Update your password to keep your account secure - - - -
- - ( - - Current Password - -
- - -
-
- -
- )} - /> - ( - - New Password - -
- - -
-
- -
- )} - /> - ( - - Confirm New Password - -
- - -
-
- -
- )} - /> -
- Password must contain at least 8 characters with uppercase, lowercase, and a number. -
- - - -
-
+ ) -- cgit v1.3.1