peer-at-code-web/src/routes/(app)/chapters/+page.server.ts
2024-04-16 00:43:58 +02:00

31 lines
665 B
TypeScript

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
};
}