save state to localStorage

This commit is contained in:
Yanick Champoux 2023-04-15 17:11:17 -04:00
parent da4b5b318e
commit 948fe96e0a
5 changed files with 16 additions and 10 deletions

View File

@ -25,7 +25,6 @@
export let rating = 0;
export let api = getContext("api");
console.log(api?.dispatch?.setDrive);
$: api?.dispatch?.setDrive?.({ rating, advanced });
</script>

View File

@ -1,9 +1,20 @@
import ship from "./ship";
import { browser } from "$app/environment";
export type Api = ReturnType<typeof ship.createStore>;
export const createApi = () => {
const api = ship.createStore();
console.log(api);
return api;
const state = browser
? JSON.parse(localStorage.getItem("ship") || "null")
: undefined;
const api = ship.createStore(state || undefined);
if (browser) {
api.subscribe(() => {
localStorage.setItem("ship", JSON.stringify(api.getState()));
});
}
return api;
};

View File

@ -100,11 +100,6 @@ shipDux.addReaction((api) => {
};
});
shipDux.addEffect((api) => (next) => (action) => {
console.log(action);
next(action);
});
shipDux.addReaction((api) =>
createSelector(
api.selectors.getShipMass,

1
src/routes/+layout.js Normal file
View File

@ -0,0 +1 @@
export const prerender = true;

View File

@ -8,7 +8,7 @@
export let api = getContext("api");
let ship = {};
api.subscribe(() => {
api?.subscribe(() => {
ship = api.getState();
});
</script>