From da4b5b318e7b68cee2ae726b6e9998a215a060af Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 15 Apr 2023 16:53:53 -0400 Subject: [PATCH 1/3] link the store to shipEdit --- src/lib/store/ship.ts | 4 +++- src/routes/+page.svelte | 11 ++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib/store/ship.ts b/src/lib/store/ship.ts index 1fc8f0e..2cf8951 100644 --- a/src/lib/store/ship.ts +++ b/src/lib/store/ship.ts @@ -18,7 +18,9 @@ import { fireconsDux } from "./ship/weaponry/firecons"; import { adfcDux } from "./ship/weaponry/adfc"; import { weaponsDux } from "./ship/weaponry/weapons"; -process.env.UPDEEP_MODE = "dangerously_never_freeze"; +if (typeof process !== "undefined") { + process.env.UPDEEP_MODE = "dangerously_never_freeze"; +} const structure = new Updux({ initialState: {}, diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 8830a2f..b6d78ab 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1,5 +1,14 @@ - + From 948fe96e0af0fd826c88e5739b6606cf8c1242eb Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 15 Apr 2023 17:11:17 -0400 Subject: [PATCH 2/3] 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(); }); From 88c08a8b79368b3054140eaba79ec5820f564d3f Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 16 Apr 2023 11:52:36 -0400 Subject: [PATCH 3/3] add a test --- src/routes/page.test.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/routes/page.test.js b/src/routes/page.test.js index 895c08d..280358b 100644 --- a/src/routes/page.test.js +++ b/src/routes/page.test.js @@ -1,9 +1,27 @@ -import { render, fireEvent, screen, getByText } from "@testing-library/svelte"; +import { render, fireEvent } from "@testing-library/svelte"; import "@testing-library/jest-dom"; +import { tick } from "svelte"; import Page from "./+page.svelte"; +import { createApi } from "$lib/store/api.ts"; test("we have a page", () => { const { getByText } = render(Page); expect(getByText("propulsion")).toBeInTheDocument(); }); + +test("we can pass a store", async () => { + const context = new Map(); + const api = createApi(); + context.set("api", api); + + api.dispatch.updateIdentification({ shipClass: "Bonobo" }); + + const { getByPlaceholderText } = render(Page, { context }); + + const classInput = getByPlaceholderText("ship class"); + await fireEvent.input(classInput, { target: { value: "Tarzan" } }); + expect(classInput.value).toEqual("Tarzan"); + + expect(api.getState().identification.shipClass).toBe("Tarzan"); +});