import { API_URL } from '$env/static/private'; import { redirect } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; import type { Chapter } from '$lib/types'; import { handleRedirect } from '$lib/utils'; export const load: PageServerLoad = async ({ url, locals: { user }, fetch }) => { if (!user) redirect(302, handleRedirect('login', url)); let chapters: Chapter[]; const res = await fetch(`${API_URL}/chapters`); if (!res.ok) { chapters = []; } else { chapters = await res.json(); } if (chapters.length) chapters .sort((a) => { return (a.start && a.end) ? -1 : 1; }) return { title: 'Chapitres', chapters }; }