Prepare supplied financial directory with product coverage and native dossier continuity
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
import { Link, router } from '@inertiajs/react';
|
||||
import { useRef, useState } from 'react';
|
||||
import { profileJson, ProfileValidationError } from '@/lib/profile-json';
|
||||
import { createOperationKey } from '@/lib/operation-key';
|
||||
import { card, input, button } from '@/pages/investment/shared';
|
||||
import type { Programme } from '@/pages/funding/shared';
|
||||
|
||||
export type FundingDossier = {
|
||||
id: string;
|
||||
version: number;
|
||||
opportunity: string | null;
|
||||
closed: boolean;
|
||||
href: string;
|
||||
};
|
||||
export function FundingDossierChoice({
|
||||
record,
|
||||
dossier,
|
||||
selection,
|
||||
needs,
|
||||
destinations,
|
||||
}: {
|
||||
record: Programme;
|
||||
dossier: FundingDossier | null;
|
||||
selection: Record<string, string>;
|
||||
needs: Record<string, string>;
|
||||
destinations: { id: string; opportunity: string | null; title: string }[];
|
||||
}) {
|
||||
const [need, setNeed] = useState(selection.need ?? '');
|
||||
const [product, setProduct] = useState('');
|
||||
const [consent, setConsent] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
const [result, setResult] = useState<FundingDossier | null>(null);
|
||||
const pending = useRef<Record<string, unknown> | null>(null);
|
||||
const running = useRef(false);
|
||||
async function save() {
|
||||
if (!dossier || running.current) return;
|
||||
if (!pending.current)
|
||||
pending.current = {
|
||||
context: dossier.id,
|
||||
opportunity: dossier.opportunity,
|
||||
version: dossier.version,
|
||||
version_id: record.version_id,
|
||||
product: product || null,
|
||||
need,
|
||||
territory: selection.territory,
|
||||
consent,
|
||||
operation_key: createOperationKey(),
|
||||
};
|
||||
running.current = true;
|
||||
setBusy(true);
|
||||
setError('');
|
||||
try {
|
||||
setResult(
|
||||
(await profileJson(
|
||||
`/fundos/${record.id}/dossier`,
|
||||
pending.current,
|
||||
'POST',
|
||||
)) as FundingDossier,
|
||||
);
|
||||
pending.current = null;
|
||||
} catch (e) {
|
||||
setError(
|
||||
e instanceof Error
|
||||
? e.message
|
||||
: 'Resultado por confirmar. Repita a mesma intenção.',
|
||||
);
|
||||
if (
|
||||
e instanceof ProfileValidationError &&
|
||||
e.status !== null &&
|
||||
e.status >= 400 &&
|
||||
e.status < 500 &&
|
||||
e.status !== 408
|
||||
)
|
||||
pending.current = null;
|
||||
} finally {
|
||||
running.current = false;
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
return (
|
||||
<section className={card}>
|
||||
<h2>Continuar a preparação no meu dossier</h2>
|
||||
<p>
|
||||
Conserva a referência, versão, produto, local e necessidade. Não
|
||||
envia candidatura ou dados à instituição.
|
||||
</p>
|
||||
{!dossier ? (
|
||||
<label>
|
||||
Escolher dossier e oportunidade existentes
|
||||
<select
|
||||
className={input}
|
||||
defaultValue=""
|
||||
onChange={(e) => {
|
||||
const chosen = destinations[Number(e.target.value)];
|
||||
if (chosen)
|
||||
router.get(`/fundos/${record.id}`, {
|
||||
...selection,
|
||||
context: chosen.id,
|
||||
opportunity: chosen.opportunity ?? '',
|
||||
});
|
||||
}}
|
||||
>
|
||||
<option value="">Seleccionar</option>
|
||||
{destinations.map((d, i) => (
|
||||
<option key={`${d.id}:${d.opportunity}`} value={i}>
|
||||
{d.title}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
) : result ? (
|
||||
<Link className={button} href={result.href}>
|
||||
Escolha guardada — continuar no mesmo dossier
|
||||
</Link>
|
||||
) : (
|
||||
<>
|
||||
<fieldset
|
||||
disabled={
|
||||
busy || Boolean(pending.current) || dossier.closed
|
||||
}
|
||||
>
|
||||
<label>
|
||||
Necessidade
|
||||
<select
|
||||
className={input}
|
||||
value={need}
|
||||
onChange={(e) => setNeed(e.target.value)}
|
||||
>
|
||||
<option value="">Seleccionar</option>
|
||||
{Object.entries(needs).map(([id, label]) => (
|
||||
<option key={id} value={id}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
{record.payload.kind === 'institution' &&
|
||||
(record.payload.products ?? []).length > 0 && (
|
||||
<label>
|
||||
Programa/produto
|
||||
<select
|
||||
className={input}
|
||||
value={product}
|
||||
onChange={(e) =>
|
||||
setProduct(e.target.value)
|
||||
}
|
||||
>
|
||||
<option value="">
|
||||
Seleccionar e conferir
|
||||
cobertura/condições
|
||||
</option>
|
||||
{record.payload.products?.map((p) => (
|
||||
<option key={p.code} value={p.code}>
|
||||
{p.title} ·{' '}
|
||||
{p.availability === 'unknown'
|
||||
? 'Disponibilidade por confirmar'
|
||||
: p.availability ===
|
||||
'closed'
|
||||
? 'Encerrado'
|
||||
: 'Oferta identificada'}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
<p>
|
||||
Local seleccionado:{' '}
|
||||
{selection.territory
|
||||
? selection.territory_label ||
|
||||
'Conservado da pesquisa; consulte a cobertura do produto.'
|
||||
: 'Volte à pesquisa e escolha a localização antes de guardar.'}
|
||||
</p>
|
||||
<label>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={consent}
|
||||
onChange={(e) => setConsent(e.target.checked)}
|
||||
/>
|
||||
Conservar esta referência privada no dossier;
|
||||
financiamento não garantido.
|
||||
</label>
|
||||
</fieldset>
|
||||
<button
|
||||
type="button"
|
||||
className={button}
|
||||
disabled={
|
||||
busy ||
|
||||
dossier.closed ||
|
||||
(!pending.current &&
|
||||
(!consent || !need || !selection.territory))
|
||||
}
|
||||
onClick={() => void save()}
|
||||
>
|
||||
{pending.current
|
||||
? 'Repetir a mesma intenção'
|
||||
: 'Guardar e continuar no dossier'}
|
||||
</button>
|
||||
{dossier.closed && (
|
||||
<p>Dossier encerrado; consulta preservada.</p>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
{error && <p role="alert">{error}</p>}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user