Skip to content

Commit 79b00ae

Browse files
committed
🎨 Allow saving and restoring of showing/hiding of replenishment workbook for curriculum (#1726)
1 parent 78ec4a3 commit 79b00ae

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/lib/components/WorkBooks/WorkBookList.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { ButtonGroup, Button, Toggle } from 'svelte-5-ui-lib';
55
66
import { taskGradesByWorkBookTypeStore } from '$lib/stores/task_grades_by_workbook_type';
7+
import { replenishmentWorkBooksStore } from '$lib/stores/replenishment_workbook.svelte';
78
import { canRead } from '$lib/utils/authorship';
89
import { WorkBookType, type WorkbookList, type WorkbooksList } from '$lib/types/workbook';
910
import { getTaskGradeLabel } from '$lib/utils/task';
@@ -57,8 +58,6 @@
5758
}),
5859
);
5960
60-
let isShowReplenishment: boolean = $state(false);
61-
6261
function countReadableWorkbooks(workbooks: WorkbooksList): number {
6362
const results = workbooks.reduce((count, workbook: WorkbookList) => {
6463
const hasReadPermission = canRead(workbook.isPublished, userId, workbook.authorId);
@@ -158,11 +157,16 @@
158157
</div>
159158

160159
<div class="mt-4 md:mt-0 pb-4">
161-
<Toggle bind:checked={isShowReplenishment}>表示</Toggle>
160+
<Toggle
161+
checked={replenishmentWorkBooksStore.canView()}
162+
onclick={replenishmentWorkBooksStore.toggleView}
163+
>
164+
表示
165+
</Toggle>
162166
</div>
163167
</div>
164168

165-
{#if isShowReplenishment}
169+
{#if replenishmentWorkBooksStore.canView()}
166170
<WorkBookBaseTable
167171
{workbookType}
168172
workbooks={replenishedWorkbooks}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// See:
2+
// https://svelte.dev/docs/kit/$app-environment#browser
3+
import { browser } from '$app/environment';
4+
5+
// See:
6+
// https://svelte.dev/docs/svelte/stores
7+
// https://svelte.dev/docs/svelte/$state
8+
const IS_SHOWN_REPLENISHMENT_WORKBOOKS = 'is_shown_replenishment_workbooks';
9+
10+
class ReplenishmentWorkBooksStore {
11+
private isShown = $state<boolean>(this.loadInitialState());
12+
13+
// Note:
14+
// The $state only manages state in memory and is reset on page reload.
15+
// In addition, it is not linked to the browser's persistent storage.
16+
private loadInitialState(): boolean {
17+
// WHY: Cannot access localStorage during SSR (server-side rendering).
18+
if (!browser) {
19+
return false;
20+
}
21+
22+
const savedStatus = localStorage.getItem(IS_SHOWN_REPLENISHMENT_WORKBOOKS);
23+
return savedStatus ? JSON.parse(savedStatus) : false;
24+
}
25+
26+
canView() {
27+
return this.isShown;
28+
}
29+
30+
toggleView = () => {
31+
this.isShown = !this.isShown;
32+
33+
if (browser) {
34+
localStorage.setItem(IS_SHOWN_REPLENISHMENT_WORKBOOKS, JSON.stringify(this.isShown));
35+
}
36+
};
37+
38+
reset() {
39+
this.isShown = false;
40+
}
41+
}
42+
43+
export const replenishmentWorkBooksStore = new ReplenishmentWorkBooksStore();

0 commit comments

Comments
 (0)