2026-07-03 21:04:43 -05:00
|
|
|
import { useMutation } from '@tanstack/react-query';
|
|
|
|
|
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';
|
refactor(ts): convert all UI primitives + ThemeContext to TSX (B2)
Foundational for phase B: the ui/* primitives were untyped .jsx, and TS infers
React-19 ref-as-prop components as *requiring* a ref, so every consumer errored.
Converted all 22 ui primitives (button, input, card, dialog, select, table,
dropdown-menu, tabs, badge, checkbox, switch, tooltip, separator, label,
Skeleton, collapsible, alert-dialog, sonner, save-status, theme-toggle,
input-dialog, confirm-dialog) using ComponentProps<'el'> / ComponentProps<typeof
Radix.X> + VariantProps for cva components — refs now optional, event params
typed. Also converted ThemeContext.tsx (createContext(null) made useContext's
post-guard return `never`, so setTheme was uncallable — fixed with a typed
context value) and three tracker cells (EditableCell/NotesCell/
LowerThisMonthButton) that consume the branded TrackerRow.
Added a shared errMessage(unknown) helper to utils (strict catch → unknown);
usePaymentActions reuses it. TrackerRow gains monthly_notes + amount_suggestion
(real server fields).
typecheck 0, lint 0 errors (42 warns), build green, 48 client tests. NOTE: the
e2e auth-setup probe is currently red on an unrelated, pre-existing
release-notes-modal dismissal race (reproduces on the last known-good commit;
the a11y snapshot shows these converted primitives rendering correctly) —
investigating separately.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 22:57:22 -05:00
|
|
|
import { fmt, errMessage } from '@/lib/utils';
|
2026-07-03 21:47:12 -05:00
|
|
|
import type { Dollars } from '@/lib/money';
|
2026-07-03 21:04:43 -05:00
|
|
|
import { useInvalidateTrackerData } from '@/hooks/useQueries';
|
|
|
|
|
|
2026-07-03 21:47:12 -05:00
|
|
|
/** The bits of a tracker row these payment actions need. */
|
|
|
|
|
interface PayableRow {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:04:43 -05:00
|
|
|
// Quick-pay a tracker row (record a payment for `val` on `paidDate`) with a
|
|
|
|
|
// reversible Undo toast. A React Query mutation: `isPending` comes for free and
|
|
|
|
|
// the tracker/badge caches are invalidated on settle. Shared by the desktop and
|
|
|
|
|
// mobile tracker rows so the flow lives in one place.
|
|
|
|
|
export function useQuickPay() {
|
|
|
|
|
const invalidate = useInvalidateTrackerData();
|
|
|
|
|
const mutation = useMutation({
|
2026-07-03 21:47:12 -05:00
|
|
|
mutationFn: (payload: { bill_id: number; amount: number; paid_date: string }) => api.quickPay(payload),
|
2026-07-03 21:04:43 -05:00
|
|
|
onSettled: () => invalidate(),
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-03 21:47:12 -05:00
|
|
|
const run = (row: PayableRow, val: Dollars, paidDate: string) => mutation.mutate(
|
2026-07-03 21:04:43 -05:00
|
|
|
{ bill_id: row.id, amount: val, paid_date: paidDate },
|
|
|
|
|
{
|
|
|
|
|
onSuccess: (payment) => {
|
|
|
|
|
toast.success(`${row.name} — ${fmt(val)} paid`, {
|
|
|
|
|
action: {
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
onClick: async () => {
|
|
|
|
|
try {
|
|
|
|
|
await api.deletePayment(payment.id);
|
|
|
|
|
toast.success('Payment removed');
|
|
|
|
|
invalidate();
|
|
|
|
|
} catch (err) {
|
2026-07-03 21:47:12 -05:00
|
|
|
toast.error(errMessage(err, 'Failed to undo payment.'));
|
2026-07-03 21:04:43 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
},
|
2026-07-03 21:47:12 -05:00
|
|
|
onError: (err) => toast.error(errMessage(err, 'Failed to add payment.')),
|
2026-07-03 21:04:43 -05:00
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return { run, isPending: mutation.isPending };
|
|
|
|
|
}
|
2026-07-03 21:18:18 -05:00
|
|
|
|
2026-07-03 21:47:12 -05:00
|
|
|
interface TogglePaidOptions {
|
|
|
|
|
wasPaid: boolean;
|
|
|
|
|
threshold: Dollars;
|
|
|
|
|
setOptimisticPaid: (value: boolean | undefined) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-03 21:18:18 -05:00
|
|
|
// Toggle a tracker row paid/unpaid. The caller owns the instant optimistic flip
|
|
|
|
|
// (its local `setOptimisticPaid`) so the row updates immediately; this hook rolls
|
|
|
|
|
// it back on error, invalidates the tracker/badge caches on settle, and shows the
|
|
|
|
|
// right toast (an Undo when un-paying, a specific "paid" message otherwise).
|
|
|
|
|
// Shared by the desktop and mobile tracker rows.
|
2026-07-03 21:47:12 -05:00
|
|
|
export function useTogglePaid(year: number, month: number) {
|
2026-07-03 21:18:18 -05:00
|
|
|
const invalidate = useInvalidateTrackerData();
|
|
|
|
|
const mutation = useMutation({
|
2026-07-03 21:47:12 -05:00
|
|
|
mutationFn: ({ rowId, amount }: { rowId: number; amount: number | undefined }) =>
|
|
|
|
|
api.togglePaid(rowId, { amount, year, month }),
|
2026-07-03 21:18:18 -05:00
|
|
|
onSettled: () => invalidate(),
|
|
|
|
|
});
|
|
|
|
|
|
2026-07-03 21:47:12 -05:00
|
|
|
const run = (row: PayableRow, { wasPaid, threshold, setOptimisticPaid }: TogglePaidOptions) => {
|
2026-07-03 21:18:18 -05:00
|
|
|
setOptimisticPaid(!wasPaid); // instant flip; effect/rollback reconciles
|
|
|
|
|
mutation.mutate(
|
|
|
|
|
{ rowId: row.id, amount: wasPaid ? undefined : threshold },
|
|
|
|
|
{
|
|
|
|
|
onSuccess: (result) => {
|
|
|
|
|
if (wasPaid && result.paymentId) {
|
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
|
|
|
const paymentId = result.paymentId; // capture the narrowed id for the deferred Undo closure
|
2026-07-03 21:18:18 -05:00
|
|
|
toast.success('Payment moved to recovery', {
|
|
|
|
|
action: {
|
|
|
|
|
label: 'Undo',
|
|
|
|
|
onClick: async () => {
|
|
|
|
|
try {
|
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
|
|
|
await api.restorePayment(paymentId);
|
2026-07-03 21:18:18 -05:00
|
|
|
toast.success('Payment restored');
|
|
|
|
|
invalidate();
|
|
|
|
|
} catch (err) {
|
2026-07-03 21:47:12 -05:00
|
|
|
toast.error(errMessage(err, 'Failed to restore payment'));
|
2026-07-03 21:18:18 -05:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} else if (!wasPaid) {
|
|
|
|
|
toast.success(`${row.name} — ${fmt(threshold)} paid`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onError: (err) => {
|
|
|
|
|
setOptimisticPaid(undefined); // roll back the instant flip
|
2026-07-03 21:47:12 -05:00
|
|
|
toast.error(errMessage(err, 'Failed to toggle payment status'));
|
2026-07-03 21:18:18 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { run, isPending: mutation.isPending };
|
|
|
|
|
}
|