54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { error, redirect } from '@sveltejs/kit';
|
|
import { baggedPeaks, deleteActivity, getActivity } from '$lib/server/db';
|
|
import { haversine } from '$lib/server/gpx';
|
|
import type { Actions, PageServerLoad } from './$types';
|
|
|
|
export const load: PageServerLoad = ({ params, locals }) => {
|
|
const activity = getActivity(Number(params.id));
|
|
if (!activity) error(404, 'Activity not found');
|
|
|
|
const points = JSON.parse(activity.points) as {
|
|
lat: number;
|
|
lon: number;
|
|
ele: number | null;
|
|
t: number | null;
|
|
}[];
|
|
|
|
const latlngs = points.map((p) => [p.lat, p.lon] as [number, number]);
|
|
|
|
// cumulative-distance elevation profile
|
|
const profile: { d: number; ele: number }[] = [];
|
|
let dist = 0;
|
|
for (let i = 0; i < points.length; i++) {
|
|
if (i > 0) {
|
|
dist += haversine(points[i - 1].lat, points[i - 1].lon, points[i].lat, points[i].lon);
|
|
}
|
|
if (points[i].ele !== null) profile.push({ d: dist, ele: points[i].ele! });
|
|
}
|
|
|
|
const bagged = baggedPeaks(activity.id, activity.user_id);
|
|
|
|
return {
|
|
activity: { ...activity, points: undefined },
|
|
canDelete: locals.user?.id === activity.user_id,
|
|
latlngs,
|
|
profile,
|
|
bagged: bagged.map((p) => ({
|
|
id: p.id,
|
|
name: p.name,
|
|
elevation_m: Math.round(p.elevation_m ?? 0),
|
|
lat: p.lat,
|
|
lon: p.lon,
|
|
climbed: true
|
|
}))
|
|
};
|
|
};
|
|
|
|
export const actions: Actions = {
|
|
delete: async ({ params, locals }) => {
|
|
if (!locals.user) error(401, 'Not signed in');
|
|
deleteActivity(Number(params.id), locals.user.id);
|
|
redirect(303, '/activities');
|
|
}
|
|
};
|