diff --git a/teressa-copeland-homes/src/app/portal/(protected)/documents/[docId]/_components/PreparePanel.tsx b/teressa-copeland-homes/src/app/portal/(protected)/documents/[docId]/_components/PreparePanel.tsx index bd50233..10022a4 100644 --- a/teressa-copeland-homes/src/app/portal/(protected)/documents/[docId]/_components/PreparePanel.tsx +++ b/teressa-copeland-homes/src/app/portal/(protected)/documents/[docId]/_components/PreparePanel.tsx @@ -3,102 +3,51 @@ import { useState } from 'react'; import { useRouter } from 'next/navigation'; import { TextFillForm } from './TextFillForm'; -interface Client { id: string; name: string; email: string; } - interface PreparePanelProps { docId: string; - clients: Client[]; - /** The client already explicitly assigned to this document (null if not yet assigned). */ - assignedClientId: string | null; - /** The client the document belongs to — used as the default selection when no explicit assignedClientId. */ - defaultClientId: string; + defaultEmail: string; + clientName: string; currentStatus: string; } -/** - * Parse a comma/newline-separated string of email addresses into a trimmed, deduplicated array. - * Empty tokens are filtered out. - */ function parseEmails(raw: string): string[] { - return raw - .split(/[\n,]+/) - .map((e) => e.trim()) - .filter(Boolean); + return raw.split(/[\n,]+/).map((e) => e.trim()).filter(Boolean); } -/** Very light email format check — catches obvious mistakes without a full RFC 5322 parse. */ function isValidEmail(email: string): boolean { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); } -export function PreparePanel({ - docId, - clients, - assignedClientId, - defaultClientId, - currentStatus, -}: PreparePanelProps) { +export function PreparePanel({ docId, defaultEmail, clientName, currentStatus }: PreparePanelProps) { const router = useRouter(); - - // selectedClientId: id from the dropdown (empty string = "pick manually by email") - const [selectedClientId, setSelectedClientId] = useState( - assignedClientId ?? defaultClientId, - ); - - // primaryEmail: editable email for the assigned client (pre-filled, can be overridden) - const assignedClient = clients.find(c => c.id === assignedClientId); - const [primaryEmail, setPrimaryEmail] = useState(assignedClient?.email ?? ''); - - // emailInput: raw text for manual email entry (used when selectedClientId is '' or - // as additional CC addresses) - const [emailInput, setEmailInput] = useState(''); - + const [recipients, setRecipients] = useState(defaultEmail); const [textFillData, setTextFillData] = useState>({}); const [loading, setLoading] = useState(false); const [result, setResult] = useState<{ ok: boolean; message: string } | null>(null); - // Don't show the panel if already sent/signed - const canPrepare = currentStatus === 'Draft'; - - // The email addresses that will be sent with the prepare request. - // When an assigned client exists, use the (editable) primaryEmail. - // Otherwise use the dropdown-selected client's email. - // Any manually-entered addresses are appended. - function buildEmailAddresses(): string[] { - const addresses: string[] = []; - - if (assignedClientId && primaryEmail.trim()) { - addresses.push(primaryEmail.trim()); - } else if (selectedClientId) { - const client = clients.find((c) => c.id === selectedClientId); - if (client?.email) addresses.push(client.email); - } - - const extras = parseEmails(emailInput); - for (const e of extras) { - if (!addresses.includes(e)) addresses.push(e); - } - - return addresses; + if (currentStatus !== 'Draft') { + return ( +
+ Document status is {currentStatus} — preparation is only available for Draft documents. +
+ ); } async function handlePrepare() { setLoading(true); setResult(null); - const emailAddresses = buildEmailAddresses(); + const emailAddresses = parseEmails(recipients); - // Require at least one email address if (emailAddresses.length === 0) { - setResult({ ok: false, message: 'Please select a client or enter at least one email address.' }); + setResult({ ok: false, message: 'Enter at least one recipient email.' }); setLoading(false); return; } - // Validate all addresses const invalid = emailAddresses.filter((e) => !isValidEmail(e)); if (invalid.length > 0) { - setResult({ ok: false, message: `Invalid email address(es): ${invalid.join(', ')}` }); + setResult({ ok: false, message: `Invalid email(s): ${invalid.join(', ')}` }); setLoading(false); return; } @@ -107,18 +56,14 @@ export function PreparePanel({ const res = await fetch(`/api/documents/${docId}/prepare`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - textFillData, - assignedClientId: selectedClientId || null, - emailAddresses, - }), + body: JSON.stringify({ textFillData, emailAddresses }), }); if (!res.ok) { const err = await res.json().catch(() => ({ error: 'Unknown error' })); setResult({ ok: false, message: err.error ?? 'Prepare failed' }); } else { - setResult({ ok: true, message: 'Document prepared successfully. Status updated to Sent.' }); - router.refresh(); // Update the page to reflect new status + setResult({ ok: true, message: 'Document prepared. Status updated to Sent.' }); + router.refresh(); } } catch (e) { setResult({ ok: false, message: String(e) }); @@ -127,71 +72,26 @@ export function PreparePanel({ } } - if (!canPrepare) { - return ( -
- Document status is {currentStatus} — preparation is only available for Draft documents. -
- ); - } - return (

Prepare Document

- {/* Primary recipient */}
- {assignedClientId ? ( - <> - setPrimaryEmail(e.target.value)} - className="w-full border rounded px-2 py-1.5 text-sm" - placeholder="recipient@example.com" - /> -

- Assigned client: {clients.find(c => c.id === assignedClientId)?.name ?? assignedClientId} -

- - ) : ( - - )} -
- - {/* Additional / manual email addresses */} -
-