2026-05-31 15:06:10 -05:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { toast } from 'sonner';
|
refactor(ts): convert the API client to TypeScript (TS9)
client/api.js -> api.ts. Types the fetch infrastructure: generic _fetch<T>
and get/post/put/patch/del<T> helpers, an ApiError interface (status/code/
details/data), a QueryParams type for the query-string builder, and File-typed
upload helpers. Endpoint response shapes are typed incrementally — most methods
default to Promise<unknown>; the ones consumed by typed .ts files get real
shapes (quickPay -> PaymentRecord, togglePaid -> TogglePaidResult, settings ->
settings map).
Typing togglePaid's result surfaced a latent narrowing bug in usePaymentActions:
the un-pay Undo closure read result.paymentId (number|undefined) inside a
deferred async callback where TS re-widens the if-narrowed value — fixed by
capturing the id in a const before the closure.
The 16 files importing '@/api.js' with an explicit extension were normalized to
'@/api' so Vite/TS resolve the .ts.
Verified: typecheck 0, lint 0 errors (47 warns), build green, 48 client tests,
and 17/17 e2e probe (every page renders, all API paths respond) — the central
fetch-module rename is runtime-safe.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:09:04 -05:00
|
|
|
import { api } from '@/api';
|
2026-05-31 15:06:10 -05:00
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
|
|
|
|
|
// Monthly notes stored in monthly_bill_state — per-month, not per-bill.
|
|
|
|
|
export function NotesCell({ row, refresh }) {
|
|
|
|
|
const savedNote = row.monthly_notes || '';
|
|
|
|
|
const [value, setValue] = useState(savedNote);
|
|
|
|
|
const [saving, setSaving] = useState(false);
|
|
|
|
|
|
|
|
|
|
async function handleBlur() {
|
|
|
|
|
const trimmed = value.trim();
|
|
|
|
|
if (trimmed === savedNote) return;
|
|
|
|
|
|
|
|
|
|
const year = row.year;
|
|
|
|
|
const month = row.month;
|
|
|
|
|
|
|
|
|
|
if (!year || !month) {
|
|
|
|
|
toast.error('Cannot save notes without year/month context');
|
|
|
|
|
setValue(savedNote);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSaving(true);
|
|
|
|
|
try {
|
|
|
|
|
await api.saveBillMonthlyState(row.id, {
|
|
|
|
|
year,
|
|
|
|
|
month,
|
|
|
|
|
notes: trimmed || null,
|
|
|
|
|
is_skipped: row.is_skipped,
|
|
|
|
|
actual_amount: row.actual_amount,
|
|
|
|
|
});
|
|
|
|
|
refresh();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
toast.error(err.message);
|
|
|
|
|
setValue(savedNote);
|
|
|
|
|
} finally { setSaving(false); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={e => setValue(e.target.value)}
|
|
|
|
|
onBlur={handleBlur}
|
|
|
|
|
onKeyDown={e => { if (e.key === 'Enter') e.currentTarget.blur(); }}
|
|
|
|
|
placeholder='Add monthly notes…'
|
|
|
|
|
disabled={saving}
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full bg-transparent text-sm placeholder:text-muted-foreground/40',
|
|
|
|
|
'border-0 outline-none ring-0',
|
|
|
|
|
'text-muted-foreground focus:text-foreground',
|
|
|
|
|
'transition-colors duration-150',
|
|
|
|
|
'disabled:cursor-not-allowed disabled:opacity-40',
|
|
|
|
|
value && 'text-foreground/80',
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|