-
-
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 31858cd
Showing
3 changed files
with
97 additions
and
21 deletions.
There are no files selected for viewing
17 changes: 15 additions & 2 deletions
17
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,44 @@ | ||
import { goto } from '$app/navigation'; | ||
import { page } from '$app/stores'; | ||
import { writable, get, derived, readonly } from 'svelte/store'; | ||
|
||
export class SearchParams<T extends string> { | ||
private key: string; | ||
private store$ = writable<Array<T>>([]); | ||
|
||
constructor(key: string) { | ||
this.key = key; | ||
this.store$.subscribe(this.handleUpdate); | ||
this.store$.set((get(page).url.searchParams.get(this.key) || []) as Array<T>); | ||
} | ||
|
||
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()}`); | ||
}; | ||
|
||
getValues() { | ||
return readonly(this.store$); | ||
} | ||
|
||
hasValue(value: T | Array<T>) { | ||
if (value instanceof Array) { | ||
return derived(this.getValues(), (values) => values.some((value) => value.includes(value))); | ||
} | ||
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