persist settings

This commit is contained in:
Seth Trowbridge 2023-06-07 21:09:10 -04:00
parent ed49a67933
commit 34b9297306
3 changed files with 49 additions and 36 deletions

View File

@ -161,7 +161,7 @@ export function Reducer(inState, inAction)
const test = clone.Test[Data]; const test = clone.Test[Data];
if(test) if(test)
{ {
clone.TestIndex = Data; clone.Pick = Data;
clone.Live = Reselect(clone, test); clone.Live = Reselect(clone, test);
clone.Draw = clone.Draw =
{ {
@ -227,6 +227,8 @@ export function Reducer(inState, inAction)
clone.Show.Answer = Data; clone.Show.Answer = Data;
} }
SaveSettings(clone);
return clone; return clone;
} }
@ -286,34 +288,42 @@ const TestDefault = [
] ]
} }
]; ];
/** @type {Store.Test[]} */
/** @type {Store.Test[] | string | null } */ const TestActual = JSON.parse(localStorage.getItem("app-tests")||"false") || TestDefault;
let TestSaved = localStorage.getItem("app-tests");
if(TestSaved)
{
TestSaved = JSON.parse(TestSaved);
}
/**@type {(inState:Store.State)=>void} */ /**@type {(inState:Store.State)=>void} */
const SaveTests =(inState)=> localStorage.setItem("app-tests", JSON.stringify(inState.Test)); const SaveTests =(inState)=> localStorage.setItem("app-tests", JSON.stringify(inState.Test));
/**@type {(inState:Store.State)=>void} */ /** @type {Store.StatePartSimple} */
const SaveSettings =(inState)=> const SettingsDefault =
{
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(
{ {
Chan: { Min:0, Max:1, Value:0, Step:1 }, Chan: { Min:0, Max:1, Value:0, Step:1 },
Freq: { Min:2, Max:8, Value:3, Step:1 }, Freq: { Min:2, Max:8, Value:3, Step:1 },
Stim: { Min:-10, Max:120, Value:30, Step:5 }, Stim: { Min:-10, Max:120, Value:30, Step:5 },
Errs: 0, 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: Live:
{ {
Test: undefined, Test: undefined,
@ -326,15 +336,8 @@ export const Initial = Reducer(
UserR:{Points:[], Paths:[]}, UserR:{Points:[], Paths:[]},
TestL:{Points:[], Paths:[]}, TestL:{Points:[], Paths:[]},
TestR:{Points:[], Paths:[]} TestR:{Points:[], Paths:[]}
}, }
Show: }, {Name:"Test", Data:SettingsActual.Pick});
{
Cursor:true,
Answer:false
},
TestIndex: 0,
Test: TestActual
}, {Name:"Test", Data:0});
export const Context = React.createContext(/** @type {Store.Binding} */([Initial, (_a)=>{}])); export const Context = React.createContext(/** @type {Store.Binding} */([Initial, (_a)=>{}]));

View File

@ -53,7 +53,7 @@ export const Header =()=>
<div class="p-4 flex-1"> <div class="p-4 flex-1">
<div class="box-buttons w-full"> <div class="box-buttons w-full">
<select id="test-select" class="w-full px-2 py-2 rounded-lg border(1 slate-200) font-bold text(xl white) cursor-pointer bg-earmark" value=${State.TestIndex} onChange=${handleChange}> <select id="test-select" class="w-full px-2 py-2 rounded-lg border(1 slate-200) font-bold text(xl white) cursor-pointer bg-earmark" value=${State.Pick} onChange=${handleChange}>
${State.Test.map((t, i)=>html`<option class="text-black" value=${i}>${t.Name}</option>`)} ${State.Test.map((t, i)=>html`<option class="text-black" value=${i}>${t.Name}</option>`)}
</select> </select>
</div> </div>

20
ts/store.d.ts vendored
View File

@ -13,9 +13,9 @@ declare namespace Store {
}; };
type Test = { type Test = {
Name: string; Name : string;
Done?: Grade; Done?: Grade;
Plot: Array<TestFrequency> Plot : Array<TestFrequency>
}; };
type Context = { type Context = {
@ -24,18 +24,28 @@ declare namespace Store {
Mark?: TestFrequencySample; Mark?: TestFrequencySample;
}; };
type State = { type StatePartSimple =
{
Chan: Range; Chan: Range;
Freq: Range; Freq: Range;
Stim: Range; Stim: Range;
Errs: number; Errs: number;
Pick: number;
Show:
{
Cursor:boolean,
Answer:boolean
}
};
type StatePartComplex =
{
Live: Context; Live: Context;
Draw: DrawChart; Draw: DrawChart;
Show: {Cursor:boolean, Answer:boolean}
TestIndex: number;
Test: Array<Test>; Test: Array<Test>;
}; };
type State = StatePartSimple & StatePartComplex;
type ActionMark = { Name: "Mark"; Data: boolean | null }; type ActionMark = { Name: "Mark"; Data: boolean | null };
type ActionTest = { Name: "Test"; Data: number }; type ActionTest = { Name: "Test"; Data: number };
type ActionChan = { Name: "Chan"; Data: number }; type ActionChan = { Name: "Chan"; Data: number };