From d1d654ebac2d51e3841675faeb56480e440f622f Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Tue, 5 Mar 2024 18:08:09 +0100 Subject: Initial commit --- frontend/src/lib/Sort.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 frontend/src/lib/Sort.ts (limited to 'frontend/src/lib/Sort.ts') diff --git a/frontend/src/lib/Sort.ts b/frontend/src/lib/Sort.ts new file mode 100644 index 0000000..4c9a353 --- /dev/null +++ b/frontend/src/lib/Sort.ts @@ -0,0 +1,42 @@ +import { SortDirection } from '$gql/graphql'; +import { getContext, setContext } from 'svelte'; +import { writable, type Writable } from 'svelte/store'; +import { navigate } from './Navigation'; + +export interface SortData { + on: T; + direction: SortDirection; + seed: number | undefined; +} + +export class SortContext { + on: T; + direction: SortDirection; + seed: number | undefined; + labels: Record; + + constructor({ on, direction, seed }: SortData, labels: Record) { + this.on = on; + this.direction = direction; + this.seed = seed; + this.labels = labels; + } + + set update({ on, direction, seed }: SortData) { + this.on = on; + this.direction = direction; + this.seed = seed; + } + + apply(params: URLSearchParams) { + navigate({ sort: { on: this.on, direction: this.direction, seed: this.seed } }, params); + } +} + +export function initSortContext(sort: SortData, labels: Record) { + return setContext>>('sort', writable(new SortContext(sort, labels))); +} + +export function getSortContext() { + return getContext>>('sort'); +} -- cgit v1.2.3-2-gb3c3