Adding collection route, header path to collection, minimal view of game for collection, and move save/clear to utils.

This commit is contained in:
Bradley Shellnut 2022-08-29 11:33:51 -05:00
parent e228c6f6e4
commit eaf286b5c9
6 changed files with 51 additions and 42 deletions

View file

@ -5,7 +5,8 @@
import { collectionStore } from '$lib/stores/collectionStore'; import { collectionStore } from '$lib/stores/collectionStore';
import { addToCollection, removeFromCollection } from '$lib/util/manipulateCollection'; import { addToCollection, removeFromCollection } from '$lib/util/manipulateCollection';
export let game: GameType; export let game: GameType | SavedGameType;
export let minimal: boolean = false;
export let detailed: boolean = false; export let detailed: boolean = false;
$: existsInCollection = $collectionStore.find((item: SavedGameType) => item.id === game.id); $: existsInCollection = $collectionStore.find((item: SavedGameType) => item.id === game.id);
</script> </script>
@ -18,22 +19,32 @@
</a> </a>
</div> </div>
<div class="game-details"> {#if !minimal && game?.year_published && game.players && game.max_players && game.playtime}
<p>{game.year_published}</p> <div class="game-details">
<p>{game.players} {game.max_players === 1 ? 'player' : 'players'}</p> <p>{game.year_published}</p>
<p>{game.playtime} minutes</p> <p>{game.players} {game.max_players === 1 ? 'player' : 'players'}</p>
<p>Minimum Age: {game.min_age}</p> <p>{game.playtime} minutes</p>
<a href={`/game/${game.id}`}>View Game</a> <p>Minimum Age: {game.min_age}</p>
{#if detailed} <a href={`/game/${game.id}`}>View Game</a>
<div class="description">{@html game.description}</div> {#if detailed}
{/if} <div class="description">{@html game.description}</div>
</div> {/if}
</div>
{/if}
{#if existsInCollection} {#if existsInCollection}
<button aria-label="Remove from collection" class="btn" type="button" on:click={() => removeFromCollection(game)} <button
aria-label="Remove from collection"
class="btn"
type="button"
on:click={() => removeFromCollection(game)}
>Remove <MinusCircleIcon width="24" height="24" /></button >Remove <MinusCircleIcon width="24" height="24" /></button
> >
{:else} {:else}
<button aria-label="Add to collection" class="btn" type="button" on:click={() => addToCollection(game)} <button
aria-label="Add to collection"
class="btn"
type="button"
on:click={() => addToCollection(game)}
>Add to collection <PlusCircleIcon width="24" height="24" /></button >Add to collection <PlusCircleIcon width="24" height="24" /></button
> >
{/if} {/if}
@ -67,11 +78,11 @@
.game-container { .game-container {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
@media (max-width: 650px) { @media (max-width: 650px) {
max-width: none; max-width: none;
} }
gap: var(--spacing-16); gap: var(--spacing-16);
padding: var(--spacing-16) var(--spacing-16); padding: var(--spacing-16) var(--spacing-16);
transition: all 0.3s; transition: all 0.3s;

View file

@ -1,22 +1,18 @@
<script lang="ts"> <script lang="ts">
import { page } from '$app/stores';
import Themes from '$lib/components/preferences/themes.svelte';
import Profile from '../preferences/profile.svelte'; import Profile from '../preferences/profile.svelte';
import logo from './bored-game.png'; import logo from './bored-game.png';
// let theme: CarbonTheme = "white";
</script> </script>
<header> <header>
<div class="corner"> <div class="corner">
<a href="/"> <a href="/" title="Home">
<img src={logo} alt="Bored Game Home" /> <img src={logo} alt="Bored Game Home" />
</a> </a>
</div> </div>
<nav> <nav>
<!-- <div><Themes /></div> --> <a href="/collection" title="Go to your collection">Your Collection</a>
<div><Profile /></div> <Profile />
</nav> </nav>
</header> </header>
@ -56,6 +52,7 @@
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
gap: 2rem;
margin: 1rem; margin: 1rem;
--background: rgba(255, 255, 255, 0.7); --background: rgba(255, 255, 255, 0.7);
} }
@ -107,7 +104,7 @@
padding: 0 1em; padding: 0 1em;
color: var(--heading-color); color: var(--heading-color);
font-weight: 700; font-weight: 700;
font-size: 0.8rem; /* font-size: 0.8rem; */
text-transform: uppercase; text-transform: uppercase;
letter-spacing: 0.1em; letter-spacing: 0.1em;
text-decoration: none; text-decoration: none;
@ -115,6 +112,7 @@
} }
a:hover { a:hover {
text-decoration: underline;
color: var(--accent-color); color: var(--accent-color);
} }
</style> </style>

View file

@ -1,23 +1,6 @@
<script lang="ts"> <script lang="ts">
import { browser } from '$app/env';
import { collectionStore } from '$root/lib/stores/collectionStore';
import { ToastType } from '$root/lib/types';
import { SaveIcon, TrashIcon } from '@rgossiaux/svelte-heroicons/outline'; import { SaveIcon, TrashIcon } from '@rgossiaux/svelte-heroicons/outline';
import { toast } from '../toast/toast'; import { clearCollection, saveCollection } from '$root/lib/util/manipulateCollection';
function saveCollection() {
console.log('Saving collection');
console.log('collectionStore', $collectionStore);
if (!browser) return;
localStorage.collection = JSON.stringify($collectionStore);
toast.send('Saved collection', { duration: 3000, type: ToastType.INFO });
}
function clearCollection() {
if (!browser) return;
localStorage.collection = [];
toast.send('Cleared collection', { duration: 3000, type: ToastType.INFO });
}
</script> </script>
<div> <div>

View file

@ -21,6 +21,7 @@ export type ToastData = {
export type SavedGameType = { export type SavedGameType = {
id: string; id: string;
name: string; name: string;
thumb_url: string;
} }
export type GameType = { export type GameType = {

View file

@ -1,11 +1,13 @@
import { collectionStore } from '$lib/stores/collectionStore'; import { collectionStore } from '$lib/stores/collectionStore';
import { toast } from '$lib/components/toast/toast'; import { toast } from '$lib/components/toast/toast';
import { ToastType, type GameType, type SavedGameType } from '$lib/types'; import { ToastType, type GameType, type SavedGameType } from '$lib/types';
import { browser } from '$app/env';
function convertToSavedGame(game: GameType): SavedGameType { function convertToSavedGame(game: GameType): SavedGameType {
return { return {
id: game.id, id: game.id,
name: game.name, name: game.name,
thumb_url: game.thumb_url,
}; };
} }
@ -18,3 +20,17 @@ export function removeFromCollection(game: GameType) {
collectionStore.remove(game.id); collectionStore.remove(game.id);
toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO }); toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO });
} }
export function saveCollection() {
console.log('Saving collection');
console.log('collectionStore', collectionStore);
if (!browser) return;
localStorage.collection = JSON.stringify(collectionStore);
toast.send('Saved collection', { duration: 3000, type: ToastType.INFO });
}
export function clearCollection() {
if (!browser) return;
localStorage.collection = [];
toast.send('Cleared collection', { duration: 3000, type: ToastType.INFO });
}

View file

@ -12,7 +12,7 @@
<div class="games"> <div class="games">
<div class="games-list"> <div class="games-list">
{#each $collectionStore as game} {#each $collectionStore as game}
<Game {game} /> <Game minimal {game} />
{/each} {/each}
</div> </div>
</div> </div>