import { useMutation } from "@tanstack/react-query"; import { KeyRound, User } from "lucide-react"; import { useState } from "react"; import { useNavigate } from "react-router"; import { toast } from "sonner"; import { changePasswordMutation, logoutMutation } from "~/api-client/@tanstack/react-query.gen"; import { Button } from "~/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "~/components/ui/card"; import { Input } from "~/components/ui/input"; import { Label } from "~/components/ui/label"; import { appContext } from "~/context"; import type { Route } from "./+types/settings"; export function meta(_: Route.MetaArgs) { return [ { title: "Settings - Ironmount" }, { name: "description", content: "Manage your account settings and preferences.", }, ]; } export async function clientLoader({ context }: Route.LoaderArgs) { const ctx = context.get(appContext); return ctx; } export default function Settings({ loaderData }: Route.ComponentProps) { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const navigate = useNavigate(); const logout = useMutation({ ...logoutMutation(), onSuccess: () => { navigate("/login", { replace: true }); }, }); const changePassword = useMutation({ ...changePasswordMutation(), onSuccess: (data) => { if (data.success) { toast.success("Password changed successfully. You will be logged out."); setTimeout(() => { logout.mutate({}); }, 1500); } else { toast.error("Failed to change password", { description: data.message }); } }, onError: (error) => { toast.error("Failed to change password", { description: error.message }); }, }); const handleChangePassword = (e: React.FormEvent) => { e.preventDefault(); if (newPassword !== confirmPassword) { toast.error("Passwords do not match"); return; } if (newPassword.length < 8) { toast.error("Password must be at least 8 characters long"); return; } changePassword.mutate({ body: { currentPassword, newPassword, }, }); }; return (
Account Information Your account details
Change Password Update your password to keep your account secure
setCurrentPassword(e.target.value)} className="max-w-md" required />
setNewPassword(e.target.value)} className="max-w-md" required minLength={8} />

Must be at least 8 characters long

setConfirmPassword(e.target.value)} className="max-w-md" required minLength={8} />
); }