From 948fe96e0af0fd826c88e5739b6606cf8c1242eb Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 15 Apr 2023 17:11:17 -0400 Subject: [PATCH] save state to localStorage --- .../ShipEdit/Propulsion/Engine.svelte | 1 - src/lib/store/api.ts | 17 ++++++++++++++--- src/lib/store/ship.ts | 5 ----- src/routes/+layout.js | 1 + src/routes/+page.svelte | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 src/routes/+layout.js diff --git a/src/lib/components/ShipEdit/Propulsion/Engine.svelte b/src/lib/components/ShipEdit/Propulsion/Engine.svelte index 2925a6f..27f2988 100644 --- a/src/lib/components/ShipEdit/Propulsion/Engine.svelte +++ b/src/lib/components/ShipEdit/Propulsion/Engine.svelte @@ -25,7 +25,6 @@ export let rating = 0; export let api = getContext("api"); - console.log(api?.dispatch?.setDrive); $: api?.dispatch?.setDrive?.({ rating, advanced }); diff --git a/src/lib/store/api.ts b/src/lib/store/api.ts index a3b87eb..a90e647 100644 --- a/src/lib/store/api.ts +++ b/src/lib/store/api.ts @@ -1,9 +1,20 @@ import ship from "./ship"; +import { browser } from "$app/environment"; export type Api = ReturnType; 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; }; diff --git a/src/lib/store/ship.ts b/src/lib/store/ship.ts index 2cf8951..fca0fcc 100644 --- a/src/lib/store/ship.ts +++ b/src/lib/store/ship.ts @@ -100,11 +100,6 @@ shipDux.addReaction((api) => { }; }); -shipDux.addEffect((api) => (next) => (action) => { - console.log(action); - next(action); -}); - shipDux.addReaction((api) => createSelector( api.selectors.getShipMass, diff --git a/src/routes/+layout.js b/src/routes/+layout.js new file mode 100644 index 0000000..189f71e --- /dev/null +++ b/src/routes/+layout.js @@ -0,0 +1 @@ +export const prerender = true; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index b6d78ab..5e8a0f9 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -8,7 +8,7 @@ export let api = getContext("api"); let ship = {}; - api.subscribe(() => { + api?.subscribe(() => { ship = api.getState(); });