boredgame/src/routes/+page.svelte

222 lines
5.9 KiB
Svelte
Raw Normal View History

2022-01-28 05:27:12 +00:00
<script lang="ts">
2022-09-29 22:22:01 +00:00
import { enhance, applyAction } from '$app/forms';
import type { ActionData, PageData } from './$types';
2022-09-29 22:22:01 +00:00
import { ToastType, type GameType, type SavedGameType } from '$root/lib/types';
2022-10-05 19:53:43 +00:00
import { toast } from '$root/lib/components/toast/toast';
2022-09-29 22:22:01 +00:00
import { gameStore } from '$lib/stores/gameSearchStore';
import { boredState } from '$root/lib/stores/boredState';
import RemoveCollectionDialog from '$root/lib/components/dialog/RemoveCollectionDialog.svelte';
import Game from '$lib/components/game/index.svelte';
import TextSearch from '$lib/components/search/textSearch/index.svelte';
import RandomSearch from '$lib/components/search/random/index.svelte';
import Random from '$lib/components/random/index.svelte';
import Pagination from '$lib/components/pagination/index.svelte';
import RemoveWishlistDialog from '$root/lib/components/dialog/RemoveWishlistDialog.svelte';
import SkeletonPlaceholder from '$root/lib/components/SkeletonPlaceholder.svelte';
2022-09-29 22:22:01 +00:00
export let data: PageData;
export let form: ActionData;
2022-10-26 02:19:30 +00:00
console.log('form routesss', form);
2022-09-29 22:22:01 +00:00
console.log('Formed data:', JSON.stringify(data));
let pageSize: number;
let currentPage: number;
let submitting = $boredState?.loading;
console.log({ submitting });
2022-09-29 22:22:01 +00:00
$: totalItems = 0;
console.log('totalItems', totalItems);
// async function handleItemsPerPageChange(event) {
// const perPage = event?.detail;
// if ($gameStore.length )
// }
async function handleNextPageEvent(event: CustomEvent) {
console.log('Next page called', event);
boredState.update((n) => ({ ...n, loading: true }));
const form = event.target as HTMLFormElement;
console.log('form', form);
const response = await fetch('/api/game', {
method: 'POST',
headers: { accept: 'application/json' },
body: new FormData(form)
});
const responseData = await response.json();
boredState.update((n) => ({ ...n, loading: false }));
gameStore.removeAll();
gameStore.addAll(responseData?.games);
const skip = $boredState?.search?.skip;
const pageSize = $boredState?.search?.pageSize;
const currentPage = $boredState?.search?.currentPage;
const totalCount = responseData?.totalCount;
boredState.update((n) => ({
...n,
search: { totalCount, skip, pageSize, currentPage }
}));
}
let gameToRemove: GameType | SavedGameType;
interface RemoveGameEvent extends Event {
detail: GameType | SavedGameType;
}
function handleRemoveCollection(event: RemoveGameEvent) {
console.log('Remove collection event handler');
2022-09-29 22:22:01 +00:00
console.log('event', event);
gameToRemove = event?.detail;
boredState.update((n) => ({
...n,
dialog: { isOpen: true, content: RemoveCollectionDialog, additionalData: gameToRemove }
}));
}
function handleRemoveWishlist(event: RemoveGameEvent) {
console.log('Remove wishlist event handler');
console.log('event', event);
gameToRemove = event?.detail;
boredState.update((n) => ({
...n,
dialog: { isOpen: true, content: RemoveWishlistDialog, additionalData: gameToRemove }
}));
}
2022-01-28 05:27:12 +00:00
</script>
<svelte:head>
2022-09-29 22:22:01 +00:00
<title>Bored Game | Home</title>
2022-01-28 05:27:12 +00:00
</svelte:head>
<h1>Search Boardgames!</h1>
2022-09-01 22:31:19 +00:00
<p style="margin: 1rem 0;">
Input your requirements to search for board games that match your criteria.
2022-09-01 22:31:19 +00:00
</p>
<div class="game-search">
2022-09-29 22:22:01 +00:00
<form
2022-10-05 19:53:43 +00:00
action="/search"
2022-09-29 22:22:01 +00:00
method="post"
use:enhance={() => {
gameStore.removeAll();
2022-09-29 22:22:01 +00:00
boredState.update((n) => ({ ...n, loading: true }));
return async ({ result }) => {
boredState.update((n) => ({ ...n, loading: false }));
console.log('result main page search', result);
2022-09-29 22:22:01 +00:00
// `result` is an `ActionResult` object
if (result.type === 'success') {
console.log('In success');
const resultGames = result?.data?.games;
if (resultGames?.length <= 0) {
toast.send('No results found 😿', {
duration: 3000,
type: ToastType.ERROR,
dismissible: true
});
}
gameStore.addAll(resultGames);
2022-09-29 22:22:01 +00:00
totalItems = result?.data?.totalCount;
console.log(`Frontend result: ${JSON.stringify(result)}`);
await applyAction(result);
} else {
console.log('Invalid');
2022-10-26 02:19:30 +00:00
await applyAction(result);
2022-09-29 22:22:01 +00:00
}
};
}}
>
2022-10-26 02:19:30 +00:00
<TextSearch showButton advancedSearch {form} />
2022-09-29 22:22:01 +00:00
</form>
<section>
<p>Or pick a random game!</p>
<div class="random-buttons">
<RandomSearch />
<Random />
</div>
</section>
</div>
2022-11-03 05:44:00 +00:00
<!-- <SkeletonPlaceholder style="height: 12rem; width: 12rem; border-radius: 10px;" /> -->
{#if $gameStore?.length > 0}
2022-09-29 22:22:01 +00:00
<div class="games">
<h1>Games Found:</h1>
<div class="games-list">
{#each $gameStore as game (game.id)}
<Game
on:handleRemoveWishlist={handleRemoveWishlist}
on:handleRemoveCollection={handleRemoveCollection}
{game}
/>
2022-09-29 22:22:01 +00:00
{/each}
</div>
</div>
{:else if $boredState.loading}
<div class="games">
<h1>Games Found:</h1>
<div class="games-list">
{#each [1, 2, 3, 4] as game, i}
<SkeletonPlaceholder
style="width: 100%; height: 500px; border-radius: var(--borderRadius);"
/>
{/each}
</div>
</div>
{/if}
<!-- <Pagination
2022-09-29 22:22:01 +00:00
{pageSize}
{currentPage}
{totalItems}
forwardText="Next"
backwardText="Prev"
pageSizes={[10, 25, 50, 100]}
on:nextPageEvent={handleNextPageEvent}
on:previousPageEvent={(event) => console.log('Prev page called', event)}
on:perPageEvent={(event) => console.log('Per page called', event)}
/> -->
<style lang="scss">
2022-09-29 22:22:01 +00:00
.game-search {
display: grid;
gap: 2rem;
section {
display: grid;
gap: 1rem;
}
}
.games {
margin: 2rem 0rem;
h1 {
margin-bottom: 2rem;
}
}
.games-list {
display: grid;
--listColumns: 4;
grid-template-columns: repeat(var(--listColumns), minmax(200px, 1fr));
2022-09-29 22:22:01 +00:00
gap: 2rem;
@media (max-width: 1200px) {
--listColumns: 3;
}
2022-09-29 22:22:01 +00:00
@media (max-width: 800px) {
--listColumns: 2;
2022-09-29 22:22:01 +00:00
}
@media (max-width: 650px) {
--listColumns: 1;
2022-09-29 22:22:01 +00:00
}
}
.random-buttons {
display: flex;
place-content: space-between;
place-items: center;
@media (max-width: 650px) {
display: grid;
grid-template-columns: 1fr 1fr;
}
}
2022-01-28 05:27:12 +00:00
</style>