chore: Add GeoJSON endpoint to retrieve combined GeoJSON data from static files

This commit is contained in:
Sean Morley
2024-07-27 12:46:50 -04:00
parent 70d08eff8a
commit 57eee7bb5d
4 changed files with 83 additions and 25 deletions

View File

@@ -5,12 +5,6 @@ import type { Adventure, VisitedRegion } from '$lib/types';
const endpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
export const load = (async (event) => {
let countryCodesToFetch = ['FR', 'US', 'CA', 'DE', 'AU', 'MX', 'JP'];
let geoJSON = {
type: 'FeatureCollection',
features: []
};
if (!event.locals.user) {
return redirect(302, '/login');
} else {
@@ -20,6 +14,8 @@ export const load = (async (event) => {
}
});
let geoJsonUrl = `${endpoint}/api/geojson/` as string;
let visitedRegionsFetch = await fetch(`${endpoint}/api/visitedregion/`, {
headers: {
Cookie: `${event.cookies.get('auth')}`
@@ -27,19 +23,6 @@ export const load = (async (event) => {
});
let visitedRegions = (await visitedRegionsFetch.json()) as VisitedRegion[];
await Promise.all(
countryCodesToFetch.map(async (code) => {
let res = await fetch(`${endpoint}/static/data/${code.toLowerCase()}.json`);
console.log('fetching ' + code);
let json = await res.json();
if (!json) {
console.error(`Failed to fetch ${code} GeoJSON`);
} else {
geoJSON.features = geoJSON.features.concat(json.features);
}
})
);
if (!visitedFetch.ok) {
console.error('Failed to fetch visited adventures');
return redirect(302, '/login');
@@ -61,7 +44,7 @@ export const load = (async (event) => {
return {
props: {
markers,
geoJSON,
geoJsonUrl,
visitedRegions
}
};