bgg-ottawa-sell-club/src/lib/stores/index.js

52 lines
1.4 KiB
JavaScript

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));
};