From 34b92973063a59af6fb5f84fd2cfa70a13949338 Mon Sep 17 00:00:00 2001 From: Seth Trowbridge Date: Wed, 7 Jun 2023 21:09:10 -0400 Subject: [PATCH] persist settings --- js/store.js | 63 +++++++++++++++++++++++++++------------------------ js/ui.js | 2 +- ts/store.d.ts | 20 ++++++++++++---- 3 files changed, 49 insertions(+), 36 deletions(-) diff --git a/js/store.js b/js/store.js index 8b41f12..1d4fa3b 100644 --- a/js/store.js +++ b/js/store.js @@ -161,7 +161,7 @@ export function Reducer(inState, inAction) const test = clone.Test[Data]; if(test) { - clone.TestIndex = Data; + clone.Pick = Data; clone.Live = Reselect(clone, test); clone.Draw = { @@ -227,6 +227,8 @@ export function Reducer(inState, inAction) clone.Show.Answer = Data; } + SaveSettings(clone); + return clone; } @@ -286,34 +288,42 @@ const TestDefault = [ ] } ]; - -/** @type {Store.Test[] | string | null } */ -let TestSaved = localStorage.getItem("app-tests"); -if(TestSaved) -{ - TestSaved = JSON.parse(TestSaved); -} - +/** @type {Store.Test[]} */ +const TestActual = JSON.parse(localStorage.getItem("app-tests")||"false") || TestDefault; /**@type {(inState:Store.State)=>void} */ const SaveTests =(inState)=> localStorage.setItem("app-tests", JSON.stringify(inState.Test)); -/**@type {(inState:Store.State)=>void} */ -const SaveSettings =(inState)=> -{ - const clone = {...inState, Test:null, Draw:null, }; - localStorage.setItem("app-settings", JSON.stringify(clone)); -} - - -/** @type {Store.Test[]} */ -const TestActual = Array.isArray(TestSaved) ? TestSaved : TestDefault - -export const Initial = Reducer( +/** @type {Store.StatePartSimple} */ +const SettingsDefault = { Chan: { Min:0, Max:1, Value:0, Step:1 }, Freq: { Min:2, Max:8, Value:3, Step:1 }, Stim: { Min:-10, Max:120, Value:30, Step:5 }, Errs: 0, + Pick: 0, + Show: { Cursor:true, Answer:false } +}; +/** @type {Store.StatePartSimple} */ +const SettingsActual = JSON.parse(localStorage.getItem("app-settings")||"false") || SettingsDefault; +/**@type {(inState:Store.State)=>void} */ +const SaveSettings =(inState)=> +{ + /** @type {Store.StatePartSimple} */ + const clone = { + Chan:inState.Chan, + Freq:inState.Freq, + Stim:inState.Stim, + Errs:inState.Errs, + Pick:inState.Pick, + Show:inState.Show + }; + localStorage.setItem("app-settings", JSON.stringify(clone)); +}; + +export const Initial = Reducer( +{ + ...SettingsActual, + Test: TestActual, Live: { Test: undefined, @@ -326,15 +336,8 @@ export const Initial = Reducer( UserR:{Points:[], Paths:[]}, TestL:{Points:[], Paths:[]}, TestR:{Points:[], Paths:[]} - }, - Show: - { - Cursor:true, - Answer:false - }, - TestIndex: 0, - Test: TestActual -}, {Name:"Test", Data:0}); + } +}, {Name:"Test", Data:SettingsActual.Pick}); export const Context = React.createContext(/** @type {Store.Binding} */([Initial, (_a)=>{}])); diff --git a/js/ui.js b/js/ui.js index 6d358d7..19b36c1 100644 --- a/js/ui.js +++ b/js/ui.js @@ -53,7 +53,7 @@ export const Header =()=>
- ${State.Test.map((t, i)=>html``)}
diff --git a/ts/store.d.ts b/ts/store.d.ts index 8f4aae8..cdde39f 100644 --- a/ts/store.d.ts +++ b/ts/store.d.ts @@ -13,9 +13,9 @@ declare namespace Store { }; type Test = { - Name: string; + Name : string; Done?: Grade; - Plot: Array + Plot : Array }; type Context = { @@ -24,18 +24,28 @@ declare namespace Store { Mark?: TestFrequencySample; }; - type State = { + type StatePartSimple = + { Chan: Range; Freq: Range; Stim: Range; Errs: number; + Pick: number; + Show: + { + Cursor:boolean, + Answer:boolean + } + }; + type StatePartComplex = + { Live: Context; Draw: DrawChart; - Show: {Cursor:boolean, Answer:boolean} - TestIndex: number; Test: Array; }; + type State = StatePartSimple & StatePartComplex; + type ActionMark = { Name: "Mark"; Data: boolean | null }; type ActionTest = { Name: "Test"; Data: number }; type ActionChan = { Name: "Chan"; Data: number };