232 lines
9.7 KiB
TypeScript
232 lines
9.7 KiB
TypeScript
import { router } from '@inertiajs/react';
|
|
import { useState } from 'react';
|
|
import CatalogueChoiceSelect from '@/components/catalogue-choice-select';
|
|
import DemographicEvidence, {
|
|
type DemographicAnalysis,
|
|
} from '@/components/demographic-evidence';
|
|
import { Frame, button } from './shared';
|
|
|
|
export default function Demography(
|
|
props: Parameters<typeof DemographySelection>[0],
|
|
) {
|
|
// Server filters identify this consultation, including browser back/forward.
|
|
return (
|
|
<DemographySelection key={JSON.stringify(props.filters)} {...props} />
|
|
);
|
|
}
|
|
|
|
function DemographySelection({
|
|
evidence,
|
|
filters,
|
|
territories,
|
|
sectors,
|
|
}: {
|
|
evidence: DemographicAnalysis;
|
|
filters: {
|
|
territories?: string[];
|
|
start_year?: number;
|
|
end_year?: number;
|
|
sector?: string;
|
|
};
|
|
territories: {
|
|
id: string;
|
|
name: string;
|
|
location_type: string;
|
|
parent_id: string | null;
|
|
}[];
|
|
sectors: Record<string, string>;
|
|
}) {
|
|
const [selected, setSelected] = useState(filters.territories ?? []);
|
|
const [province, setProvince] = useState('');
|
|
const [district, setDistrict] = useState('');
|
|
const [selectionNotice, setSelectionNotice] = useState('');
|
|
const candidate = district || province;
|
|
const [start, setStart] = useState(
|
|
filters.start_year ?? new Date().getFullYear(),
|
|
);
|
|
const [end, setEnd] = useState(filters.end_year ?? start);
|
|
const [sector, setSector] = useState(filters.sector ?? 'general');
|
|
return (
|
|
<Frame title="Demografia e viabilidade territorial">
|
|
<p>
|
|
Compare até quatro localizações no mesmo sector e período. Use
|
|
esta evidência em conjunto com as condições económicas, fiscais
|
|
e territoriais do investimento.
|
|
</p>
|
|
<form
|
|
className="grid gap-4 rounded border bg-white p-4 md:grid-cols-2"
|
|
onSubmit={(e) => {
|
|
e.preventDefault();
|
|
router.get(
|
|
'/localizacoes/demografia',
|
|
{
|
|
territories: selected,
|
|
start_year: start,
|
|
end_year: end,
|
|
sector,
|
|
},
|
|
{ preserveState: true },
|
|
);
|
|
}}
|
|
>
|
|
<fieldset className="min-w-0 space-y-3">
|
|
<legend>Localizações a comparar (até quatro)</legend>
|
|
<label htmlFor="demography-province" className="block">
|
|
Província
|
|
</label>
|
|
<CatalogueChoiceSelect
|
|
id="demography-province"
|
|
label="Província da comparação"
|
|
value={province}
|
|
options={territories
|
|
.filter((t) => t.location_type === 'province')
|
|
.map((t) => ({ value: t.id, label: t.name }))}
|
|
onChange={(value) => {
|
|
setProvince(value);
|
|
if (district)
|
|
setSelectionNotice(
|
|
'A escolha do distrito foi limpa. Os locais já acrescentados à comparação foram mantidos.',
|
|
);
|
|
setDistrict('');
|
|
}}
|
|
/>
|
|
<label htmlFor="demography-district" className="block">
|
|
Distrito (opcional)
|
|
</label>
|
|
<CatalogueChoiceSelect
|
|
id="demography-district"
|
|
label="Distrito da comparação (opcional)"
|
|
value={district}
|
|
disabled={!province}
|
|
options={territories
|
|
.filter(
|
|
(t) =>
|
|
t.location_type === 'district' &&
|
|
t.parent_id === province,
|
|
)
|
|
.map((t) => ({ value: t.id, label: t.name }))}
|
|
onChange={setDistrict}
|
|
/>
|
|
<p>
|
|
Pode comparar apenas a província ou escolher um dos seus
|
|
distritos.
|
|
</p>
|
|
{selectionNotice && <p role="status">{selectionNotice}</p>}
|
|
<button
|
|
type="button"
|
|
className={button}
|
|
disabled={
|
|
!candidate ||
|
|
selected.length >= 4 ||
|
|
selected.includes(candidate)
|
|
}
|
|
onClick={() =>
|
|
setSelected((current) => [...current, candidate])
|
|
}
|
|
>
|
|
Acrescentar à comparação
|
|
</button>
|
|
<ul className="space-y-2">
|
|
{selected.map((id) => {
|
|
const t = territories.find(
|
|
(item) => item.id === id,
|
|
);
|
|
const label = t
|
|
? `${t.name} — ${t.location_type === 'province' ? 'Província' : 'Distrito'}`
|
|
: 'Localização guardada';
|
|
return (
|
|
<li
|
|
key={id}
|
|
className="flex flex-wrap items-center gap-2"
|
|
>
|
|
<span>{label}</span>
|
|
<button
|
|
type="button"
|
|
className="underline"
|
|
aria-label={`Retirar ${label} da comparação`}
|
|
onClick={() =>
|
|
setSelected((current) =>
|
|
current.filter(
|
|
(value) => value !== id,
|
|
),
|
|
)
|
|
}
|
|
>
|
|
Retirar
|
|
</button>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</fieldset>
|
|
<div className="space-y-3">
|
|
<label className="block">
|
|
Sector
|
|
<select
|
|
className="block w-full rounded border p-2"
|
|
value={sector}
|
|
onChange={(e) => setSector(e.target.value)}
|
|
>
|
|
{Object.entries(sectors).map(([key, label]) => (
|
|
<option value={key} key={key}>
|
|
{label}
|
|
</option>
|
|
))}
|
|
</select>
|
|
</label>
|
|
<label className="block">
|
|
Ano inicial
|
|
<input
|
|
className="block rounded border p-2"
|
|
type="number"
|
|
min="1900"
|
|
max="2100"
|
|
value={start}
|
|
onChange={(e) => setStart(Number(e.target.value))}
|
|
/>
|
|
</label>
|
|
<label className="block">
|
|
Ano final
|
|
<input
|
|
className="block rounded border p-2"
|
|
type="number"
|
|
min={start}
|
|
max={Math.min(2100, start + 50)}
|
|
value={end}
|
|
onChange={(e) => setEnd(Number(e.target.value))}
|
|
/>
|
|
</label>
|
|
<button className={button} disabled={selected.length === 0}>
|
|
Consultar e comparar
|
|
</button>
|
|
</div>
|
|
</form>
|
|
<DemographicEvidence evidence={evidence} />
|
|
{evidence.territories.length > 0 && (
|
|
<section className="space-y-3 rounded border p-4">
|
|
<h2 className="text-xl font-semibold">
|
|
Transformar a análise numa ideia
|
|
</h2>
|
|
<p>
|
|
Prepare uma ideia nesta localização e confronte-a com a
|
|
oferta existente. Não é necessário haver uma
|
|
oportunidade publicada para iniciar a análise.
|
|
</p>
|
|
{evidence.territories.map((t) => (
|
|
<a
|
|
key={t.id}
|
|
className="block py-2 underline"
|
|
href={
|
|
'/investimentos/iniciar?source=' +
|
|
encodeURIComponent('territory:' + t.id)
|
|
}
|
|
>
|
|
Preparar ideia em {t.name}
|
|
</a>
|
|
))}
|
|
</section>
|
|
)}
|
|
</Frame>
|
|
);
|
|
}
|