import { useRef, useState } from "react"; import { Link } from "@inertiajs/react"; import type { FormEvent } from "react"; import { newSubmissionKey } from "@/lib/portal-api"; import { Frame, Field, button, card, input } from "./shared"; type Opportunity = { id: string; slug: string; title: string; sector?: { name: string } | null }; type Props = { record: Opportunity; modalities: string[]; returnUrl?: string }; type Form = { fullName: string; email: string; phone: string; investorType: string; country: string; sectorInterest: string; modality: string; investmentRange: string; message: string; privacyConsent: boolean; shareWithProponent: boolean; }; export default function BusinessInterest({ record, modalities, returnUrl = "/negocios" }: Props) { const [form, setForm] = useState
({ fullName: "", email: "", phone: "", investorType: "empresa", country: "MZ", sectorInterest: record.sector?.name ?? "", modality: modalities[0] ?? "", investmentRange: "", message: "", privacyConsent: false, shareWithProponent: false }); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const [receipt, setReceipt] = useState(""); const key = useRef(newSubmissionKey()); const update = (field: K, value: Form[K]) => setForm((current) => ({ ...current, [field]: value })); const submit = async (event: FormEvent) => { event.preventDefault(); if (!form.privacyConsent) { setError("Aceite o tratamento de dados para enviar o pedido."); return; } setBusy(true); setError(""); const xsrf = decodeURIComponent(document.cookie.split("; ").find((cookie) => cookie.startsWith("XSRF-TOKEN="))?.slice(11) ?? ""); try { const response = await fetch("/api/v1/interest-requests", { method: "POST", credentials: "same-origin", headers: { Accept: "application/json", "Content-Type": "application/json", "X-XSRF-TOKEN": xsrf, "Idempotency-Key": key.current }, body: JSON.stringify({ ...form, existingBusinessId: record.id }), }); const data = await response.json(); if (!response.ok) throw new Error(data.message ?? "Não foi possível enviar a manifestação."); setReceipt(data.data.reference); key.current = newSubmissionKey(); } catch (cause) { setError(cause instanceof Error ? cause.message : "Não foi possível enviar a manifestação."); } finally { setBusy(false); } }; return ( Voltar à oportunidade

Oportunidade: {record.title}

{receipt ?

Pedido recebido

Referência: {receipt}

A equipa fará a triagem antes de qualquer partilha com o proponente.

: ( update("fullName", e.target.value)} /> update("email", e.target.value)} /> update("phone", e.target.value)} /> update("country", e.target.value)} /> update("sectorInterest", e.target.value)} /> update("investmentRange", e.target.value)} />