feat: filter public map indicators by indicator and reference period
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { useId, useState } from 'react';
|
||||
|
||||
export type QuantitativeMapContext = {
|
||||
state: 'available' | 'unavailable' | 'not_included' | 'error';
|
||||
records: {
|
||||
@@ -45,6 +47,39 @@ const metadataLabels: Record<string, string> = {
|
||||
source_date: 'data da fonte',
|
||||
};
|
||||
const text = (value?: string | null) => value?.trim() || 'Não indicado';
|
||||
const completeDate = (value?: string | null) =>
|
||||
Boolean(
|
||||
value &&
|
||||
/^\d{4}-\d{2}-\d{2}$/.test(value) &&
|
||||
!Number.isNaN(Date.parse(value)) &&
|
||||
new Date(value).toISOString().slice(0, 10) === value,
|
||||
);
|
||||
|
||||
export function filterMapIndicators(
|
||||
records: QuantitativeMapContext['records'],
|
||||
indicator: string,
|
||||
start: string,
|
||||
end: string,
|
||||
) {
|
||||
if (start && end && start > end) return [];
|
||||
return records.filter((row) => {
|
||||
if (indicator !== 'all' && row.indicator !== indicator) return false;
|
||||
if (!start && !end) return true;
|
||||
// Incomplete or inverted source periods cannot establish overlap.
|
||||
if (
|
||||
!completeDate(row.period_start) ||
|
||||
!completeDate(row.period_end) ||
|
||||
!row.period_start ||
|
||||
!row.period_end ||
|
||||
row.period_start > row.period_end
|
||||
)
|
||||
return false;
|
||||
return (
|
||||
(!start || row.period_end >= start) &&
|
||||
(!end || row.period_start <= end)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export default function InvestmentMapIndicators({
|
||||
context,
|
||||
@@ -53,12 +88,19 @@ export default function InvestmentMapIndicators({
|
||||
context: QuantitativeMapContext | null;
|
||||
theme: string;
|
||||
}) {
|
||||
const filterId = useId();
|
||||
const [indicator, setIndicator] = useState('all');
|
||||
const [start, setStart] = useState('');
|
||||
const [end, setEnd] = useState('');
|
||||
if (!['all', 'workforce', 'economic_potential'].includes(theme))
|
||||
return null;
|
||||
const rows =
|
||||
const themeRows =
|
||||
context?.records.filter(
|
||||
(row) => theme === 'all' || row.theme === theme,
|
||||
) ?? [];
|
||||
const rows = filterMapIndicators(themeRows, indicator, start, end);
|
||||
const invalidPeriod = Boolean(start && end && start > end);
|
||||
const filtered = indicator !== 'all' || Boolean(start || end);
|
||||
return (
|
||||
<section
|
||||
className="space-y-2 rounded border p-3"
|
||||
@@ -96,13 +138,101 @@ export default function InvestmentMapIndicators({
|
||||
</p>
|
||||
) : (
|
||||
<>
|
||||
<fieldset className="grid gap-3 sm:grid-cols-3">
|
||||
<legend>
|
||||
Filtrar observações do contexto territorial
|
||||
</legend>
|
||||
<label
|
||||
className="grid gap-1"
|
||||
htmlFor={`${filterId}-indicator`}
|
||||
>
|
||||
Indicador
|
||||
<select
|
||||
className="bg-background w-full rounded border p-2"
|
||||
id={`${filterId}-indicator`}
|
||||
value={indicator}
|
||||
onChange={(event) =>
|
||||
setIndicator(event.target.value)
|
||||
}
|
||||
>
|
||||
<option value="all">
|
||||
Todos os indicadores
|
||||
</option>
|
||||
{Object.entries(labels).map(([key, label]) => (
|
||||
<option key={key} value={key}>
|
||||
{label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label
|
||||
className="grid gap-1"
|
||||
htmlFor={`${filterId}-start`}
|
||||
>
|
||||
Período de referência — desde
|
||||
<input
|
||||
className="w-full min-w-0 rounded border p-2"
|
||||
id={`${filterId}-start`}
|
||||
type="date"
|
||||
value={start}
|
||||
onInput={(event) =>
|
||||
setStart(event.currentTarget.value)
|
||||
}
|
||||
aria-invalid={invalidPeriod}
|
||||
aria-describedby={`${filterId}-help`}
|
||||
/>
|
||||
</label>
|
||||
<label
|
||||
className="grid gap-1"
|
||||
htmlFor={`${filterId}-end`}
|
||||
>
|
||||
Período de referência — até
|
||||
<input
|
||||
className="w-full min-w-0 rounded border p-2"
|
||||
id={`${filterId}-end`}
|
||||
type="date"
|
||||
value={end}
|
||||
onInput={(event) =>
|
||||
setEnd(event.currentTarget.value)
|
||||
}
|
||||
aria-invalid={invalidPeriod}
|
||||
aria-describedby={`${filterId}-help`}
|
||||
/>
|
||||
</label>
|
||||
<button
|
||||
className="w-fit rounded border px-3 py-2 text-sm underline sm:col-span-3"
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setIndicator('all');
|
||||
setStart('');
|
||||
setEnd('');
|
||||
}}
|
||||
>
|
||||
Limpar filtros dos indicadores
|
||||
</button>
|
||||
</fieldset>
|
||||
<p id={`${filterId}-help`}>
|
||||
O período selecciona observações com sobreposição de
|
||||
datas, incluindo os limites. Datas incompletas ou
|
||||
invertidas na fonte ficam fora do filtro temporal. A
|
||||
data da fonte não substitui o período de referência.
|
||||
Estes filtros actuam apenas nas observações recebidas;
|
||||
não alteram oportunidades ou limites de cobertura.
|
||||
</p>
|
||||
{invalidPeriod && (
|
||||
<p role="alert">
|
||||
O início do período deve ser anterior ou igual ao
|
||||
fim.
|
||||
</p>
|
||||
)}
|
||||
<p role="status">
|
||||
{rows.length}{' '}
|
||||
{rows.length === 1 ? 'observação' : 'observações'} nesta
|
||||
camada, em {context.zones_consulted}{' '}
|
||||
camada, de {themeRows.length} recebidas, em{' '}
|
||||
{context.zones_consulted}{' '}
|
||||
{context.zones_consulted === 1
|
||||
? 'ficha territorial consultada.'
|
||||
: 'fichas territoriais consultadas.'}
|
||||
: 'fichas territoriais consultadas.'}{' '}
|
||||
A contagem não mede cobertura nacional.
|
||||
</p>
|
||||
{context.truncated && (
|
||||
@@ -114,8 +244,10 @@ export default function InvestmentMapIndicators({
|
||||
)}
|
||||
{rows.length === 0 ? (
|
||||
<p>
|
||||
Sem observações públicas destes indicadores na
|
||||
resposta. Valor desconhecido não equivale a zero.
|
||||
{filtered
|
||||
? 'Nenhuma observação corresponde aos filtros dos indicadores.'
|
||||
: 'Sem observações públicas destes indicadores na resposta.'}{' '}
|
||||
Valor desconhecido não equivale a zero.
|
||||
</p>
|
||||
) : (
|
||||
<details>
|
||||
|
||||
Reference in New Issue
Block a user