198 lines
7.5 KiB
TypeScript
198 lines
7.5 KiB
TypeScript
import { useState } from 'react';
|
|
import { portalRequest } from '@/lib/portal-api';
|
|
import { card, button } from '@/pages/investment/shared';
|
|
import type { Programme } from '@/pages/funding/shared';
|
|
|
|
type Assessment = {
|
|
notice: string;
|
|
criteria: { code: string; status: string }[];
|
|
};
|
|
const statusLabels: Record<string, string> = {
|
|
matches: 'Corresponde à resposta esperada',
|
|
does_not_match: 'Não corresponde à resposta esperada',
|
|
unknown: 'Informação por confirmar',
|
|
not_applicable: 'Não aplicável ao tipo de candidato indicado',
|
|
};
|
|
|
|
export default function FundingCriteriaAssessment({
|
|
record,
|
|
}: {
|
|
record: Programme;
|
|
}) {
|
|
const [kind, setKind] = useState('unknown');
|
|
const [windowCode, setWindowCode] = useState('');
|
|
const [answers, setAnswers] = useState<Record<string, string>>({});
|
|
const [assessment, setAssessment] = useState<Assessment | null>(null);
|
|
const [error, setError] = useState('');
|
|
const [busy, setBusy] = useState(false);
|
|
const selectedWindow = record.payload.windows.find(
|
|
(item) => item.code === windowCode,
|
|
);
|
|
const criteria = [
|
|
...record.payload.criteria,
|
|
...(selectedWindow?.criteria ?? []),
|
|
];
|
|
const applicable = criteria.filter(
|
|
(item) => item.applies_to === 'all' || item.applies_to === kind,
|
|
);
|
|
const awaitingType = criteria.filter(
|
|
(item) => item.applies_to !== 'all' && kind === 'unknown',
|
|
).length;
|
|
const notApplicable = criteria.length - applicable.length - awaitingType;
|
|
function resetResult() {
|
|
setAssessment(null);
|
|
setError('');
|
|
}
|
|
async function assess() {
|
|
setBusy(true);
|
|
resetResult();
|
|
try {
|
|
setAssessment(
|
|
await portalRequest<Assessment>(
|
|
`/fundos/${record.id}/criterios`,
|
|
{
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
version_id: record.version_id,
|
|
applicant_kind: kind,
|
|
window_code: windowCode || null,
|
|
answers: Object.fromEntries(
|
|
applicable.map((item) => [
|
|
item.code,
|
|
answers[item.code] ?? 'unknown',
|
|
]),
|
|
),
|
|
}),
|
|
},
|
|
),
|
|
);
|
|
} catch (failure) {
|
|
setError(
|
|
failure instanceof Error
|
|
? failure.message
|
|
: 'Não foi possível conferir os critérios.',
|
|
);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
return (
|
|
<section className={card}>
|
|
<h2 className="text-xl font-semibold">
|
|
Conferir critérios documentados
|
|
</h2>
|
|
<p>
|
|
Esta consulta não envia candidatura nem concede financiamento.
|
|
As respostas não ficam guardadas.
|
|
</p>
|
|
<label className="block">
|
|
Tipo de candidato
|
|
<select
|
|
name="applicant-kind"
|
|
disabled={busy}
|
|
className="m-2 rounded border p-2"
|
|
value={kind}
|
|
onChange={(event) => {
|
|
setKind(event.target.value);
|
|
resetResult();
|
|
}}
|
|
>
|
|
<option value="unknown">Por definir</option>
|
|
<option value="individual">Pessoa singular</option>
|
|
<option value="entity">Entidade</option>
|
|
</select>
|
|
</label>
|
|
{record.payload.windows.length > 0 && (
|
|
<label className="block">
|
|
Janela
|
|
<select
|
|
name="funding-window"
|
|
disabled={busy}
|
|
className="m-2 rounded border p-2"
|
|
value={windowCode}
|
|
onChange={(event) => {
|
|
setWindowCode(event.target.value);
|
|
resetResult();
|
|
}}
|
|
>
|
|
<option value="">Apenas critérios gerais</option>
|
|
{record.payload.windows.map((item) => (
|
|
<option key={item.code} value={item.code}>
|
|
{item.title}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
)}
|
|
{criteria.length === 0 && (
|
|
<p>
|
|
Esta ficha não contém critérios suficientes para conferir a
|
|
correspondência.
|
|
</p>
|
|
)}
|
|
{awaitingType > 0 && (
|
|
<p>
|
|
Escolha o tipo de candidato para apresentar os{' '}
|
|
{awaitingType} critérios específicos. Sem essa escolha,
|
|
ficam por confirmar.
|
|
</p>
|
|
)}
|
|
{notApplicable > 0 && (
|
|
<p>
|
|
{notApplicable} critérios de outro tipo de candidato não se
|
|
aplicam à escolha indicada.
|
|
</p>
|
|
)}
|
|
{applicable.map((item) => (
|
|
<div key={item.code} className="my-4">
|
|
<label>
|
|
{item.question}
|
|
<select
|
|
name={`criterion-${item.code}`}
|
|
disabled={busy}
|
|
className="m-2 rounded border p-2"
|
|
value={answers[item.code] ?? 'unknown'}
|
|
onChange={(event) => {
|
|
setAnswers({
|
|
...answers,
|
|
[item.code]: event.target.value,
|
|
});
|
|
resetResult();
|
|
}}
|
|
>
|
|
<option value="unknown">
|
|
Não sei / por confirmar
|
|
</option>
|
|
<option value="yes">Sim</option>
|
|
<option value="no">Não</option>
|
|
</select>
|
|
</label>
|
|
<p>{item.explanation}</p>
|
|
<p>Fonte: {item.source_reference}</p>
|
|
{assessment && (
|
|
<p>
|
|
{
|
|
statusLabels[
|
|
assessment.criteria.find(
|
|
(result) => result.code === item.code,
|
|
)?.status ?? 'unknown'
|
|
]
|
|
}
|
|
</p>
|
|
)}
|
|
</div>
|
|
))}
|
|
<button
|
|
className={button}
|
|
disabled={busy || criteria.length === 0}
|
|
onClick={() => void assess()}
|
|
>
|
|
{busy ? 'A conferir…' : 'Conferir respostas'}
|
|
</button>
|
|
{error && <p role="alert">{error}</p>}
|
|
{assessment && <p role="status">{assessment.notice}</p>}
|
|
</section>
|
|
);
|
|
}
|