'use client'; import { useState } from 'react'; import { UserPlus, Loader2, Check } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { apiFetch } from '@/lib/api/client'; interface PortalInviteButtonProps { clientId: string; clientName: string; defaultEmail?: string; } /** * Admin button on the client detail header that creates a portal user for * the client and sends them an activation email. Uses the client's primary * email as the default but lets the admin override. */ export function PortalInviteButton({ clientId, clientName, defaultEmail, }: PortalInviteButtonProps) { const [open, setOpen] = useState(false); const [email, setEmail] = useState(defaultEmail ?? ''); const [name, setName] = useState(clientName); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [success, setSuccess] = useState(false); function reset() { setEmail(defaultEmail ?? ''); setName(clientName); setError(''); setSuccess(false); setLoading(false); } async function submit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(''); try { await apiFetch(`/api/v1/clients/${clientId}/portal-user`, { method: 'POST', body: { email, name }, }); setSuccess(true); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to send invitation'); } finally { setLoading(false); } } return ( <> { if (!o) reset(); setOpen(o); }} > Invite to client portal We'll email an activation link. The client picks their own password from there. {success ? (
Activation email sent to {email}.
) : (
setEmail(e.target.value)} disabled={loading} placeholder="client@example.com" />
setName(e.target.value)} disabled={loading} />
{error &&

{error}

}
)} {success ? ( ) : ( <> )}
); }