diff --git a/src/routes/trust.index.tsx b/src/routes/trust.index.tsx index 98d9639..d7492cb 100644 --- a/src/routes/trust.index.tsx +++ b/src/routes/trust.index.tsx @@ -312,4 +312,127 @@ function TrustLedgerPage() { ); -} \ No newline at end of file +} +function RecordEntryDialog({ + open, type, clients, onClose, onSaved, +}: { + open: boolean; + type: "deposit" | "withdrawal"; + clients: ClientLite[]; + onClose: () => void; + onSaved: (entry: Entry) => void; +}) { + const { user } = useAuth(); + const [clientId, setClientId] = useState(""); + const [caseId, setCaseId] = useState("_pool"); + const [cases, setCases] = useState<{ id: string; case_number: string | null; title: string | null }[]>([]); + const [date, setDate] = useState(new Date().toISOString().slice(0, 10)); + const [amount, setAmount] = useState(""); + const [note, setNote] = useState(""); + const [saving, setSaving] = useState(false); + + useEffect(() => { + if (!open) { + setClientId(""); setCaseId("_pool"); setCases([]); + setAmount(""); setNote(""); setDate(new Date().toISOString().slice(0, 10)); + } + }, [open]); + + useEffect(() => { + if (!clientId) { setCases([]); return; } + (async () => { + const { data } = await supabase + .from("cases") + .select("id, case_number, title") + .eq("client_id", clientId) + .is("archived_at", null) + .order("opened_at", { ascending: false }); + setCases(data ?? []); + })(); + }, [clientId]); + + const sortedClients = useMemo( + () => [...clients].filter((c) => !c.archived_at).sort((a, b) => (a.name ?? "").localeCompare(b.name ?? "")), + [clients], + ); + + const save = async () => { + if (!clientId) return toast.error("Pick a client"); + const amt = Number(amount); + if (!amt || amt <= 0) return toast.error("Enter a valid amount"); + setSaving(true); + const { data, error } = await supabase + .from("trust_ledger_entries") + .insert({ + client_id: clientId, + case_id: caseId === "_pool" ? null : caseId, + entry_date: date, + entry_type: type, + amount: amt, + note: note || null, + created_by: user?.id ?? null, + }) + .select("*") + .single(); + setSaving(false); + if (error) return toast.error(error.message); + toast.success(`${type === "deposit" ? "Deposit" : "Withdrawal"} recorded`); + onSaved(data as Entry); + }; + + return ( + !v && onClose()}> + + + Record {type} + +
+
+ + +
+
+ + +
+
+
+ + setDate(e.target.value)} /> +
+
+ + setAmount(e.target.value)} /> +
+
+
+ +