Files
classquiz-ai/frontend/src/lib/stores.ts
T
2022-07-02 17:20:55 +02:00

50 lines
1.2 KiB
TypeScript

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { writable } from 'svelte/store';
export const navbarVisible = writable(true);
export const signedIn = writable(false);
export const pathname = writable('/');
export const alertModal = writable({ open: false, title: '', body: '' });
import { goto } from '$app/navigation';
import { page } from '$app/stores';
const URLSearchParamsToObject = (params: URLSearchParams) => {
const obj = {};
params.forEach((v: string, k: string) => {
obj[k] = v;
});
return obj;
};
export const createQueryParamsStore = (key: string) => {
let params;
page.subscribe((v) => {
params = URLSearchParamsToObject(v.url.searchParams);
});
return {
// eslint-disable-next-line @typescript-eslint/ban-types
subscribe: (cb: Function) => {
return page.subscribe((p) => {
cb(p.url.searchParams.get(key));
});
},
set: (value: string) => {
params[key] = value;
const urlSearchParams = new URLSearchParams(params);
goto(`?${urlSearchParams.toString()}`, {
keepfocus: true,
replaceState: true,
noscroll: true
});
}
};
};