-
Notifications
You must be signed in to change notification settings - Fork 1
/
+page.server.ts
31 lines (28 loc) · 1.23 KB
/
+page.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import {flash_redirect} from '@friendofsvelte/django-kit';
import type {OffsetPaginated} from "$lib/interfaces";
import type {Todo} from "$lib/interfaces/todos";
import type {PageServerLoad, PageServerLoadEvent} from './$types';
import {get_paginator} from "$lib/server";
import {via_route, via_route_name} from "@friendofsvelte/django-kit/server/actions";
export const actions = {
...via_route_name([{name: 'create_todo', method: 'POST'}, {name: 'delete_todo', method: 'DELETE'},]),
...via_route(['update',], {prefix: 'todos'})
}
const get_todos = async (event: PageServerLoadEvent) => {
const paginator = get_paginator(event.url);
const query = `limit=${paginator.limit}&offset=${paginator.offset}&search=${paginator.search}&order_by=${paginator.order_by}`;
const response = await event.fetch(`$api/todos/?${query}`);
return await response.json() as OffsetPaginated<Todo>;
}
export const load: PageServerLoad = async (event) => {
if (!event.locals.current_user) {
flash_redirect(event.cookies, {
alias: 'error',
message: 'You need to be logged in to access the dashboard.',
message_type: 'error'
}, 302, '/login')
}
return {
todos: await get_todos(event)
}
}