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/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 new file mode 100644 index 0000000..1daf4a3 --- /dev/null +++ b/src/lib/store/api.js @@ -0,0 +1,44 @@ +import Dexie, { liveQuery } from 'dexie'; +import { writable, derived } 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; + }); + + 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/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); +}); 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
+
+ +