From db8bb6ccc0c8edc329a34fcbc1bf81532c6b237d Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 12 Jan 2023 14:20:54 -0500 Subject: [PATCH 1/2] add the api store --- package.json | 4 +++- src/lib/store/api.js | 32 ++++++++++++++++++++++++++++++++ src/lib/store/api.test.js | 28 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/lib/store/api.js create mode 100644 src/lib/store/api.test.js diff --git a/package.json b/package.json index beb1c72..28093f7 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,13 @@ "svelte": "^3.54.0", "svelte-check": "^2.9.2", "vite": "^4.0.0", - "vitest": "^0.25.3" + "vitest": "^0.25.8" }, "type": "module", "dependencies": { "beercss": "^3.0.4", + "dexie": "^3.2.2", + "fake-indexeddb": "^4.0.1", "material-dynamic-colors": "^0.1.5" } } diff --git a/src/lib/store/api.js b/src/lib/store/api.js new file mode 100644 index 0000000..236a607 --- /dev/null +++ b/src/lib/store/api.js @@ -0,0 +1,32 @@ +import Dexie, { liveQuery } from 'dexie'; +import { writable } from 'svelte/store'; + +const seedCampaign = { + battles: [], +}; + +export function genApi(options = {}) { + const storage = new Dexie('Campaigns', options.dexie ?? {}); + + storage.version(1).stores({ + campaigns: '++id,name', + }); + + const campaigns = liveQuery(async () => { + const c = []; + await storage.campaigns + .toCollection() + .each(({ id, name }) => c.push({ id, name })); + return c; + }); + + return { + campaigns, + storage, + event: { + addCampaign: (name) => + storage.campaigns.add({ name, ...seedCampaign }), + deleteCampaign: (id) => storage.campaigns.delete(id), + }, + }; +} diff --git a/src/lib/store/api.test.js b/src/lib/store/api.test.js new file mode 100644 index 0000000..912295d --- /dev/null +++ b/src/lib/store/api.test.js @@ -0,0 +1,28 @@ +import { test, expect } from 'vitest'; +//import { indexedDB, IDBKeyRange } from 'fake-indexeddb'; +import 'fake-indexeddb/auto'; + +import { genApi } from './api.js'; + +const get = async (store) => new Promise((resolve) => store.subscribe(resolve)); + +test('create and add and remove campaigns', async () => { + const api = genApi(); //{ dexie: { indexedDB, IDBKeyRange } }); + + api.event.addCampaign('C1'); + api.event.addCampaign('C2'); + + let result = await get(api.campaigns); + + expect(result).toHaveLength(2); + expect(result[0]).toEqual({ + id: 1, + name: 'C1', + }); + + api.event.deleteCampaign(1); + + result = await get(api.campaigns); + + expect(result).toHaveLength(1); +}); From ef0da12a045d459ecad3332037d751b964768853 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 12 Jan 2023 16:42:34 -0500 Subject: [PATCH 2/2] add the api store --- src/lib/components/Campaigns.svelte | 46 +++++++++++++++++-- src/lib/store/api.js | 14 +++++- src/routes/+layout.svelte | 21 +++++++++ src/routes/+page.svelte | 12 +++++ src/routes/campaign/[campaignId]/+layout.js | 4 ++ .../campaign/[campaignId]/+layout.svelte | 14 ++++++ src/routes/campaign/[campaignId]/+page.svelte | 12 +++++ 7 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 src/routes/+layout.svelte create mode 100644 src/routes/+page.svelte create mode 100644 src/routes/campaign/[campaignId]/+layout.js create mode 100644 src/routes/campaign/[campaignId]/+layout.svelte create mode 100644 src/routes/campaign/[campaignId]/+page.svelte diff --git a/src/lib/components/Campaigns.svelte b/src/lib/components/Campaigns.svelte index bb94bd3..83550de 100644 --- a/src/lib/components/Campaigns.svelte +++ b/src/lib/components/Campaigns.svelte @@ -4,11 +4,15 @@ - {#each campaigns as campaign (campaign.name)} + {#each campaigns as campaign (campaign.id)}
south_america
-
{campaign.name}
+
+ + {campaign.name} + +

chapter 2, second battle.

+ + +
+ @@ -37,4 +74,7 @@ h5 { flex: 1; } + article { + height: 90vh; + } diff --git a/src/lib/store/api.js b/src/lib/store/api.js index 236a607..1daf4a3 100644 --- a/src/lib/store/api.js +++ b/src/lib/store/api.js @@ -1,5 +1,5 @@ import Dexie, { liveQuery } from 'dexie'; -import { writable } from 'svelte/store'; +import { writable, derived } from 'svelte/store'; const seedCampaign = { battles: [], @@ -20,13 +20,25 @@ export function genApi(options = {}) { return c; }); + const activeCampaignId = writable(); + + const activeCampaign = derived(activeCampaignId, async (id, set) => { + const x = await storage.campaigns.get({ id }); + console.log({ x }); + set(x); + }); + return { campaigns, + activeCampaign, storage, event: { addCampaign: (name) => storage.campaigns.add({ name, ...seedCampaign }), deleteCampaign: (id) => storage.campaigns.delete(id), + setActiveCampaign: async (id) => { + activeCampaignId.set(id); + }, }, }; } diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte new file mode 100644 index 0000000..84f03e3 --- /dev/null +++ b/src/routes/+layout.svelte @@ -0,0 +1,21 @@ + +
+ +
+ + + + diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte new file mode 100644 index 0000000..17a775f --- /dev/null +++ b/src/routes/+page.svelte @@ -0,0 +1,12 @@ + + + + + diff --git a/src/routes/campaign/[campaignId]/+layout.js b/src/routes/campaign/[campaignId]/+layout.js new file mode 100644 index 0000000..bb41680 --- /dev/null +++ b/src/routes/campaign/[campaignId]/+layout.js @@ -0,0 +1,4 @@ +export function load({ params }) { + params.campaignId = parseInt(params?.campaignId); + return params; +} diff --git a/src/routes/campaign/[campaignId]/+layout.svelte b/src/routes/campaign/[campaignId]/+layout.svelte new file mode 100644 index 0000000..552dddd --- /dev/null +++ b/src/routes/campaign/[campaignId]/+layout.svelte @@ -0,0 +1,14 @@ + + + + + diff --git a/src/routes/campaign/[campaignId]/+page.svelte b/src/routes/campaign/[campaignId]/+page.svelte new file mode 100644 index 0000000..32a8f1f --- /dev/null +++ b/src/routes/campaign/[campaignId]/+page.svelte @@ -0,0 +1,12 @@ +
+
{$activeCampaign.name}
+
Chapter 1
+
+ +