2022-10-13 04:17:30 +00:00
|
|
|
<script lang="ts">
|
2022-10-14 04:01:01 +00:00
|
|
|
import { applyAction, enhance } from '$app/forms';
|
2022-11-04 01:47:29 +00:00
|
|
|
import { ToastType, type GameType, type SavedGameType } from '$root/lib/types';
|
2022-10-13 04:17:30 +00:00
|
|
|
import type { ActionData, PageData } from './$types';
|
|
|
|
|
import Game from '$lib/components/game/index.svelte';
|
|
|
|
|
import { gameStore } from '$lib/stores/gameSearchStore';
|
2022-10-14 04:01:01 +00:00
|
|
|
import TextSearch from '$lib/components/search/textSearch/index.svelte';
|
2022-10-13 04:17:30 +00:00
|
|
|
import RemoveCollectionDialog from '$root/lib/components/dialog/RemoveCollectionDialog.svelte';
|
|
|
|
|
import { boredState } from '$root/lib/stores/boredState';
|
2022-10-14 04:01:01 +00:00
|
|
|
import { toast } from '$root/lib/components/toast/toast';
|
2022-11-09 02:07:05 +00:00
|
|
|
import Pagination from '$lib/components/pagination/index.svelte';
|
2022-10-31 20:09:21 +00:00
|
|
|
import RemoveWishlistDialog from '$root/lib/components/dialog/RemoveWishlistDialog.svelte';
|
2022-10-13 04:17:30 +00:00
|
|
|
|
|
|
|
|
export let data: PageData;
|
2022-11-01 00:31:36 +00:00
|
|
|
console.log('search page data', data);
|
2022-10-13 04:17:30 +00:00
|
|
|
export let form: ActionData;
|
2022-11-01 00:31:36 +00:00
|
|
|
console.log('search page form', form);
|
2022-11-09 02:07:05 +00:00
|
|
|
|
2022-10-13 04:17:30 +00:00
|
|
|
let gameToRemove: GameType | SavedGameType;
|
2022-11-09 02:07:05 +00:00
|
|
|
let pageSize = 10;
|
|
|
|
|
console.log('Form data page', +form?.data?.page);
|
|
|
|
|
let page = +form?.data?.page || 1;
|
|
|
|
|
$: skip = (page - 1) * pageSize;
|
|
|
|
|
console.log({ skip });
|
|
|
|
|
let totalItems = form?.totalCount || data?.totalCount || 0;
|
|
|
|
|
console.log({ pageSize });
|
|
|
|
|
console.log({ page });
|
|
|
|
|
console.log({ totalItems });
|
|
|
|
|
let submitting = $boredState?.loading;
|
|
|
|
|
let numberOfGameSkeleton = 1;
|
|
|
|
|
console.log('Search page total count: ', totalItems);
|
2022-10-13 04:17:30 +00:00
|
|
|
|
|
|
|
|
$: if (data?.games) {
|
|
|
|
|
gameStore.removeAll();
|
|
|
|
|
gameStore.addAll(data?.games);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 03:54:44 +00:00
|
|
|
$: if (form?.games) {
|
|
|
|
|
gameStore.removeAll();
|
|
|
|
|
gameStore.addAll(form?.games);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 04:17:30 +00:00
|
|
|
interface RemoveGameEvent extends Event {
|
|
|
|
|
detail: GameType | SavedGameType;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-31 20:09:21 +00:00
|
|
|
function handleRemoveCollection(event: RemoveGameEvent) {
|
|
|
|
|
console.log('Remove collection event handler');
|
2022-10-13 04:17:30 +00:00
|
|
|
console.log('event', event);
|
|
|
|
|
gameToRemove = event?.detail;
|
|
|
|
|
boredState.update((n) => ({
|
|
|
|
|
...n,
|
|
|
|
|
dialog: { isOpen: true, content: RemoveCollectionDialog, additionalData: gameToRemove }
|
|
|
|
|
}));
|
|
|
|
|
}
|
2022-10-31 20:09:21 +00:00
|
|
|
|
|
|
|
|
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-11-09 02:07:05 +00:00
|
|
|
|
|
|
|
|
function handleNextPageEvent(event: CustomEvent) {
|
|
|
|
|
console.log('Next page called', event.detail);
|
|
|
|
|
console.log('Current page: ', page);
|
|
|
|
|
if (+event?.detail?.page === page + 1) {
|
|
|
|
|
console.log('Page equals plus one');
|
|
|
|
|
page += 1;
|
|
|
|
|
}
|
|
|
|
|
console.log('New page value: ', page);
|
|
|
|
|
console.log('New skip value: ', skip);
|
|
|
|
|
document.getElementById('skip')?.setAttribute('value', `${page * pageSize}`);
|
|
|
|
|
console.log('New skip value DOM: ', document.getElementById('skip')?.getAttribute('value'));
|
|
|
|
|
document.getElementById('search-submit')?.click();
|
|
|
|
|
}
|
2022-10-13 04:17:30 +00:00
|
|
|
</script>
|
|
|
|
|
|
2022-10-14 04:01:01 +00:00
|
|
|
<div class="game-search">
|
2022-11-09 02:07:05 +00:00
|
|
|
{skip}
|
2022-10-14 04:01:01 +00:00
|
|
|
<form
|
2022-11-09 02:07:05 +00:00
|
|
|
id="search-form"
|
2022-10-30 04:03:50 +00:00
|
|
|
action="/search"
|
2022-10-14 04:01:01 +00:00
|
|
|
method="post"
|
2022-11-09 02:07:05 +00:00
|
|
|
use:enhance={({ data }) => {
|
|
|
|
|
gameStore.removeAll();
|
|
|
|
|
// data.append('limit', pageSize.toString());
|
|
|
|
|
// data.append('skip', Math.floor(page * pageSize).toString());
|
2022-10-14 04:01:01 +00:00
|
|
|
boredState.update((n) => ({ ...n, loading: true }));
|
|
|
|
|
return async ({ result }) => {
|
|
|
|
|
boredState.update((n) => ({ ...n, loading: false }));
|
|
|
|
|
console.log(result);
|
|
|
|
|
// `result` is an `ActionResult` object
|
|
|
|
|
if (result.type === 'error') {
|
|
|
|
|
toast.send('Error!', { duration: 3000, type: ToastType.ERROR, dismissible: true });
|
|
|
|
|
await applyAction(result);
|
2022-11-01 00:31:36 +00:00
|
|
|
} else if (result.type === 'success') {
|
2022-10-14 04:01:01 +00:00
|
|
|
gameStore.removeAll();
|
|
|
|
|
gameStore.addAll(result?.data?.games);
|
2022-10-31 20:09:21 +00:00
|
|
|
// totalItems = result?.data?.totalCount;
|
2022-11-09 02:07:05 +00:00
|
|
|
console.log(`Frontend result search enhance: ${JSON.stringify(result)}`);
|
|
|
|
|
totalItems = result?.data?.totalCount;
|
|
|
|
|
// skip = result?.data?.skip || 0;
|
|
|
|
|
// page = skip / pageSize || 0;
|
|
|
|
|
// console.log('enhance', page, skip, totalItems);
|
2022-10-14 04:01:01 +00:00
|
|
|
toast.send('Sucess!', { duration: 3000, type: ToastType.INFO, dismissible: true });
|
|
|
|
|
await applyAction(result);
|
2022-11-01 00:31:36 +00:00
|
|
|
} else {
|
|
|
|
|
await applyAction(result);
|
2022-10-14 04:01:01 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}}
|
|
|
|
|
>
|
2022-11-09 02:07:05 +00:00
|
|
|
<input id="skip" type="hidden" name="skip" value={skip} />
|
|
|
|
|
<input id="limit" type="hidden" name="limit" value={pageSize} />
|
2022-11-01 00:31:36 +00:00
|
|
|
<TextSearch showButton advancedSearch {form} />
|
2022-10-14 04:01:01 +00:00
|
|
|
</form>
|
|
|
|
|
</div>
|
2022-10-13 04:17:30 +00:00
|
|
|
|
|
|
|
|
{#if $gameStore?.length > 0}
|
|
|
|
|
<div class="games">
|
|
|
|
|
<h1>Games Found:</h1>
|
|
|
|
|
<div class="games-list">
|
|
|
|
|
{#each $gameStore as game (game.id)}
|
2022-10-31 20:09:21 +00:00
|
|
|
<Game
|
|
|
|
|
on:handleRemoveWishlist={handleRemoveWishlist}
|
|
|
|
|
on:handleRemoveCollection={handleRemoveCollection}
|
|
|
|
|
{game}
|
|
|
|
|
/>
|
2022-10-13 04:17:30 +00:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
2022-11-09 02:07:05 +00:00
|
|
|
<Pagination
|
|
|
|
|
{pageSize}
|
|
|
|
|
{page}
|
|
|
|
|
{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)}
|
|
|
|
|
/>
|
2022-10-13 04:17:30 +00:00
|
|
|
</div>
|
2022-11-01 00:31:36 +00:00
|
|
|
{:else if form && form?.status && form.status !== 200}
|
2022-10-30 04:03:50 +00:00
|
|
|
<h1>There was an error searching for games!</h1>
|
|
|
|
|
<h2>Please try again later.</h2>
|
2022-10-13 04:17:30 +00:00
|
|
|
{/if}
|
2022-10-14 03:54:44 +00:00
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
.games {
|
|
|
|
|
margin: 2rem 0rem;
|
|
|
|
|
|
|
|
|
|
h1 {
|
|
|
|
|
margin-bottom: 2rem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.games-list {
|
|
|
|
|
display: grid;
|
|
|
|
|
grid-template-columns: repeat(3, minmax(200px, 1fr));
|
|
|
|
|
gap: 2rem;
|
|
|
|
|
|
|
|
|
|
@media (max-width: 800px) {
|
|
|
|
|
grid-template-columns: 1fr 1fr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@media (max-width: 650px) {
|
|
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|