add a potential initialState to the store (for testing)

main
Yanick Champoux 2022-04-10 17:41:50 -04:00
parent 24b07c3557
commit 012db36e65
1 changed files with 22 additions and 25 deletions

View File

@ -8,36 +8,33 @@ import { initial } from "lodash";
let composeEnhancers = compose;
if (dev && browser && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) {
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
}
export default () => {
export default (initialState = undefined) => {
if (browser) {
const i = localStorage.getItem("ship");
let initialState = undefined;
if (i) initialState = JSON.parse(localStorage.getItem("ship"));
}
if( browser ) {
const i =localStorage.getItem('ship');
const duxStore = shipDux.createStore(initialState, (mw) =>
composeEnhancers(applyMiddleware(mw))
);
if(i) initialState = JSON.parse(localStorage.getItem('ship'));
}
const duxStore = shipDux.createStore(initialState, (mw) =>
composeEnhancers(applyMiddleware(mw))
);
let previous;
const state = readable(duxStore.getState(), (set) => {
duxStore.subscribe(() => {
if (previous === duxStore.getState()) return;
previous = duxStore.getState();
set(previous);
if( browser ) localStorage.setItem('ship', JSON.stringify(previous));
});
let previous;
const state = readable(duxStore.getState(), (set) => {
duxStore.subscribe(() => {
if (previous === duxStore.getState()) return;
previous = duxStore.getState();
set(previous);
if (browser) localStorage.setItem("ship", JSON.stringify(previous));
});
});
return {
dispatch: duxStore.dispatch,
state,
shipMass: derived(state, (state) => state.reqs.mass),
};
return {
dispatch: duxStore.dispatch,
state,
shipMass: derived(state, (state) => state.reqs.mass),
};
};