Fixed random search in form action and the random component.

This commit is contained in:
Bradley Shellnut 2022-10-26 17:31:57 -04:00
parent a8ed73ebc0
commit bac9b7f888
3 changed files with 103 additions and 68 deletions

View file

@ -1,31 +1,60 @@
<script lang="ts"> <script lang="ts">
import { applyAction, enhance } from '$app/forms';
import { boredState } from '$lib/stores/boredState'; import { boredState } from '$lib/stores/boredState';
import { gameStore } from '$lib/stores/gameSearchStore'; import { gameStore } from '$lib/stores/gameSearchStore';
import { ToastType } from '$root/lib/types';
import { toast } from '../../toast/toast';
async function handleSubmit(event: SubmitEvent) { // async function handleSubmit(event: SubmitEvent) {
// submitting = true; // // submitting = true;
boredState.update((n) => ({ ...n, loading: true })); // boredState.update((n) => ({ ...n, loading: true }));
const form = event.target as HTMLFormElement; // const form = event.target as HTMLFormElement;
console.log('form', form); // console.log('form', form);
const response = await fetch('/api/games', { // const response = await fetch('/api/games', {
method: 'POST', // method: 'POST',
headers: { accept: 'application/json' }, // headers: { accept: 'application/json' },
body: new FormData(form) // body: new FormData(form)
}); // });
const responseData = await response.json(); // const responseData = await response.json();
// submitting = false; // // submitting = false;
boredState.update((n) => ({ ...n, loading: false })); // boredState.update((n) => ({ ...n, loading: false }));
gameStore.removeAll(); // gameStore.removeAll();
gameStore.addAll(responseData?.games); // gameStore.addAll(responseData?.games);
// games = responseData?.games; // // games = responseData?.games;
} // }
let submitting = $boredState?.loading; let submitting = $boredState?.loading;
let checked = true;
</script> </script>
<form on:submit|preventDefault={handleSubmit} method="post"> <form
action="/search"
method="POST"
use:enhance={() => {
boredState.update((n) => ({ ...n, loading: true }));
return async ({ result }) => {
boredState.update((n) => ({ ...n, loading: false }));
console.log('result main page search', result);
// `result` is an `ActionResult` object
if (result.type === 'success') {
console.log('In success');
gameStore.removeAll();
const resultGames = result?.data?.games;
if (resultGames?.length <= 0) {
toast.send('No results!', { duration: 3000, type: ToastType.INFO, dismissible: true });
}
gameStore.addAll(resultGames);
console.log(`Frontend result: ${JSON.stringify(result)}`);
await applyAction(result);
} else {
console.log('Invalid');
await applyAction(result);
}
};
}}
>
<fieldset aria-busy={submitting} disabled={submitting}> <fieldset aria-busy={submitting} disabled={submitting}>
<input type="checkbox" id="random" name="random" hidden checked /> <input type="checkbox" id="random" name="random" hidden {checked} />
<button class="btn" type="submit" disabled={submitting}>Random Game 🎲</button> <button class="btn" type="submit" disabled={submitting}>Random Game 🎲</button>
</fieldset> </fieldset>
</form> </form>

View file

@ -73,7 +73,7 @@
<h1>Search Boardgames!</h1> <h1>Search Boardgames!</h1>
<p style="margin: 1rem 0;"> <p style="margin: 1rem 0;">
Input your requirements to search for board game that match your criteria. Input your requirements to search for board games that match your criteria.
</p> </p>
<div class="game-search"> <div class="game-search">
<form <form

View file

@ -29,6 +29,11 @@ export const actions: Actions = {
name: '' name: ''
}; };
const random = form.get('random') && form.get('random') === 'on';
if (random) {
queryParams.random = random;
} else {
const minAge = form.get('minAge'); const minAge = form.get('minAge');
const minPlayers = form.get('minPlayers'); const minPlayers = form.get('minPlayers');
const maxPlayers = form.get('maxPlayers'); const maxPlayers = form.get('maxPlayers');
@ -68,6 +73,7 @@ export const actions: Actions = {
if (name) { if (name) {
queryParams.name = `${name}`; queryParams.name = `${name}`;
} }
}
const newQueryParams: Record<string, string> = {}; const newQueryParams: Record<string, string> = {};
for (const key in queryParams) { for (const key in queryParams) {