"use client"; import React, { useState } from "react"; import { api, errorText } from "@/lib/api"; import { PRODUCT_NAMES, type Product } from "@/lib/products"; import type { Licence } from "@/lib/types"; import { FormError, Modal } from "@/components/ui"; import { ModalButtons } from "./ReadCard"; import { endOfDay, type Done } from "./shared"; /** A new licence for a product the church doesn't have yet. Accounts comes free with the first other one. */ export function AddProductModal({ customerId, available, onClose, onDone }: { customerId: string; available: Product[]; onClose: () => void; onDone: Done }) { const form0 = { product: available.find(p => p !== "accounts") || available[0], expiresAt: "", graceDays: "14" }; 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 { const l = await api(`/api/admin/customers/${customerId}/licences`, { method: "POST", body: { product: form.product, expiresAt: endOfDay(form.expiresAt), graceDays: form.graceDays }, }); onDone({ type: "ok", text: `${PRODUCT_NAMES[l.product]} licence ${l.key} created. Send it to the church for their setup.` + (l.accountsLicence ? ` Their Accounts licence (free) was created too: ${l.accountsLicence.key}.` : ""), }); } catch (err) { setError(errorText(err)); } finally { setBusy(false); } }; return (
setForm({ ...form, expiresAt: e.target.value })} />
setForm({ ...form, graceDays: e.target.value })} />

One licence per product (revoke one to replace it). Accounts is free and is created with the first other product.

); }