1
0
Fork 0
mirror of https://github.com/seanmorley15/AdventureLog.git synced 2025-08-03 20:25:18 +02:00
AdventureLog/frontend/src/routes/map/+page.server.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-07-08 11:44:39 -04:00
import { redirect } from '@sveltejs/kit';
import type { PageServerLoad } from './$types';
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
2024-07-24 10:40:48 -04:00
import type { Adventure, VisitedRegion } from '$lib/types';
2024-07-08 11:44:39 -04:00
const endpoint = PUBLIC_SERVER_URL || 'http://localhost:8000';
export const load = (async (event) => {
if (!event.locals.user) {
return redirect(302, '/login');
} else {
let visitedFetch = await fetch(`${endpoint}/api/adventures/all/`, {
2024-07-08 11:44:39 -04:00
headers: {
Cookie: `${event.cookies.get('auth')}`
}
});
2024-07-24 10:40:48 -04:00
let visitedRegionsFetch = await fetch(`${endpoint}/api/visitedregion/`, {
headers: {
Cookie: `${event.cookies.get('auth')}`
}
});
let visitedRegions = (await visitedRegionsFetch.json()) as VisitedRegion[];
2024-07-08 11:44:39 -04:00
if (!visitedFetch.ok) {
console.error('Failed to fetch visited adventures');
return redirect(302, '/login');
} else {
let visited = (await visitedFetch.json()) as Adventure[];
// make a long lat array like this { lngLat: [-20, 0], name: 'Adventure 1' },
let markers = visited
.filter((adventure) => adventure.latitude !== null && adventure.longitude !== null)
.map((adventure) => {
return {
lngLat: [adventure.longitude, adventure.latitude] as [number, number],
name: adventure.name,
type: adventure.type
2024-07-08 11:44:39 -04:00
};
});
2024-07-24 10:40:48 -04:00
2024-07-24 14:16:25 -04:00
console.log('sent');
2024-07-08 11:44:39 -04:00
return {
props: {
2024-07-24 10:40:48 -04:00
markers,
visitedRegions
2024-07-08 11:44:39 -04:00
}
};
}
}
}) satisfies PageServerLoad;