import { useState, useEffect } from "react"; import { KeyRound, UserPen } from "lucide-react"; export default function Settings() { const [username, setUsername] = useState(""); const [newUsername, setNewUsername] = useState(""); const [usernameError, setUsernameError] = useState(""); const [usernameSuccess, setUsernameSuccess] = useState(false); const [usernameLoading, setUsernameLoading] = useState(false); const [pwForm, setPwForm] = useState({ current_password: "", new_password: "", confirm_password: "", }); const [pwError, setPwError] = useState(""); const [pwSuccess, setPwSuccess] = useState(false); const [pwLoading, setPwLoading] = useState(false); useEffect(() => { fetch("/api/auth/me") .then((r) => r.json()) .then((d) => { setUsername(d.username); setNewUsername(d.username); }); }, []); const saveUsername = async (e: React.FormEvent) => { e.preventDefault(); setUsernameError(""); setUsernameSuccess(false); if (newUsername.trim() === username) return; setUsernameLoading(true); const res = await fetch("/api/auth/change-username", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ new_username: newUsername.trim() }), }); setUsernameLoading(false); if (res.ok) { setUsername(newUsername.trim()); setUsernameSuccess(true); } else { const data = await res.json(); setUsernameError(data.error || "Failed to update username"); } }; const savePassword = async (e: React.FormEvent) => { e.preventDefault(); setPwError(""); setPwSuccess(false); if (pwForm.new_password !== pwForm.confirm_password) { setPwError("New passwords do not match"); return; } setPwLoading(true); const res = await fetch("/api/auth/change-password", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ current_password: pwForm.current_password, new_password: pwForm.new_password, }), }); setPwLoading(false); if (res.ok) { setPwSuccess(true); setPwForm({ current_password: "", new_password: "", confirm_password: "" }); } else { const data = await res.json(); setPwError(data.error || "Failed to change password"); } }; return (