259 lines
10 KiB
TypeScript
259 lines
10 KiB
TypeScript
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<Form>({
|
|
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 = <K extends keyof Form>(field: K, value: Form[K]) =>
|
|
setForm((current) => ({ ...current, [field]: value }));
|
|
const submit = async (event: FormEvent<HTMLFormElement>) => {
|
|
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 (
|
|
<Frame title="Manifestar interesse">
|
|
<Link href={returnUrl} className="underline">
|
|
Voltar à oportunidade
|
|
</Link>
|
|
<p>
|
|
Oportunidade: <strong>{record.title}</strong>
|
|
</p>
|
|
{receipt ? (
|
|
<section className={card} role="status">
|
|
<h2 className="text-xl font-bold">Pedido recebido</h2>
|
|
<p>Referência: {receipt}</p>
|
|
<p>
|
|
A equipa fará a triagem antes de qualquer partilha com o
|
|
proponente.
|
|
</p>
|
|
</section>
|
|
) : (
|
|
<form className={`${card} space-y-4`} onSubmit={submit}>
|
|
<Field label="Nome ou denominação">
|
|
<input
|
|
className={input}
|
|
required
|
|
minLength={3}
|
|
maxLength={150}
|
|
value={form.fullName}
|
|
onChange={(e) => update('fullName', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Email">
|
|
<input
|
|
className={input}
|
|
type="email"
|
|
required
|
|
maxLength={150}
|
|
value={form.email}
|
|
onChange={(e) => update('email', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Telefone">
|
|
<input
|
|
className={input}
|
|
type="tel"
|
|
maxLength={30}
|
|
value={form.phone}
|
|
onChange={(e) => update('phone', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Tipo de investidor">
|
|
<select
|
|
className={input}
|
|
value={form.investorType}
|
|
onChange={(e) =>
|
|
update('investorType', e.target.value)
|
|
}
|
|
>
|
|
<option value="empresa">Empresa</option>
|
|
<option value="individual">Pessoa singular</option>
|
|
<option value="fundo">Fundo</option>
|
|
<option value="entidade_publica">
|
|
Entidade pública
|
|
</option>
|
|
<option value="outro">Outro</option>
|
|
</select>
|
|
</Field>
|
|
<Field label="País">
|
|
<input
|
|
className={input}
|
|
required
|
|
maxLength={100}
|
|
value={form.country}
|
|
onChange={(e) => update('country', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<Field label="Sector de interesse">
|
|
<input
|
|
className={input}
|
|
required
|
|
maxLength={150}
|
|
value={form.sectorInterest}
|
|
onChange={(e) =>
|
|
update('sectorInterest', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="Modalidade pretendida">
|
|
<select
|
|
className={input}
|
|
required
|
|
value={form.modality}
|
|
onChange={(e) => update('modality', e.target.value)}
|
|
>
|
|
{modalities.map((modality) => (
|
|
<option key={modality} value={modality}>
|
|
{modality.replaceAll('_', ' ')}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</Field>
|
|
<Field label="Intervalo de investimento">
|
|
<input
|
|
className={input}
|
|
maxLength={80}
|
|
value={form.investmentRange}
|
|
onChange={(e) =>
|
|
update('investmentRange', e.target.value)
|
|
}
|
|
/>
|
|
</Field>
|
|
<Field label="Motivação e pedido">
|
|
<textarea
|
|
className={input}
|
|
required
|
|
minLength={20}
|
|
maxLength={2000}
|
|
rows={5}
|
|
value={form.message}
|
|
onChange={(e) => update('message', e.target.value)}
|
|
/>
|
|
</Field>
|
|
<label className="flex items-start gap-3">
|
|
<input
|
|
type="checkbox"
|
|
required
|
|
checked={form.privacyConsent}
|
|
onChange={(e) =>
|
|
update('privacyConsent', e.target.checked)
|
|
}
|
|
/>
|
|
<span>
|
|
Autorizo o tratamento destes dados para responder a
|
|
esta manifestação. O envio não confirma
|
|
elegibilidade nem permite partilha automática com o
|
|
proponente.
|
|
</span>
|
|
</label>
|
|
<label className="flex items-start gap-3">
|
|
<input
|
|
type="checkbox"
|
|
checked={form.shareWithProponent}
|
|
onChange={(e) =>
|
|
update('shareWithProponent', e.target.checked)
|
|
}
|
|
/>
|
|
<span>
|
|
Autorizo, se necessário para avaliar esta
|
|
manifestação, a partilha do meu nome, email,
|
|
telefone e mensagem com o proponente. Esta
|
|
autorização é opcional e pode ser retirada junto da
|
|
equipa.
|
|
</span>
|
|
</label>
|
|
{error && <p role="alert">{error}</p>}
|
|
<button className={button} disabled={busy}>
|
|
{busy ? 'A enviar…' : 'Enviar manifestação'}
|
|
</button>
|
|
</form>
|
|
)}
|
|
</Frame>
|
|
);
|
|
}
|