summaryrefslogtreecommitdiffstatshomepage
path: root/frontend/src/lib/Pagination.ts
blob: f05492b34ff30ad402ac459f0eca4fa85772d1af (plain) (blame)
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 { navigate } from '$lib/Navigation';
import { getContext, setContext } from 'svelte';
import { writable, type Writable } from 'svelte/store';

export interface PaginationData {
	page: number;
	items: number;
}

export class PaginationContext {
	page = 0;
	items = 0;
	total = 0;

	set update({ page, items }: PaginationData) {
		this.page = page;
		this.items = items;
	}

	apply(params: URLSearchParams) {
		navigate({ pagination: { items: this.items } }, params);
	}
}

export function initPaginationContext() {
	return setContext<Writable<PaginationContext>>('pagination', writable(new PaginationContext()));
}

export function getPaginationContext() {
	return getContext<Writable<PaginationContext>>('pagination');
}