migration to new backend
This commit is contained in:
38
frontend/src/routes/map/+page.server.ts
Normal file
38
frontend/src/routes/map/+page.server.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import type { PageServerLoad } from './$types';
|
||||
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||
import type { Adventure } from '$lib/types';
|
||||
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/`, {
|
||||
headers: {
|
||||
Cookie: `${event.cookies.get('auth')}`
|
||||
}
|
||||
});
|
||||
if (!visitedFetch.ok) {
|
||||
console.error('Failed to fetch visited adventures');
|
||||
return redirect(302, '/login');
|
||||
} else {
|
||||
let visited = (await visitedFetch.json()) as Adventure[];
|
||||
console.log('VISITEDL ' + visited);
|
||||
// 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
|
||||
};
|
||||
});
|
||||
return {
|
||||
props: {
|
||||
markers
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}) satisfies PageServerLoad;
|
||||
31
frontend/src/routes/map/+page.svelte
Normal file
31
frontend/src/routes/map/+page.svelte
Normal file
@@ -0,0 +1,31 @@
|
||||
<script>
|
||||
// @ts-nocheck
|
||||
|
||||
import { DefaultMarker, MapEvents, MapLibre, Popup } from 'svelte-maplibre';
|
||||
export let data;
|
||||
|
||||
let markers = data.props.markers;
|
||||
console.log(markers);
|
||||
</script>
|
||||
|
||||
<MapLibre
|
||||
style="https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json"
|
||||
class="relative aspect-[9/16] max-h-[70vh] w-full sm:aspect-video sm:max-h-full"
|
||||
standardControls
|
||||
>
|
||||
{#each data.props.markers as { lngLat, name }}
|
||||
<!-- Unlike the custom marker example, default markers do not have mouse events,
|
||||
and popups only support the default openOn="click" behavior -->
|
||||
<DefaultMarker {lngLat}>
|
||||
<Popup offset={[0, -10]}>
|
||||
<div class="text-lg font-bold">{name}</div>
|
||||
</Popup>
|
||||
</DefaultMarker>
|
||||
{/each}
|
||||
</MapLibre>
|
||||
|
||||
<style>
|
||||
:global(.map) {
|
||||
height: 500px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user