- Customer page: header tiles (products, owing, next invoice, address, AI) and three tabs (Overview, Products, Invoices and payments), the tab in the URL; #billing links open Invoices and payments and Billing's links use ?tab=invoices. - Every add or edit is a popup (new Modal in components/ui.tsx: Esc / click outside, asks before discarding changes, focus kept inside, full screen on a phone). The page shows only read views. Record payment is a popup on Billing and the invoice page too. - One card per product: licence and billing fields in one Edit with one Save (each endpoint called only if its fields changed); suspend/revoke/reactivate/release in a ⋯ menu. Contact and billing details edited together. One page-level message. - History as one timeline with a filter; each kind on its own keeps its sortable table. - CustomerBilling.tsx split into components/customer/; plan in docs/CUSTOMER-PAGE-PLAN.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
74 lines
5.0 KiB
TypeScript
74 lines
5.0 KiB
TypeScript
"use client";
|
|
|
|
import React, { useState } from "react";
|
|
import { api, errorText } from "@/lib/api";
|
|
import { centsFrom, day, money, randInput } from "@/lib/billing";
|
|
import { FormError, Modal } from "@/components/ui";
|
|
import { Field, Fields, ModalButtons, ReadCard } from "./ReadCard";
|
|
import { ADDRESS_OPTIONS, changed, type AddressType, type CustomerBillingData, type Done } from "./shared";
|
|
|
|
/** Whether the included domain is due for renewal within 30 days. */
|
|
const renewalDue = (b: CustomerBillingData) => b.addressType === "included" && !!b.domainRenewsOn && b.domainRenewsOn <= new Date(Date.now() + 30 * 864e5).toISOString().slice(0, 10);
|
|
|
|
/**
|
|
* The church's address: one for all its products, billed once per invoice and never discounted.
|
|
* The popup is opened from here or from the Address tile in the header, so the page holds it.
|
|
*/
|
|
export function AddressCard({ b, onEdit, onDone }: { b: CustomerBillingData; onEdit: () => void; onDone: Done }) {
|
|
const renewed = async () => {
|
|
if (!confirm(`Mark ${b.domain || "the domain"} as renewed? The renewal date moves on a year.`)) return;
|
|
try {
|
|
const r = await api<{ message: string }>(`/api/admin/billing/customers/${b.id}/domain-renewed`, { method: "POST" });
|
|
onDone({ type: "ok", text: r.message });
|
|
} catch (err) { onDone({ type: "err", text: errorText(err) }); }
|
|
};
|
|
return (
|
|
<ReadCard title="Address" onEdit={onEdit}
|
|
actions={b.addressType === "included" && b.domainRenewsOn ? <button type="button" className="btn-secondary px-2.5 py-1.5" onClick={renewed}>Renewed</button> : null}>
|
|
<Fields>
|
|
<Field label="Type">{ADDRESS_OPTIONS[b.addressType].label}</Field>
|
|
<Field label="Domain">{b.domain}</Field>
|
|
<Field label="Price">{b.addressMonthlyCents ? `${money(b.addressMonthlyCents)} / month` : "Free"}{b.addressCustomCents != null ? " (custom)" : ""}</Field>
|
|
{b.addressType === "included" && <Field label="Domain renews on">{b.domainRenewsOn && day(b.domainRenewsOn)}</Field>}
|
|
</Fields>
|
|
{renewalDue(b) && <p className="rounded-lg bg-amber-50 p-2 text-sm text-amber-900">{b.domain || "The domain"} is due for renewal on {day(b.domainRenewsOn)}.</p>}
|
|
</ReadCard>
|
|
);
|
|
}
|
|
|
|
export function AddressModal({ b, onClose, onDone }: { b: CustomerBillingData; onClose: () => void; onDone: Done }) {
|
|
const form0 = { addressType: b.addressType, domain: b.domain || "", custom: randInput(b.addressCustomCents), domainRenewsOn: b.domainRenewsOn || "" };
|
|
const [form, setForm] = useState(form0);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const save = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await api(`/api/admin/billing/customers/${b.id}`, { method: "PUT", body: { addressType: form.addressType, domain: form.domain || null, addressCustomCents: form.custom.trim() ? centsFrom(form.custom) : null, domainRenewsOn: form.domainRenewsOn || null } });
|
|
onDone({ type: "ok", text: `Address saved: ${ADDRESS_OPTIONS[form.addressType].label}.` });
|
|
} catch (err) { setError(errorText(err)); } finally { setBusy(false); }
|
|
};
|
|
return (
|
|
<Modal title="Address" onClose={onClose} dirty={changed(form, form0)}>
|
|
<form onSubmit={save} className="space-y-3">
|
|
<div className="grid gap-3 sm:grid-cols-2">
|
|
<div>
|
|
<label className="label" htmlFor="addr-type">Address</label>
|
|
<select id="addr-type" className="input" value={form.addressType} onChange={e => setForm({ ...form, addressType: e.target.value as AddressType })}>
|
|
{(Object.keys(ADDRESS_OPTIONS) as AddressType[]).map(k => <option key={k} value={k}>{ADDRESS_OPTIONS[k].label} ({ADDRESS_OPTIONS[k].price})</option>)}
|
|
</select>
|
|
</div>
|
|
<div><label className="label" htmlFor="addr-domain">Domain</label><input id="addr-domain" className="input" value={form.domain} onChange={e => setForm({ ...form, domain: e.target.value })} placeholder={form.addressType === "crosscode" ? "hopefamily.crosscode.co.za" : "hopefamily.co.za"} /></div>
|
|
{form.addressType !== "own_domain" && <div><label className="label" htmlFor="addr-price">Custom price (R/month)</label><input id="addr-price" className="input" inputMode="decimal" placeholder={ADDRESS_OPTIONS[form.addressType].price} value={form.custom} onChange={e => setForm({ ...form, custom: e.target.value })} /></div>}
|
|
{form.addressType === "included" && <div><label className="label" htmlFor="addr-renews">Domain renews on</label><input id="addr-renews" type="date" className="input" value={form.domainRenewsOn} onChange={e => setForm({ ...form, domainRenewsOn: e.target.value })} /></div>}
|
|
</div>
|
|
<p className="text-xs text-slate-500">{ADDRESS_OPTIONS[form.addressType].help} Charged once per invoice for all the church's products, never discounted.{form.addressType === "included" ? " You're emailed 30 days before the renewal date." : ""}</p>
|
|
<FormError error={error} />
|
|
<ModalButtons busy={busy} onCancel={onClose} />
|
|
</form>
|
|
</Modal>
|
|
);
|
|
}
|