Merge branch 'improve-the-store'

This commit is contained in:
Yanick Champoux 2024-02-09 12:49:25 -05:00
commit cdb0bfaca8
11 changed files with 120 additions and 76 deletions

1
.envrc
View File

@ -1 +1,2 @@
export DATABASE_URL=sqlite3:./games.db export DATABASE_URL=sqlite3:./games.db
export VITE_DB_DIR=/dev/

View File

@ -6,6 +6,14 @@ vars:
GREETING: Hello, World! GREETING: Hello, World!
tasks: tasks:
preview:
deps: [build]
cmds:
- npm exec vite preview
test:e2e:
deps: [build]
cmds:
- npm exec playwright test
export-db: export-db:
cmds: cmds:
- sqlite3 /home/bggsell/games.db '.mode json' '.once /home/bggsell/db/games.json' 'select * from game' - sqlite3 /home/bggsell/games.db '.mode json' '.once /home/bggsell/db/games.json' 'select * from game'

20
e2e/hide-game.test.js Normal file
View File

@ -0,0 +1,20 @@
import { test, expect } from '@playwright/test';
test('hide game', async ({ page }) => {
await page.goto('/');
const nbr_games = await page
.locator('.games > div')
.all()
.then((games) => games.length);
await page.getByRole('button', { name: 'visibility' }).first().click();
await expect(async () => {
const one_hidden = await page
.locator('.games > div')
.all()
.then((games) => games.length);
expect(one_hidden).toEqual(nbr_games - 1);
}).toPass({ timeout: 10_000 });
});

12
e2e/test.js Normal file
View File

@ -0,0 +1,12 @@
import { expect, test } from '@playwright/test';
for (const url of ['/', '/stats/', '/about/']) {
test(url + ' render', async ({ page }) => {
await page.goto(url);
await expect(
page.getByRole('heading', {
name: 'Ottawa board games for sale and trade',
}),
).toBeVisible();
});
}

View File

@ -16,7 +16,7 @@
}, },
"devDependencies": { "devDependencies": {
"@changesets/cli": "^2.27.1", "@changesets/cli": "^2.27.1",
"@playwright/test": "^1.28.1", "@playwright/test": "^1.41.2",
"@sveltejs/kit": "^2.5.0", "@sveltejs/kit": "^2.5.0",
"@sveltejs/vite-plugin-svelte": "^3.0.0", "@sveltejs/vite-plugin-svelte": "^3.0.0",
"@types/eslint": "8.56.0", "@types/eslint": "8.56.0",

View File

@ -1,11 +1,12 @@
/** @type {import('@playwright/test').PlaywrightTestConfig} */ /** @type {import('@playwright/test').PlaywrightTestConfig} */
const config = { const config = {
webServer: { webServer: {
command: 'npm run build && npm run preview', command: 'npm exec vite preview',
port: 4173 port: 4173,
reuseExistingServer: true,
}, },
testDir: 'tests', testDir: 'e2e',
testMatch: /(.+\.)?(test|spec)\.[jt]s/ testMatch: /(.+\.)?(test|spec)\.[jt]s/,
}; };
export default config; export default config;

51
src/lib/stores/index.js Normal file
View File

@ -0,0 +1,51 @@
import { readable, writable, derived } from 'svelte/store';
import { dev, browser } from '$app/environment';
import u from '@yanick/updeep-remeda';
const prefix = import.meta.env.VITE_DB_DIR ?? '/db/';
console.log({ prefix });
const _games_store = readable([], (set) => {
if (!browser) return;
fetch(prefix + 'games.json')
.then((doc) => doc.json())
.then((games) =>
games.map((game) => ({
...game,
id: [game.username, game.bgg_id].join('!'),
})),
)
.then(set);
});
export const sellers_store = readable({}, (set) => {
if (!browser) return;
fetch(prefix + 'sellers.json')
.then((doc) => doc.json())
.then((sellers_list) =>
set(
Object.fromEntries(
sellers_list.map((seller) => [seller.username, seller]),
),
),
);
});
let stored = localStorage.getItem('games_hidden');
if (stored) stored = JSON.parse(stored);
const games_hidden_store = writable(stored || {});
games_hidden_store.subscribe((data) =>
localStorage.setItem('games_hidden', 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.toggle_hidden = (id) => {
games_hidden_store.update(u.updateIn(id, (v) => !v));
};

View File

@ -15,49 +15,9 @@
import { persisted } from 'svelte-persisted-store'; import { persisted } from 'svelte-persisted-store';
import u from '@yanick/updeep-remeda'; import u from '@yanick/updeep-remeda';
import AppTop from './AppTop.svelte'; import AppTop from './AppTop.svelte';
import { games_store, sellers_store } from '$lib/stores/index.js';
const prefix = dev ? '/dev/' : '/db/'; setContext('games', games_store);
setContext('sellers', sellers_store);
const games = readable([], (set) => {
if (!browser) return () => {};
fetch(prefix + 'games.json')
.then((doc) => doc.json())
.then(set);
return () => {};
});
setContext('games', games);
const sellers = readable({}, (set) => {
if (!browser) return () => {};
const sellers_list = fetch(prefix + 'sellers.json')
.then((doc) => doc.json())
.then((sellers_list) =>
set(
Object.fromEntries(
sellers_list.map((seller) => [seller.username, seller]),
),
),
);
return () => {};
});
setContext('sellers', sellers);
const games_hidden = writable(
JSON.parse(localStorage.getItem('games_hidden') || '{}'),
);
games_hidden.toggle = (username, bgg_id) => {
console.log({ username, bgg_id });
games_hidden.update(
u.updateIn([username, bgg_id].join('!'), (v) => !v),
);
};
setContext('games_hidden', games_hidden);
games_hidden.subscribe((data) =>
localStorage.setItem('games_hidden', JSON.stringify(data)),
);
</script> </script>
<style> <style>

View File

@ -1,5 +1,5 @@
<article> <article>
{#await $games} {#if $games.length == 0}
<div class="medium-height middle-align center-align"> <div class="medium-height middle-align center-align">
<div class="center-align"> <div class="center-align">
<progress class="circle"></progress> <progress class="circle"></progress>
@ -7,9 +7,13 @@
<h5>gathering games...</h5> <h5>gathering games...</h5>
</div> </div>
</div> </div>
{:then games} {:else}
<GameList {games} sellers={$sellers} /> <GameList
{/await} games={$games}
sellers={$sellers}
on:toggle_game_visibility={({ detail }) =>
games.toggle_hidden(detail)} />
{/if}
</article> </article>
<script> <script>

View File

@ -18,12 +18,10 @@
.toLowerCase() .toLowerCase()
.includes(target); .includes(target);
}) as game} }) as game}
{#if show_hidden || !$games_hidden[[game.username, game.bgg_id].join('!')]} {#if show_hidden || !game.is_hidden}
<div <div
transition:slide={{ delay: 250, duration: 300 }} transition:slide={{ delay: 250, duration: 300 }}
class:hidden={$games_hidden[ class:hidden={game.is_hidden}>
[game.username, game.bgg_id].join('!')
]}>
<div class="grid top-align"> <div class="grid top-align">
<a <a
class="s12 m2" class="s12 m2"
@ -75,13 +73,8 @@
<button <button
class="transparent round" class="transparent round"
title="hide game" title="hide game"
on:click={() => on:click={() => toggle_visibility(game.id)}
toggle_game_visibility( >{#if game.is_hidden}<i>visibility_off</i
game.username,
game.bgg_id,
)}
>{#if $games_hidden[[game.username, game.bgg_id].join('!')]}<i
>visibility_off</i
>{:else} >{:else}
<i>visibility</i> <i>visibility</i>
{/if} {/if}
@ -97,16 +90,19 @@
</div> </div>
<script> <script>
import { getContext } from 'svelte'; import { getContext, createEventDispatcher } from 'svelte';
import { slide, fade } from 'svelte/transition'; import { slide, fade } from 'svelte/transition';
export let games; export let games = [];
export let sellers = {}; export let sellers = {};
let show_hidden = false; let show_hidden = false;
let search_text = ''; let search_text = '';
const dispatch = createEventDispatcher();
const toggle_visibility = (id) => dispatch('toggle_game_visibility', id);
// add filter // add filter
// add sort (user, game, price) // add sort (user, game, price)
@ -115,9 +111,6 @@
return date.replace(/T.*/, ''); return date.replace(/T.*/, '');
} }
const games_hidden = getContext('games_hidden');
const toggle_game_visibility = games_hidden.toggle;
</script> </script>
<style> <style>

View File

@ -1,6 +0,0 @@
import { expect, test } from '@playwright/test';
test('index page has expected h1', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('heading', { name: 'Welcome to SvelteKit' })).toBeVisible();
});