import { useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { getMe, getProfile, updateProfile, type ProfileUpdate } from "../api/client"; export default function ProfilePage() { const queryClient = useQueryClient(); const [editing, setEditing] = useState(false); const [error, setError] = useState(null); const { data: user } = useQuery({ queryKey: ["me"], queryFn: getMe }); const { data: profile, isLoading } = useQuery({ queryKey: ["profile"], queryFn: getProfile, }); const [form, setForm] = useState({}); const mutation = useMutation({ mutationFn: updateProfile, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["profile"] }); setEditing(false); setError(null); }, onError: (err: any) => { const detail = err?.response?.data?.detail; if (Array.isArray(detail)) { setError(detail.map((d: any) => d.msg).join("; ")); } else { setError(detail ?? "Failed to save profile"); } }, }); const startEditing = () => { setForm({ phone: profile?.phone ?? "", date_of_birth: profile?.date_of_birth ?? "", position: profile?.position ?? "", address: profile?.address ?? "", }); setError(null); setEditing(true); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); // Send null for empty strings so the backend clears the field const payload: ProfileUpdate = {}; for (const [k, v] of Object.entries(form)) { (payload as any)[k] = v === "" ? null : v; } mutation.mutate(payload); }; if (isLoading) return
Loading…
; return ( <>

Profile

{!editing ? ( <>
) : (
setForm((f) => ({ ...f, position: v }))} /> setForm((f) => ({ ...f, phone: v }))} type="tel" /> setForm((f) => ({ ...f, date_of_birth: v }))} type="date" /> setForm((f) => ({ ...f, address: v }))} /> {error &&

{error}

}
)}
); } function Row({ label, value }: { label: string; value?: string | null }) { return ( {label} {value ?? "—"} ); } function Field({ label, value, onChange, type = "text", }: { label: string; value: string; onChange: (v: string) => void; type?: string; }) { return (
onChange(e.target.value)} style={{ width: "100%", padding: "6px 8px", boxSizing: "border-box" }} />
); }