File tree Expand file tree Collapse file tree 2 files changed +51
-4
lines changed
Expand file tree Collapse file tree 2 files changed +51
-4
lines changed Original file line number Diff line number Diff line change 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' ;
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 );
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 }
Original file line number Diff line number Diff line change 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 ( ) ;
You can’t perform that action at this time.
0 commit comments