267 lines
13 KiB
TypeScript
267 lines
13 KiB
TypeScript
import { useEffect, useRef, useState } from 'react';
|
|
import { InvestmentContextAssessment } from './investment-context-assessment';
|
|
import { Link } from '@inertiajs/react';
|
|
import {
|
|
contextApi,
|
|
contextFieldNames,
|
|
type ContextView,
|
|
} from '@/lib/investor-context';
|
|
import { inheritCompatibleFields } from '@/lib/context-inheritance';
|
|
type Props = {
|
|
contextId?: string | null;
|
|
consumer: 'simulator' | 'land' | 'desk' | 'submission';
|
|
values: Record<string, unknown>;
|
|
enabled?: boolean;
|
|
readOnly?: boolean;
|
|
allowedFields?: string[];
|
|
persistInheritance?: boolean;
|
|
boundRevision?: number;
|
|
onApply: (patch: Record<string, unknown>, replace: boolean) => void;
|
|
onReview?: (revision: number, reviewed: boolean) => void;
|
|
};
|
|
const geo = ['province_id', 'district_id', 'admin_post_id', 'locality_id'];
|
|
const empty = (v: unknown) =>
|
|
v === null ||
|
|
v === undefined ||
|
|
v === '' ||
|
|
(Array.isArray(v) && v.length === 0);
|
|
const text = (v: unknown) =>
|
|
typeof v === 'string' ? v : JSON.stringify(v ?? '');
|
|
export function DossierPrefill(props: Props) {
|
|
const {
|
|
contextId,
|
|
consumer,
|
|
enabled = true,
|
|
readOnly = false,
|
|
boundRevision = 0,
|
|
} = props;
|
|
const latest = useRef(props);
|
|
latest.current = props;
|
|
const [view, setView] = useState<ContextView | null>(null);
|
|
const [error, setError] = useState('');
|
|
const [retry, setRetry] = useState(0);
|
|
const [selected, setSelected] = useState<string[]>([]);
|
|
const [reviewed, setReviewed] = useState(false);
|
|
const [message, setMessage] = useState('');
|
|
useEffect(() => {
|
|
if (!contextId) return;
|
|
let stopped = false;
|
|
setView(null);
|
|
setError('');
|
|
void contextApi<ContextView>(`/investimentos/${contextId}/contexto`)
|
|
.then((v) => {
|
|
if (!stopped) {
|
|
setView(v);
|
|
setReviewed(false);
|
|
}
|
|
})
|
|
.catch((e) => {
|
|
if (!stopped) setError(e.message);
|
|
});
|
|
return () => {
|
|
stopped = true;
|
|
};
|
|
}, [contextId, retry]);
|
|
useEffect(() => {
|
|
if (!view || view.case.id !== contextId || !enabled || readOnly || !view.can_write) return;
|
|
const source = view.prefill[consumer] ?? {};
|
|
const now = latest.current;
|
|
const inherited = inheritCompatibleFields(`dossier:${contextId}:${consumer}`, now.values, source, now.values.context_reuse, now.allowedFields);
|
|
if (Object.keys(inherited.patch).length || (now.persistInheritance !== false && now.values.context_reuse !== inherited.metadata)) {
|
|
// The shared reconciliation has already selected safe fields; metadata travels with the same saved form.
|
|
now.onApply({...inherited.patch, ...(now.persistInheritance === false ? {} : {context_reuse:inherited.metadata})}, true);
|
|
setMessage(
|
|
'Dados compatíveis reutilizados do mesmo dossier. Os valores herdados acompanham o contexto; as suas alterações locais foram preservadas.',
|
|
);
|
|
}
|
|
setReviewed(true);
|
|
now.onReview?.(view.context.revision, true);
|
|
}, [view, enabled, readOnly, contextId, consumer, props.values]);
|
|
if (!contextId) return null;
|
|
if (error)
|
|
return (
|
|
<section
|
|
className="my-4 rounded border border-amber-400 p-4"
|
|
role="alert"
|
|
>
|
|
<p>{error} Os dados do formulário foram preservados.</p>
|
|
<button
|
|
type="button"
|
|
className="underline"
|
|
onClick={() => setRetry((x) => x + 1)}
|
|
>
|
|
Recuperar contexto novamente
|
|
</button>
|
|
</section>
|
|
);
|
|
if (!view)
|
|
return (
|
|
<p role="status">A recuperar os dados autorizados do dossier…</p>
|
|
);
|
|
const source = Object.fromEntries(
|
|
Object.entries(view.prefill[consumer] ?? {}).filter(
|
|
([k]) => !props.allowedFields || props.allowedFields.includes(k),
|
|
),
|
|
);
|
|
const differences = Object.keys(source).filter(
|
|
(k) => k !== 'context_applicability_snapshot' && text(props.values[k]) !== text(source[k]),
|
|
);
|
|
const linkedRevision =
|
|
consumer === 'simulator'
|
|
? (view.simulator_context_revision ?? 0)
|
|
: boundRevision;
|
|
const stale = view.context.revision !== linkedRevision && !reviewed;
|
|
return (
|
|
<section
|
|
className="my-4 space-y-3 rounded border border-emerald-300 bg-emerald-50 p-4 [overflow-wrap:anywhere]"
|
|
aria-label="Dados do dossier neste formulário"
|
|
>
|
|
<h2 className="font-bold">
|
|
Mesmo investimento · contexto v{view.context.revision}
|
|
</h2>
|
|
<p>
|
|
UUID {contextId} ·{' '}
|
|
<Link
|
|
className="underline"
|
|
href={`/investimentos/${contextId}`}
|
|
>
|
|
recuperar dossier
|
|
</Link>
|
|
. Mestres, declarações deste formulário e conteúdo já submetido
|
|
mantêm-se separados.
|
|
</p>
|
|
<InvestmentContextAssessment contextId={contextId} raw={view.prefill.simulator?.context_applicability_snapshot} />
|
|
{stale && (
|
|
<p className="font-semibold" role="status">
|
|
{linkedRevision > 0
|
|
? `O contexto do dossier difere da revisão vinculada a este registo (v${linkedRevision}).`
|
|
: 'Confira os valores deste formulário para o contexto atual; não há uma revisão anterior vinculada a estas entradas.'}{' '}
|
|
Os valores e resultados anteriores permanecem guardados;
|
|
confira os pressupostos antes de os usar no cenário atual.
|
|
</p>
|
|
)}
|
|
{message && <p role="status">{message}</p>}
|
|
{Object.values(view.context.selection).map(
|
|
(r) =>
|
|
r && (
|
|
<p key={r.key}>
|
|
Fonte disponível:{' '}
|
|
<Link className="underline" href={r.href}>
|
|
{r.title}
|
|
</Link>{' '}
|
|
· {r.kind} · v{r.version ?? 'não exposta'}. {r.key}
|
|
</p>
|
|
),
|
|
)}
|
|
{!readOnly && view.can_write && differences.length > 0 && (
|
|
<details>
|
|
<summary className="cursor-pointer font-semibold">
|
|
Comparar com os campos já preenchidos
|
|
</summary>
|
|
<p>
|
|
Os seus valores conservam-se por defeito. Selecione
|
|
apenas os campos que pretende substituir; o formulário
|
|
continua a precisar de ser guardado/submetido pela ação
|
|
existente.
|
|
</p>
|
|
<div className="overflow-x-auto">
|
|
<table className="w-full text-left">
|
|
<thead>
|
|
<tr>
|
|
<th>Substituir</th>
|
|
<th>Valor atual</th>
|
|
<th>Valor proposto pelo dossier</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{differences.map((k) => (
|
|
<tr key={k}>
|
|
<td className="p-2 align-top">
|
|
<label>
|
|
<input
|
|
type="checkbox"
|
|
checked={selected.includes(
|
|
k,
|
|
)}
|
|
onChange={(e) => {
|
|
const keys =
|
|
geo.includes(k)
|
|
? geo
|
|
: [k];
|
|
setSelected(
|
|
e.target.checked
|
|
? [
|
|
...new Set(
|
|
[
|
|
...selected,
|
|
...keys,
|
|
],
|
|
),
|
|
]
|
|
: selected.filter(
|
|
(x) =>
|
|
!keys.includes(
|
|
x,
|
|
),
|
|
),
|
|
);
|
|
}}
|
|
/>{' '}
|
|
{k === 'parcels'
|
|
? 'Localização da primeira parcela'
|
|
: (contextFieldNames[k] ??
|
|
k)}
|
|
</label>
|
|
</td>
|
|
<td className="p-2 [overflow-wrap:anywhere]">
|
|
{text(props.values[k]) ||
|
|
'não informado'}
|
|
</td>
|
|
<td className="p-2 [overflow-wrap:anywhere]">
|
|
{text(source[k]) || 'não informado'}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<button
|
|
type="button"
|
|
className="mt-3 rounded border bg-white px-3 py-2"
|
|
disabled={!selected.length || !enabled}
|
|
onClick={() => {
|
|
const patch: Record<string, unknown> = {};
|
|
for (const k of selected)
|
|
patch[k] = source[k] ?? '';
|
|
const scope = `dossier:${contextId}:${consumer}`;
|
|
let raw = String(props.values.context_reuse ?? '');
|
|
try {
|
|
const metadata = JSON.parse(raw);
|
|
if(metadata.scope === scope) {
|
|
for(const key of selected) delete metadata.inherited?.[key];
|
|
raw=JSON.stringify(metadata);
|
|
}
|
|
} catch { /* Legacy metadata does not grant ownership of other fields. */ }
|
|
const adopted=inheritCompatibleFields(scope,{...props.values,...Object.fromEntries(selected.map(key=>[key,undefined]))},patch,raw,props.allowedFields);
|
|
props.onApply({...patch,...(props.persistInheritance === false ? {} : {context_reuse:adopted.metadata})}, true);
|
|
setSelected([]);
|
|
setReviewed(false);
|
|
props.onReview?.(view.context.revision, false);
|
|
setMessage(
|
|
'Valores selecionados aplicados ao formulário. Os outros valores e os snapshots anteriores foram preservados.',
|
|
);
|
|
}}
|
|
>
|
|
Aplicar somente os campos selecionados
|
|
</button>
|
|
</details>
|
|
)}
|
|
{!readOnly && reviewed && <p>Contexto v{view.context.revision} recuperado neste formulário, sem nova confirmação dos dados compatíveis. As edições locais conservam-se; as declarações e submissões formais mantêm os seus controlos próprios.</p>}
|
|
<p className="text-xs">
|
|
Associar uma referência não confirma incentivos, licenças, terra
|
|
disponível, reserva ou autorização.
|
|
</p>
|
|
</section>
|
|
);
|
|
}
|