import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { formatAmount } from "../format.ts"; import { planBalance, setSliceAmount, withPlan, withoutPlan, type PurchaseDraft, } from "../purchaseForm.ts"; import { monthOf } from "../../domain/months.ts"; interface Props { draft: PurchaseDraft; onChange: (next: PurchaseDraft) => void; } export function PlanEditor({ draft, onChange }: Props) { const balance = planBalance(draft); if (!draft.plan) { return ( ); } return (
months { const months = Number(event.target.value); if (months >= 1) { onChange(withPlan(draft, draft.plan!.startMonth, months)); } }} />
{draft.plan.slices.map((slice, index) => (
{slice.month} onChange(setSliceAmount(draft, index, Number(event.target.value) || 0)) } /> {draft.currency}
))}

{balance === 0 ? `Slices total ${formatAmount(draft.amount)} ${draft.currency}, matching the purchase.` : `Slices are ${formatAmount(Math.abs(balance))} ${draft.currency} ${ balance > 0 ? "short of" : "over" } the purchase total.`}

); }