-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
keep admin settings accordion state in search params
- Loading branch information
1 parent
4424f3c
commit f9aa4e2
Showing
3 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
33 changes: 30 additions & 3 deletions
33
web/src/lib/components/admin-page/settings/setting-accordion.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { goto } from '$app/navigation'; | ||
import { page } from '$app/stores'; | ||
import { writable, get, derived, readonly, type Writable } from 'svelte/store'; | ||
|
||
// TODO re-visit the types. | ||
const searchParams = new Map<string, unknown>(); | ||
|
||
export class SearchParams<T extends string> { | ||
private key: string; | ||
private store$: Writable<Array<T>>; | ||
|
||
private constructor(key: string) { | ||
this.key = key; | ||
this.store$ = writable<Array<T>>((get(page).url.searchParams.get(this.key)?.split(',') || []) as Array<T>); | ||
this.store$.subscribe(this.handleUpdate); | ||
} | ||
|
||
private handleUpdate = (values: Array<T>) => { | ||
if (values.length === 0) { | ||
get(page).url.searchParams.delete(this.key); | ||
} else { | ||
get(page).url.searchParams.set(this.key, values.join(',')); | ||
} | ||
goto(`?${get(page).url.searchParams.toString()}`); | ||
}; | ||
|
||
static get<T extends string>(key: string) { | ||
const params = searchParams.get(key) as SearchParams<T>; | ||
|
||
if (params) { | ||
return params; | ||
} | ||
|
||
return searchParams.set(key, new this<T>(key)).get(key) as SearchParams<T>; | ||
} | ||
|
||
getValues() { | ||
return readonly(this.store$); | ||
} | ||
|
||
hasValue(value: T | Array<T>) { | ||
if (value instanceof Array) { | ||
return derived(this.getValues(), (values) => values.some((searchValue) => value.includes(searchValue))); | ||
} | ||
return derived(this.getValues(), (values) => values.includes(value)); | ||
} | ||
|
||
addValue(value: T | Array<T>) { | ||
this.store$.update((values) => [...values, ...(value instanceof Array ? value : [value])]); | ||
} | ||
|
||
removeValue(value: T | Array<T>) { | ||
this.store$.update((values) => | ||
values.filter((searchValue) => (value instanceof Array ? !value.includes(searchValue) : searchValue !== value)), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters