peer-at-code-web/src/routes/(app)/+page.server.ts
2024-03-31 23:55:56 +02:00

35 lines
790 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 = (async ({ url, fetch, cookies, locals: { user } }) => {
if (!user) redirect(302, handleRedirect('login', url));
const session = cookies.get('session');
const res = await fetch(`${API_URL}/chapters`, {
headers: {
Authorization: `Bearer ${session}`
}
});
if (!res.ok) {
return {
daily: null
};
}
const chapters: Chapter[] = await res.json();
const lastChapter = chapters.filter((chapter) => chapter.start && chapter.end).pop();
return {
title: 'Dashboard',
event: lastChapter,
};
}) satisfies PageServerLoad;