37 lines
936 B
TypeScript
37 lines
936 B
TypeScript
import { API_URL } from '$env/static/private';
|
|
|
|
import type { PageServerLoad } from './$types';
|
|
|
|
import type { Chapter } from '$lib/types';
|
|
import { handleRedirect } from '$lib/utils';
|
|
import { redirect } from '@sveltejs/kit';
|
|
|
|
export const load = (async ({ url, locals: { user }, fetch, cookies, params: { chapterId } }) => {
|
|
|
|
if (!user) redirect(302, handleRedirect('login', url));
|
|
|
|
const session = cookies.get('session');
|
|
|
|
const res = await fetch(`${API_URL}/chapter/${chapterId}`, {
|
|
headers: {
|
|
Authorization: `Bearer ${session}`
|
|
}
|
|
});
|
|
|
|
if (!res.ok) {
|
|
redirect(302, '/chapters');
|
|
}
|
|
|
|
const chapter = (await res.json()) as Chapter;
|
|
|
|
if (!chapter || !chapter.show && !(chapter.start && chapter.end)) {
|
|
redirect(302, '/chapters');
|
|
}
|
|
|
|
if (chapter?.puzzles?.length) chapter.puzzles = chapter.puzzles.sort((a, b) => a.scoreMax - b.scoreMax);
|
|
|
|
return {
|
|
title: chapter.name,
|
|
chapter
|
|
};
|
|
}) satisfies PageServerLoad;
|