Files
events-hub/frontend/src/components/billing/OneOffInvoice.tsx
T
joshuaandClaude Opus 5.5 97ae79f4c2 Hub 2.16.0: popups for every add and edit on the list pages
- Add server, Add customer, New install and Link existing open in popups (Modal from 2.14.0):
  ask before discarding, error by the button; saving still opens the new page.
- Add server: the join command in the same popup; closing before Copy asks first (Modal's
  `dirty` can now be the question to ask).
- Notices and Releases: one popup for new and edit (NoticeModal, ReleaseModal); the cards are read
  views. Release sources: a table with Sync now and Use the defaults; Edit opens the rows and the
  access token in a popup and saving syncs.
- One-off invoice on Billing is the OneOffInvoice popup with a church picker; NewInvoice goes and
  /billing/invoices/new opens the popup (/billing?oneoff=1).
- These pages use PageMessage. Frontend only: no migration, no API changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 12:34:22 +02:00

56 lines
3.4 KiB
TypeScript

"use client";
import React, { useEffect, useState } from "react";
import { api, errorText } from "@/lib/api";
import { todayYmd, type Invoice } from "@/lib/billing";
import { FormError, Modal } from "@/components/ui";
import { LinesEditor, editTotal, fromEdit, type EditLine } from "./LinesEditor";
/**
* Hub 2.14.0: a one-off invoice (standby support, set-up…), made as a draft, in a popup. On the
* customer page it's for that church; without `customerId` (Billing, 2.16.0) the church is chosen.
*/
export function OneOffInvoice({ customerId, onClose, onSaved }: { customerId?: string; onClose: () => void; onSaved: (inv: Invoice) => void }) {
const [form, setForm] = useState({ customerId: customerId || "", issueDate: todayYmd(), notes: "" });
const [lines, setLines] = useState<EditLine[]>([{ description: "", quantity: "1", unit: "" }]);
const [customers, setCustomers] = useState<{ id: string; name: string }[]>([]);
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const dirty = !!form.notes || fromEdit(lines).length > 0 || (!customerId && !!form.customerId);
useEffect(() => {
if (!customerId) api<{ id: string; name: string }[]>("/api/admin/billing/customers").then(setCustomers).catch(e => setError(errorText(e)));
}, [customerId]);
const save = async (e: React.FormEvent) => {
e.preventDefault();
setBusy(true);
setError(null);
try {
onSaved(await api<Invoice>("/api/admin/billing/invoices", { method: "POST", body: { ...form, lines: fromEdit(lines) } }));
} catch (err) { setError(errorText(err)); } finally { setBusy(false); }
};
return (
<Modal title="One-off invoice" onClose={onClose} dirty={dirty} wide>
<form onSubmit={save} className="space-y-4">
<p className="text-sm text-slate-600">For anything outside the licence, like standby support for a big event. It&apos;s made as a draft for you to approve.</p>
{!customerId && (
<div className="max-w-sm">
<label className="label" htmlFor="oo-customer">Church</label>
<select id="oo-customer" className="input" value={form.customerId} onChange={e => setForm({ ...form, customerId: e.target.value })} required>
<option value="">Choose…</option>
{customers.map(c => <option key={c.id} value={c.id}>{c.name}</option>)}
</select>
</div>
)}
<div className="max-w-xs"><label className="label" htmlFor="oo-date">Invoice date</label><input id="oo-date" type="date" className="input" value={form.issueDate} onChange={e => setForm({ ...form, issueDate: e.target.value })} required /><p className="mt-1 text-xs text-slate-500">It goes out on this day once approved.</p></div>
<LinesEditor lines={lines} onChange={setLines} />
<div><label className="label" htmlFor="oo-notes">Notes (printed on the invoice)</label><textarea id="oo-notes" className="input" rows={2} value={form.notes} onChange={e => setForm({ ...form, notes: e.target.value })} /></div>
<FormError error={error} />
<div className="flex flex-wrap gap-2">
<button className="btn-primary" disabled={busy || (editTotal(lines) === 0 && !fromEdit(lines).length)}>{busy ? "Saving…" : "Create draft"}</button>
<button type="button" className="btn-secondary" onClick={onClose}>Cancel</button>
</div>
</form>
</Modal>
);
}