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];
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)=>{}]));

View File

@ -53,7 +53,7 @@ export const Header =()=>
<div class="p-4 flex-1">
<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>`)}
</select>
</div>

20
ts/store.d.ts vendored
View File

@ -13,9 +13,9 @@ declare namespace Store {
};
type Test = {
Name: string;
Name : string;
Done?: Grade;
Plot: Array<TestFrequency>
Plot : Array<TestFrequency>
};
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<Test>;
};
type State = StatePartSimple & StatePartComplex;
type ActionMark = { Name: "Mark"; Data: boolean | null };
type ActionTest = { Name: "Test"; Data: number };
type ActionChan = { Name: "Chan"; Data: number };