diff --git a/src/app/dashboard/settings/page.tsx b/src/app/dashboard/settings/page.tsx new file mode 100644 index 0000000..01ad166 --- /dev/null +++ b/src/app/dashboard/settings/page.tsx @@ -0,0 +1,207 @@ +"use client"; + +import { useState } from "react"; +import { useForm } from "react-hook-form"; +import { useAuthStore } from "@/store/authStore"; +import toast from "react-hot-toast"; +import { User, Lock, Save } from "lucide-react"; + +interface ProfileForm { + name: string; + email: string; +} + +interface PasswordForm { + currentPassword: string; + newPassword: string; + confirmPassword: string; +} + +export default function SettingsPage() { + const { user, updateUser } = useAuthStore(); + const [activeTab, setActiveTab] = useState<"profile" | "password">("profile"); + + const profileForm = useForm({ + defaultValues: { + name: user?.name ?? "", + email: user?.email ?? "", + }, + }); + + const passwordForm = useForm(); + + const onProfileSubmit = (data: ProfileForm) => { + try { + updateUser(data); + toast.success("Profile updated successfully!"); + } catch { + toast.error("Failed to update profile."); + } + }; + + const onPasswordSubmit = (data: PasswordForm) => { + if (data.newPassword !== data.confirmPassword) { + toast.error("Passwords do not match."); + return; + } + if (data.newPassword.length < 6) { + toast.error("Password must be at least 6 characters."); + return; + } + try { + updateUser({ password: data.newPassword }); + toast.success("Password updated successfully!"); + passwordForm.reset(); + } catch { + toast.error("Failed to update password."); + } + }; + + return ( +
+
+

Settings

+

Manage your account settings.

+
+ +
+
+ +
+ +
+ {activeTab === "profile" && ( +
+
+ + + {profileForm.formState.errors.name && ( +

+ {profileForm.formState.errors.name.message} +

+ )} +
+ +
+ + + {profileForm.formState.errors.email && ( +

+ {profileForm.formState.errors.email.message} +

+ )} +
+ + +
+ )} + + {activeTab === "password" && ( +
+
+ + +
+ +
+ + + {passwordForm.formState.errors.newPassword && ( +

+ {passwordForm.formState.errors.newPassword.message} +

+ )} +
+ +
+ + +
+ + +
+ )} +
+
+
+ ); +}