diff --git a/src/components/BudgetEditor.tsx b/src/components/BudgetEditor.tsx index 64fe9f1..f0c445e 100644 --- a/src/components/BudgetEditor.tsx +++ b/src/components/BudgetEditor.tsx @@ -93,9 +93,10 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in // Fills main categories that have no budget yet; amounts you set stay. const suggestion = cats.filter((c) => c.main && !(c.id in budgets)).sort((a, b) => b.suggested - a.suggested); - const useSuggested = () => { + // Saves exactly what the suggestion list shows after any edits. + const useSuggested = (picked: Record) => { setSheet(null); - apply(Object.fromEntries(suggestion.map((c) => [c.id, c.suggested])), `Added ${suggestion.length} budgets`); + apply(picked, `Added ${Object.keys(picked).length} budgets`); }; const sheets = ( @@ -122,7 +123,7 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in c.label)} onDone={(id, label, amount) => { setSheet(null); setBudgets((x) => ({ ...x, [id]: amount })); setFresh(id); toast(`Added ${label}`); }} /> setSheet(null)} title="Suggested budgets"> - + c.id).join()} cats={suggestion} open={open} income={0} basis={basis} onUse={useSuggested} disabled={error === SETUP} sticky /> ); @@ -133,9 +134,9 @@ export function BudgetEditor({ basis, cats: all, initial, year, range, today, in

Set a budget from how you really spend

-

{basis} Use them as they are, or change any one after.

+

{basis} Press an amount to change it. Remove any you don't want, or add another.

- setSheet("add")}>Add one at a time} /> + setSheet("add")}>Add one at a time} /> {error &&
} {sheets}
@@ -355,16 +356,6 @@ function BudgetTable({ cats, open, budgets, total, spent, past, apply, onAdd }: cats: BudgetCat[]; open: BudgetCat[]; budgets: Record; total: number; spent: number; past: boolean; apply: (c: Changes, done?: string) => void; onAdd: (c: BudgetCat, n: number) => void; }) { const { money0, symbol } = useMoney(); - const [pick, setPick] = useState(""); - const [amount, setAmount] = useState(""); - const chosen = open.find((c) => c.id === pick); - const n = Math.round(Number(amount)); - const add = (e: React.FormEvent) => { - e.preventDefault(); - if (!chosen || !amount.trim() || !(n >= 0)) return; - onAdd(chosen, n); - setPick(""); setAmount(""); - }; const num = "px-3 py-2.5 text-right tabular whitespace-nowrap"; const extra = "hidden @2xl:table-cell"; const wide = "hidden @lg:table-cell"; // phones keep Budget and Left @@ -416,24 +407,41 @@ function BudgetTable({ cats, open, budgets, total, spent, past, apply, onAdd }: - {!!open.length && ( -
-
- ({ value: c.id, label: c.label }))} - onChange={(id) => { setPick(id); setAmount(String(open.find((c) => c.id === id)?.suggested || "")); }} /> -
- - -
- )} + ); } -function AmountCell({ label, value, symbol, onSave }: { label: string; value: number; symbol: string; onSave: (n: number) => void }) { +// Pick a category, type an amount, Add. Picking fills in the suggested amount. +function AddBudget({ open, onAdd, className }: { open: BudgetCat[]; onAdd: (c: BudgetCat, n: number) => void; className: string }) { + const { symbol } = useMoney(); + const [pick, setPick] = useState(""); + const [amount, setAmount] = useState(""); + const chosen = open.find((c) => c.id === pick); + const n = Math.round(Number(amount)); + const add = (e: React.FormEvent) => { + e.preventDefault(); + if (!chosen || !amount.trim() || !(n >= 0)) return; + onAdd(chosen, n); + setPick(""); setAmount(""); + }; + if (!open.length) return null; + return ( +
+
+ ({ value: c.id, label: c.label }))} + onChange={(id) => { setPick(id); setAmount(String(open.find((c) => c.id === id)?.suggested || "")); }} /> +
+ + +
+ ); +} + +function AmountCell({ label, value, symbol, onSave, onType }: { label: string; value: number; symbol: string; onSave: (n: number) => void; onType?: (n: number) => void }) { const [v, setV] = useState(String(value)); const commit = () => { const n = Math.round(Number(v)); @@ -443,12 +451,16 @@ function AmountCell({ label, value, symbol, onSave }: { label: string; value: nu return ( ); } @@ -547,29 +559,33 @@ function Editor({ label, value, onSave, onCancel, onDelete }: { label: string; v ); } -// Preview of the suggested budgets: the usual month per category. -function Suggest({ cats, income, basis, onUse, disabled, alt, sticky }: { cats: BudgetCat[]; income: number; basis: string; onUse: () => void; disabled: boolean; alt?: React.ReactNode; sticky?: boolean }) { - const { money0 } = useMoney(); - const total = cats.reduce((s, c) => s + c.suggested, 0); +// The suggested budgets, the usual month per category, as a draft: change an amount, remove a row or add a category +// before saving. Nothing saves until Use these, which saves exactly the rows shown. +function Suggest({ cats, open, income, basis, onUse, disabled, alt, sticky }: { cats: BudgetCat[]; open: BudgetCat[]; income: number; basis: string; onUse: (picked: Record) => void; disabled: boolean; alt?: React.ReactNode; sticky?: boolean }) { + const { money0, symbol } = useMoney(); + const [draft, setDraft] = useState>(() => Object.fromEntries(cats.map((c) => [c.id, c.suggested]))); + const set = (id: string, n: number | null) => setDraft((d) => merge(d, { [id]: n })); + const rows = Object.keys(draft).map((id) => open.find((c) => c.id === id)).filter((c) => !!c); + const total = Object.values(draft).reduce((s, n) => s + n, 0); if (!cats.length) return

Nothing to suggest right now.

{alt}
; return ( <>
    - {cats.map((c) => ( -
  • + {rows.map((c) => ( +
  • {c.label} - - {money0(c.suggested)} - + set(c.id, n)} onType={(n) => set(c.id, n)} /> +
  • ))}
+ !(c.id in draft))} onAdd={(c, n) => set(c.id, n)} className="px-5 md:px-8 pt-3" />

{basis} Never less than you've already spent this month.

{income > 0 &&

You bring in about {money0(income)} a month. {income >= total ? `This leaves ${money0(income - total)} to save.` : `You're spending ${money0(total - income)} more than you earn.`}

}
- + {alt}