Skip to content

Commit

Permalink
Use localstorage for the filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Jun 5, 2024
1 parent 0676530 commit 2fe4842
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Linting & formatting

on:
push:
branches: ["master", "actions"]
branches: ['master', 'actions']
workflow_dispatch:

jobs:
Expand All @@ -19,7 +19,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18.x
cache: "pnpm"
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test using vitest

on:
push:
branches: "master"
branches: 'master'
workflow_dispatch:

jobs:
Expand All @@ -18,7 +18,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18.x
cache: "pnpm"
cache: 'pnpm'

- name: Install dependencies
run: pnpm install --frozen-lockfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export let assignment: Assignment;
$: icon = getSubjectIcon(assignment.subject)
$: icon = getSubjectIcon(assignment.subject);
</script>

<div class="flex items-center gap-2">
Expand Down
42 changes: 17 additions & 25 deletions src/lib/components/assignment/SideMenu/Filter/Filter.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { CustomDate } from '$lib/utils/dates/custom';
import School from '$lib/components/input/School.svelte';
import Classes from '$lib/components/input/Classes.svelte';
import SchoolAndClass from '$lib/components/filter/SchoolAndClass.svelte';
type Query<S> = {
school: S;
Expand All @@ -34,13 +35,20 @@
const dispatch = createEventDispatcher<{ filterApply: Query<string> }>();
</script>

<Collapseable id="homework-filter">
<h3 class="pb-3 pt-2" slot="heading">Filters</h3>

<div class="flex flex-col gap-3" slot="content">
<School bind:school />
<Classes {school} bind:classes />

<div class="flex flex-col gap-3">
<SchoolAndClass
{query}
on:change={({ detail }) => {
dispatch('filterApply', {
classes: detail.classes,
school: detail.school,
dueStart,
dueEnd,
fromStart,
fromEnd
});
}}
>
<h4><Store store={i('assignments.filter.due')} /></h4>

<TimeRange bind:start={dueStart} bind:end={dueEnd}>
Expand All @@ -56,21 +64,5 @@

<span slot="end"><Store store={i('assignments.filter.from.latest')} /></span>
</TimeRange>

<PrimaryButton
disabled={classes.length === 0 || school === null}
on:click={() => {
dispatch('filterApply', {
classes: classes,
school: school ?? '',
dueStart,
dueEnd,
fromStart,
fromEnd
});
}}
>
<Store store={i('assignments.filter.apply')} />
</PrimaryButton>
</div>
</Collapseable>
</SchoolAndClass>
</div>
18 changes: 12 additions & 6 deletions src/lib/components/filter/SchoolAndClass.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import Collapseable from '$lib/components/utils/Collapseable.svelte';
import School from '../input/School.svelte';
import Classes from '../input/Classes.svelte';
import { createEventDispatcher } from 'svelte';
import { createEventDispatcher, onMount } from 'svelte';
import { svocalWithFallback } from '$lib/utils/store/svocal';
type Query<S> = {
school: string | S;
Expand All @@ -14,7 +15,12 @@
export let query: Query<null>;
let { school, classes } = query;
const school = svocalWithFallback('filter.school', query.school);
const classes = svocalWithFallback('filter.classes', query.classes);
onMount(() => {
dispatch('change', { school: $school ?? '', classes: $classes ?? [] });
});
const dispatch = createEventDispatcher<{
change: Query<string>;
Expand All @@ -25,15 +31,15 @@
<h3 class="pb-3 pt-2" slot="heading"><Store store={i('filter.filter')} /></h3>

<div class="flex flex-col gap-3" slot="content">
<School bind:school />
<Classes {school} bind:classes />
<School bind:school={$school} />
<Classes school={$school} bind:classes={$classes} />

<slot />

<PrimaryButton
disabled={!(school && classes.length >= 1)}
disabled={!($school && $classes.length >= 1)}
on:click={() => {
dispatch('change', { school: school ?? '', classes: classes ?? [] });
dispatch('change', { school: $school ?? '', classes: $classes ?? [] });
}}
>
<Store store={i('assignments.filter.apply')} />
Expand Down
13 changes: 13 additions & 0 deletions src/lib/utils/store/svocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ export const svocal = <T extends SvocalKey>(key: T) => {

return localstorage(svocalKey, fallback);
};

const svNoCallback = {
'filter.school': null as string | null,
'filter.classes': [] as string[]
} as const;

type SvocalNoFallback = keyof typeof svNoCallback;
type SvocalFallback<T extends SvocalNoFallback> = (typeof svNoCallback)[T];

export const svocalWithFallback = <T extends SvocalNoFallback, F extends SvocalFallback<T>>(
key: T,
fallback: F
) => localstorage<F>(key, fallback);

0 comments on commit 2fe4842

Please sign in to comment.