From ef1e9c9ae84d7a4517feff15b46152d7c014c61d Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sat, 10 Feb 2024 10:19:56 -0500 Subject: [PATCH] add a test --- e2e/test.js | 2 +- package.json | 1 + src/lib/stores/index.js | 20 +++++- src/routes/+layout.svelte | 9 ++- src/routes/+page.svelte | 3 +- src/routes/AppTop.svelte | 18 ++++- src/routes/Game.svelte | 73 ++++++++++++++------- src/routes/GameList.svelte | 2 + src/routes/cart/+page.svelte | 62 ++++++++++++++++++ src/routes/cart/Game.svelte | 113 ++++++++++++++++++++++++++++++++ src/routes/cart/GameList.svelte | 107 ++++++++++++++++++++++++++++++ 11 files changed, 380 insertions(+), 30 deletions(-) create mode 100644 src/routes/cart/+page.svelte create mode 100644 src/routes/cart/Game.svelte create mode 100644 src/routes/cart/GameList.svelte diff --git a/e2e/test.js b/e2e/test.js index 9a28c2b..0b00dcf 100644 --- a/e2e/test.js +++ b/e2e/test.js @@ -1,6 +1,6 @@ import { expect, test } from '@playwright/test'; -for (const url of ['/', '/stats/', '/about/']) { +for (const url of ['/', '/stats/', '/about/', '/cart/']) { test(url + ' render', async ({ page }) => { await page.goto(url); await expect( diff --git a/package.json b/package.json index 0fdaddc..81a575e 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "better-sqlite3": "^9.3.0", "cheerio": "1.0.0-rc.12", "node-fetch": "^3.3.2", + "remeda": "^1.40.1", "svelte-persisted-store": "^0.9.0", "vite-multiple-assets": "^1.2.10" } diff --git a/src/lib/stores/index.js b/src/lib/stores/index.js index d10d3d8..3833ad5 100644 --- a/src/lib/stores/index.js +++ b/src/lib/stores/index.js @@ -40,12 +40,26 @@ games_hidden_store.subscribe((data) => localStorage.setItem('games_hidden', JSON.stringify(data)), ); +let cart_store_stored = localStorage.getItem('cart'); +if (cart_store_stored) cart_store_stored = JSON.parse(cart_store_stored); +export const cart_store = writable(cart_store_stored || {}); +cart_store.subscribe((data) => + localStorage.setItem('cart', JSON.stringify(data)), +); + export const games_store = derived( - [_games_store, games_hidden_store], - ([$games, $hidden]) => - $games.map((game) => ({ ...game, is_hidden: $hidden[game.id] })), + [_games_store, games_hidden_store, cart_store], + ([$games, $hidden, $cart]) => + $games.map((game) => ({ + ...game, + is_hidden: $hidden[game.id], + is_in_cart: $cart[game.id], + })), ); games_store.toggle_hidden = (id) => { games_hidden_store.update(u.updateIn(id, (v) => !v)); }; +games_store.toggle_cart = (id) => { + cart_store.update(u.updateIn(id, (v) => !v)); +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 3b8afa5..2314b49 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -2,7 +2,7 @@ Ottawa board games, trades and sales
- +
@@ -15,9 +15,14 @@ import { persisted } from 'svelte-persisted-store'; import u from '@yanick/updeep-remeda'; import AppTop from './AppTop.svelte'; - import { games_store, sellers_store } from '$lib/stores/index.js'; + import { + games_store, + sellers_store, + cart_store, + } from '$lib/stores/index.js'; setContext('games', games_store); setContext('sellers', sellers_store); + setContext('cart', cart_store); diff --git a/src/routes/GameList.svelte b/src/routes/GameList.svelte index 372d45e..662e306 100644 --- a/src/routes/GameList.svelte +++ b/src/routes/GameList.svelte @@ -21,6 +21,7 @@ {#if show_hidden || !game.is_hidden} toggle_visibility(game.id)} + on:toggle_cart={() => toggle_cart(game.id)} {...game} seller={sellers[game.username]} />
@@ -41,6 +42,7 @@ const dispatch = createEventDispatcher(); const toggle_visibility = (id) => dispatch('toggle_game_visibility', id); + const toggle_cart = (id) => dispatch('toggle_cart', id); // add filter // add sort (user, game, price) diff --git a/src/routes/cart/+page.svelte b/src/routes/cart/+page.svelte new file mode 100644 index 0000000..d12dec6 --- /dev/null +++ b/src/routes/cart/+page.svelte @@ -0,0 +1,62 @@ +
+ {#if games.length == 0} +
+
+

Such restraint

+
No games in cart (yet)
+
+
+ {:else} + all_games.toggle_cart(detail)} /> + {/if} +
+ + + + diff --git a/src/routes/cart/Game.svelte b/src/routes/cart/Game.svelte new file mode 100644 index 0000000..2eea02d --- /dev/null +++ b/src/routes/cart/Game.svelte @@ -0,0 +1,113 @@ +
+
+
+ + +
+
+
+
+ + + {name} + +
+
+
+
+ {price ? '$' + price : ''} +
+
+ {notes} +
+
+ {#if updated_at} + {pretty_date(updated_at)} + {/if} +
+
+ +
+
+
+
+ + + + diff --git a/src/routes/cart/GameList.svelte b/src/routes/cart/GameList.svelte new file mode 100644 index 0000000..892a60e --- /dev/null +++ b/src/routes/cart/GameList.svelte @@ -0,0 +1,107 @@ +{#each games_sellers as seller} +

+ +   + + email + + {sellers[seller]?.neighbourhood ?? ''} + + {games.filter((game) => game.username == seller).length} game(s) +

+
+ {#each games.filter((game) => { + return game.username == seller; + }) as game} + {#if show_hidden || !game.is_hidden} + toggle_visibility(game.id)} + on:toggle_cart={() => toggle_cart(game.id)} + {...game} + seller={sellers[game.username]} /> +
+ {/if} + {/each} +
+{/each} + + + +