57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import InteractiveInvestmentMap, { type InvestmentMapFeature } from "./interactive-investment-map";
|
|
|
|
export default function InvestmentMapPanel({
|
|
filters,
|
|
}: {
|
|
filters: Record<string, string | number>;
|
|
}) {
|
|
const key = JSON.stringify(filters);
|
|
const [points, setPoints] = useState<InvestmentMapFeature[] | null>(null);
|
|
const [missing, setMissing] = useState<{ id: number; title: string; url: string }[]>([]);
|
|
const [error, setError] = useState("");
|
|
useEffect(() => {
|
|
const controller = new AbortController();
|
|
const params = new URLSearchParams(JSON.parse(key));
|
|
setPoints(null);
|
|
fetch(`/api/v1/investment/map?${params}`, {
|
|
signal: controller.signal,
|
|
headers: { Accept: "application/json" },
|
|
})
|
|
.then(async (response) => {
|
|
if (!response.ok) throw new Error("Mapa indisponível.");
|
|
return response.json();
|
|
})
|
|
.then((data) => {
|
|
if (controller.signal.aborted) return;
|
|
setPoints(data.features);
|
|
setMissing(data.unlocated ?? []);
|
|
setError(data.truncated ? "Cobertura limitada: refine os filtros." : "");
|
|
})
|
|
.catch((e) => {
|
|
if (!controller.signal.aborted) setError(e.message);
|
|
});
|
|
return () => controller.abort();
|
|
}, [key]);
|
|
return (
|
|
<section className="space-y-3">
|
|
{error && <p role="alert">{error}</p>}
|
|
<InteractiveInvestmentMap points={points} />
|
|
{missing.length > 0 && (
|
|
<div>
|
|
<p>{missing.length} investimentos aguardam coordenadas do local:</p>
|
|
<ul>
|
|
{missing.map((record) => (
|
|
<li key={record.id}>
|
|
<a href={record.url} className="underline">
|
|
{record.title}
|
|
</a>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|