28 lines
654 B
TypeScript
28 lines
654 B
TypeScript
import type { PageServerLoad } from "./$types";
|
|
import { API_URL } from "$env/static/private";
|
|
|
|
import { redirect } from "@sveltejs/kit";
|
|
|
|
import type { Chapter } from "$lib/types";
|
|
|
|
export const load: PageServerLoad = async ({ locals, fetch, params: { chapterId } }) => {
|
|
if (!locals.user) {
|
|
redirect(302, "/login");
|
|
}
|
|
|
|
let chapter: Chapter | null = null;
|
|
|
|
const res = await fetch(API_URL + `/chapter/${chapterId}`);
|
|
|
|
if (!res.ok) {
|
|
redirect(302, "/chapters");
|
|
}
|
|
|
|
chapter = await res.json();
|
|
|
|
if (!chapter || !chapter.show && !(chapter.start && chapter.end)) redirect(302, "/chapter/" + chapterId)
|
|
|
|
return {
|
|
chapter
|
|
}
|
|
};
|