import { Play } from "lucide-react"; import { useCallback, useEffect, useState } from "react"; import { Button, Dialog, DialogForm, DialogSection, DismissibleAlert, FormField, FormGrid, LoadingIndicator, ToggleSwitch, type PlatformRouteContext } from "@govoplan/core-webui"; import { listAssistedIntakeProfiles, startAssistedFormIntake, type AssistedIntakeContext, type FormIntakeProfile } from "../../api/formsRuntime"; type AssistedIntakeDialogProps = { open: boolean; settings: PlatformRouteContext["settings"]; language: string; onStarted: (instanceId: string) => void; onClose: () => void; }; export default function AssistedIntakeDialog({ open, settings, language, onStarted, onClose }: AssistedIntakeDialogProps) { const [profiles, setProfiles] = useState([]); const [profileId, setProfileId] = useState(""); const [channel, setChannel] = useState("counter"); const [affectedPartyRef, setAffectedPartyRef] = useState(""); const [representedPartyRef, setRepresentedPartyRef] = useState(""); const [authorityBasis, setAuthorityBasis] = useState("self"); const [purpose, setPurpose] = useState(""); const [legalBasisRef, setLegalBasisRef] = useState(""); const [consentBasis, setConsentBasis] = useState(""); const [noticeGiven, setNoticeGiven] = useState(false); const [responsibleFunctionRef, setResponsibleFunctionRef] = useState(""); const [accessibilityNeeds, setAccessibilityNeeds] = useState(""); const [loading, setLoading] = useState(false); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const load = useCallback(async (signal?: AbortSignal) => { setLoading(true); setError(""); try { const result = await listAssistedIntakeProfiles(settings, signal); setProfiles(result.profiles); setProfileId((current) => current || result.profiles[0]?.profile_id || ""); } finally { setLoading(false); } }, [settings]); useEffect(() => { if (!open) return undefined; const controller = new AbortController(); void load(controller.signal).catch((reason) => { if ((reason as Error).name !== "AbortError") { setError(reason instanceof Error ? reason.message : "Assisted intake profiles could not be loaded."); } }); return () => controller.abort(); }, [load, open]); async function start() { if (!valid()) return; setBusy(true); setError(""); try { const result = await startAssistedFormIntake(settings, { profileId, channel, affectedPartyRef: affectedPartyRef.trim(), representedPartyRef: representedPartyRef.trim() || undefined, authorityBasis, purpose: purpose.trim(), legalBasisRef: legalBasisRef.trim() || undefined, consentBasis: consentBasis.trim() || undefined, noticeGiven, responsibleFunctionRef: responsibleFunctionRef.trim(), language: language || "de", accessibilityNeeds: accessibilityNeeds.split(/[,\n]/).map((item) => item.trim()).filter(Boolean) }); if (!result.instance) throw new Error("The assisted session was created without a Form instance."); onStarted(result.instance.instance_id); } catch (reason) { setError(reason instanceof Error ? reason.message : "The assisted intake could not be started."); } finally { setBusy(false); } } function valid() { return Boolean( profileId && affectedPartyRef.trim() && authorityBasis.trim() && purpose.trim() && responsibleFunctionRef.trim() ); } return ( }> {error && {error}} {loading && } {!loading && profiles.length === 0 && No assisted intake profile is enabled. Ask a Forms Runtime administrator to add one for the published Form. } {!loading && profiles.length > 0 && { event.preventDefault(); void start(); }}> setAffectedPartyRef(event.target.value)} disabled={busy} required /> setRepresentedPartyRef(event.target.value)} disabled={busy} /> setResponsibleFunctionRef(event.target.value)} disabled={busy} required /> setPurpose(event.target.value)} disabled={busy} required /> setLegalBasisRef(event.target.value)} disabled={busy} /> setConsentBasis(event.target.value)} disabled={busy} /> setAccessibilityNeeds(event.target.value)} disabled={busy} /> } ); } function profileTitle(profile: FormIntakeProfile): string { const title = profile.metadata.definition_title; return typeof title === "string" && title.trim() ? title : profile.definition_ref.label ?? profile.definition_ref.object_id; }