"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 ( Renewed : null}> {ADDRESS_OPTIONS[b.addressType].label} {b.domain} {b.addressMonthlyCents ? `${money(b.addressMonthlyCents)} / month` : "Free"}{b.addressCustomCents != null ? " (custom)" : ""} {b.addressType === "included" && {b.domainRenewsOn && day(b.domainRenewsOn)}} {renewalDue(b) &&

{b.domain || "The domain"} is due for renewal on {day(b.domainRenewsOn)}.

}
); } 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(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 (
setForm({ ...form, domain: e.target.value })} placeholder={form.addressType === "crosscode" ? "hopefamily.crosscode.co.za" : "hopefamily.co.za"} />
{form.addressType !== "own_domain" &&
setForm({ ...form, custom: e.target.value })} />
} {form.addressType === "included" &&
setForm({ ...form, domainRenewsOn: e.target.value })} />
}

{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." : ""}

); }