mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
Cleanup random API and create named action for a random search.
This commit is contained in:
parent
3f9759aef1
commit
391fba7955
5 changed files with 113 additions and 212 deletions
|
|
@ -5,30 +5,12 @@
|
|||
import { ToastType } from '$root/lib/types';
|
||||
import { toast } from '../../toast/toast';
|
||||
|
||||
// async function handleSubmit(event: SubmitEvent) {
|
||||
// // submitting = true;
|
||||
// boredState.update((n) => ({ ...n, loading: true }));
|
||||
// const form = event.target as HTMLFormElement;
|
||||
// console.log('form', form);
|
||||
// const response = await fetch('/api/games', {
|
||||
// method: 'POST',
|
||||
// headers: { accept: 'application/json' },
|
||||
// body: new FormData(form)
|
||||
// });
|
||||
// const responseData = await response.json();
|
||||
// // submitting = false;
|
||||
// boredState.update((n) => ({ ...n, loading: false }));
|
||||
// gameStore.removeAll();
|
||||
// gameStore.addAll(responseData?.games);
|
||||
// // games = responseData?.games;
|
||||
// }
|
||||
|
||||
let submitting = $boredState?.loading;
|
||||
let checked = true;
|
||||
</script>
|
||||
|
||||
<form
|
||||
action="/search"
|
||||
action="/search?/random"
|
||||
method="POST"
|
||||
use:enhance={() => {
|
||||
gameStore.removeAll();
|
||||
|
|
@ -58,7 +40,7 @@
|
|||
}}
|
||||
>
|
||||
<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>
|
||||
</fieldset>
|
||||
</form>
|
||||
|
|
|
|||
208
src/lib/types.ts
208
src/lib/types.ts
|
|
@ -1,125 +1,125 @@
|
|||
import type { SvelteComponent } from "svelte";
|
||||
import type { SvelteComponent } from 'svelte';
|
||||
|
||||
export type Dialog = {
|
||||
isOpen: boolean;
|
||||
content?: typeof SvelteComponent;
|
||||
additionalData?: SavedGameType | GameType;
|
||||
}
|
||||
isOpen: boolean;
|
||||
content?: typeof SvelteComponent;
|
||||
additionalData?: SavedGameType | GameType;
|
||||
};
|
||||
|
||||
export type Search = {
|
||||
name: string;
|
||||
minAge: string;
|
||||
minPlayers: string;
|
||||
maxPlayers: string;
|
||||
exactMinAge: string;
|
||||
exactMinPlayers: string;
|
||||
exactMaxPlayers: string;
|
||||
skip: number;
|
||||
currentPage: number;
|
||||
limit: number;
|
||||
}
|
||||
name: string;
|
||||
minAge: string;
|
||||
minPlayers: string;
|
||||
maxPlayers: string;
|
||||
exactMinAge: string;
|
||||
exactMinPlayers: string;
|
||||
exactMaxPlayers: string;
|
||||
skip: number;
|
||||
currentPage: number;
|
||||
limit: number;
|
||||
};
|
||||
|
||||
export type BoredStore = {
|
||||
loading: boolean;
|
||||
dialog: Dialog;
|
||||
loading: boolean;
|
||||
dialog: Dialog;
|
||||
};
|
||||
|
||||
export enum ToastType {
|
||||
INFO = 'INFO',
|
||||
ERROR = 'ERROR',
|
||||
WARNING = 'WARNING'
|
||||
INFO = 'INFO',
|
||||
ERROR = 'ERROR',
|
||||
WARNING = 'WARNING'
|
||||
}
|
||||
|
||||
export type ToastData = {
|
||||
id: number;
|
||||
duration: number;
|
||||
dismissible: boolean;
|
||||
showButton: boolean;
|
||||
autoDismiss: boolean;
|
||||
type: ToastType;
|
||||
message: string;
|
||||
id: number;
|
||||
duration: number;
|
||||
dismissible: boolean;
|
||||
showButton: boolean;
|
||||
autoDismiss: boolean;
|
||||
type: ToastType;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export type SavedGameType = {
|
||||
id: string;
|
||||
name: string;
|
||||
thumb_url: string;
|
||||
players: string;
|
||||
playtime: string;
|
||||
}
|
||||
id: string;
|
||||
name: string;
|
||||
thumb_url: string;
|
||||
players: string;
|
||||
playtime: string;
|
||||
};
|
||||
|
||||
export type GameType = {
|
||||
id: string;
|
||||
handle: string;
|
||||
name: string;
|
||||
url: string;
|
||||
edit_url: string;
|
||||
thumb_url: string;
|
||||
image_url: string;
|
||||
price: number;
|
||||
price_ca: number;
|
||||
price_uk: number;
|
||||
price_au: number;
|
||||
msrp: number;
|
||||
year_published: number;
|
||||
min_players: number;
|
||||
max_players: number;
|
||||
min_playtime: number;
|
||||
max_playtime: number;
|
||||
min_age: number;
|
||||
description: string;
|
||||
description_preview: string;
|
||||
players: string;
|
||||
playtime: string;
|
||||
id: string;
|
||||
handle: string;
|
||||
name: string;
|
||||
url: string;
|
||||
edit_url: string;
|
||||
thumb_url: string;
|
||||
image_url: string;
|
||||
price: number;
|
||||
price_ca: number;
|
||||
price_uk: number;
|
||||
price_au: number;
|
||||
msrp: number;
|
||||
year_published: number;
|
||||
min_players: number;
|
||||
max_players: number;
|
||||
min_playtime: number;
|
||||
max_playtime: number;
|
||||
min_age: number;
|
||||
description: string;
|
||||
description_preview: string;
|
||||
players: string;
|
||||
playtime: string;
|
||||
};
|
||||
|
||||
export type SearchQuery = {
|
||||
client_id: string;
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
ids?: string[];
|
||||
list_id?: string;
|
||||
kickstarter?: boolean;
|
||||
random?: boolean;
|
||||
name?: string;
|
||||
exact?: boolean;
|
||||
fuzzy_match?: boolean;
|
||||
designer?: string;
|
||||
publisher?: string;
|
||||
artist?: string;
|
||||
mechanics?: string;
|
||||
categories?: string;
|
||||
order_by?: string;
|
||||
ascending?: boolean;
|
||||
min_players?: number;
|
||||
max_players?: number;
|
||||
min_playtime?: number;
|
||||
max_playtime?: number;
|
||||
min_age?: number;
|
||||
year_published?: number;
|
||||
gt_min_players?: number;
|
||||
gt_max_players?: number;
|
||||
gt_min_playtime?: number;
|
||||
gt_max_playtime?: number;
|
||||
gt_min_age?: number;
|
||||
gt_year_published?: number;
|
||||
gt_price?: bigint;
|
||||
gt_msrp?: bigint;
|
||||
gt_discount?: bigint;
|
||||
gt_reddit_count?: number;
|
||||
gt_reddit_week_count?: number;
|
||||
gt_reddit_day_count?: number;
|
||||
lt_min_players?: number;
|
||||
lt_max_players?: number;
|
||||
lt_min_playtime?: number;
|
||||
lt_max_playtime?: number;
|
||||
lt_min_age?: number;
|
||||
lt_year_published?: number;
|
||||
lt_price?: bigint;
|
||||
lt_msrp?: bigint;
|
||||
lt_discount?: bigint;
|
||||
lt_reddit_count?: number;
|
||||
lt_reddit_week_count?: number;
|
||||
lt_reddit_day_count?: number;
|
||||
fields?: string;
|
||||
client_id: string;
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
ids?: string[];
|
||||
list_id?: string;
|
||||
kickstarter?: boolean;
|
||||
random?: boolean;
|
||||
name?: string;
|
||||
exact?: boolean;
|
||||
fuzzy_match?: boolean;
|
||||
designer?: string;
|
||||
publisher?: string;
|
||||
artist?: string;
|
||||
mechanics?: string;
|
||||
categories?: string;
|
||||
order_by?: string;
|
||||
ascending?: boolean;
|
||||
min_players?: number;
|
||||
max_players?: number;
|
||||
min_playtime?: number;
|
||||
max_playtime?: number;
|
||||
min_age?: number;
|
||||
year_published?: number;
|
||||
gt_min_players?: number;
|
||||
gt_max_players?: number;
|
||||
gt_min_playtime?: number;
|
||||
gt_max_playtime?: number;
|
||||
gt_min_age?: number;
|
||||
gt_year_published?: number;
|
||||
gt_price?: bigint;
|
||||
gt_msrp?: bigint;
|
||||
gt_discount?: bigint;
|
||||
gt_reddit_count?: number;
|
||||
gt_reddit_week_count?: number;
|
||||
gt_reddit_day_count?: number;
|
||||
lt_min_players?: number;
|
||||
lt_max_players?: number;
|
||||
lt_min_playtime?: number;
|
||||
lt_max_playtime?: number;
|
||||
lt_min_age?: number;
|
||||
lt_year_published?: number;
|
||||
lt_price?: bigint;
|
||||
lt_msrp?: bigint;
|
||||
lt_discount?: bigint;
|
||||
lt_reddit_count?: number;
|
||||
lt_reddit_week_count?: number;
|
||||
lt_reddit_day_count?: number;
|
||||
fields?: string;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -2,13 +2,7 @@ import { error } from '@sveltejs/kit';
|
|||
import type { PageServerLoad } from './$types';
|
||||
import { boardGameApi } from '../../api';
|
||||
|
||||
type GamePageParams = {
|
||||
params: {
|
||||
id: string;
|
||||
};
|
||||
};
|
||||
|
||||
export const load: PageServerLoad = async ({ params, setHeaders }: GamePageParams) => {
|
||||
export const load: PageServerLoad = async ({ params, setHeaders }) => {
|
||||
const queryParams = {
|
||||
ids: `${params?.id}`
|
||||
};
|
||||
|
|
|
|||
|
|
@ -225,6 +225,7 @@
|
|||
place-items: center;
|
||||
gap: 1.5rem;
|
||||
margin: 1rem;
|
||||
line-height: 1.5em;
|
||||
}
|
||||
|
||||
.overflow-description {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import type { Actions, PageServerLoad, RequestEvent } from '../$types';
|
||||
import { BOARD_GAME_ATLAS_CLIENT_ID } from '$env/static/private';
|
||||
import { error, fail } from '@sveltejs/kit';
|
||||
import type { GameType, Search, SearchQuery } from '$root/lib/types';
|
||||
import type { GameType, RandomSearch, Search, SearchQuery } from '$root/lib/types';
|
||||
import { mapAPIGameToBoredGame } from '$root/lib/util/gameMapper';
|
||||
import { search_schema } from '$root/lib/zodValidation';
|
||||
import { ZodError } from 'zod';
|
||||
|
|
@ -144,90 +144,20 @@ export const load: PageServerLoad = async ({ fetch, url }) => {
|
|||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
default: async ({ request }: RequestEvent): Promise<any> => {
|
||||
console.log('In search action specific');
|
||||
// Do things in here
|
||||
const formData = Object.fromEntries(await request.formData()) as Search;
|
||||
console.log('formData', formData);
|
||||
console.log('passed in limit:', formData?.limit);
|
||||
console.log('passed in skip:', formData?.skip);
|
||||
const limit = formData?.limit || 10;
|
||||
const skip = formData?.skip || 0;
|
||||
|
||||
random: async ({ request }: RequestEvent): Promise<any> => {
|
||||
const queryParams: SearchQuery = {
|
||||
order_by: 'rank',
|
||||
ascending: false,
|
||||
limit: +limit,
|
||||
skip: +skip,
|
||||
client_id: BOARD_GAME_ATLAS_CLIENT_ID,
|
||||
fuzzy_match: true,
|
||||
name: ''
|
||||
random: true
|
||||
};
|
||||
|
||||
// TODO: Check name length and not search if not advanced search
|
||||
|
||||
const random = formData?.random === 'on';
|
||||
|
||||
if (random) {
|
||||
console.log('Random');
|
||||
queryParams.random = random;
|
||||
} else {
|
||||
try {
|
||||
const {
|
||||
name,
|
||||
minAge,
|
||||
minPlayers,
|
||||
maxPlayers,
|
||||
exactMinAge,
|
||||
exactMinPlayers,
|
||||
exactMaxPlayers
|
||||
} = search_schema.parse(formData);
|
||||
|
||||
if (minAge) {
|
||||
if (exactMinAge) {
|
||||
queryParams.min_age = minAge;
|
||||
} else {
|
||||
queryParams.gt_min_age = minAge === 1 ? 0 : minAge - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (minPlayers) {
|
||||
if (exactMinPlayers) {
|
||||
queryParams.min_players = minPlayers;
|
||||
} else {
|
||||
queryParams.gt_min_players = minPlayers === 1 ? 0 : minPlayers - 1;
|
||||
}
|
||||
}
|
||||
if (maxPlayers) {
|
||||
if (exactMaxPlayers) {
|
||||
queryParams.max_players = maxPlayers;
|
||||
} else {
|
||||
queryParams.lt_max_players = maxPlayers + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (name) {
|
||||
queryParams.name = name;
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof ZodError) {
|
||||
console.log(error);
|
||||
|
||||
const { fieldErrors: errors } = error.flatten();
|
||||
return fail(400, { data: formData, errors });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const newQueryParams: Record<string, string> = {};
|
||||
for (const key in queryParams) {
|
||||
console.log('key', key);
|
||||
console.log('queryParams[key]', queryParams[key as keyof SearchQuery]);
|
||||
newQueryParams[key] = `${queryParams[key as keyof SearchQuery]}`;
|
||||
}
|
||||
|
||||
const urlQueryParams = new URLSearchParams(newQueryParams);
|
||||
console.log('urlQueryParams', urlQueryParams);
|
||||
|
||||
try {
|
||||
const url = `https://api.boardgameatlas.com/api/search${
|
||||
|
|
@ -260,20 +190,14 @@ export const actions: Actions = {
|
|||
// console.log('returning from search', games)
|
||||
|
||||
return {
|
||||
games,
|
||||
totalCount,
|
||||
limit: parseInt(limit),
|
||||
skip: parseInt(skip)
|
||||
games
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(`Error searching board games ${e}`);
|
||||
}
|
||||
return {
|
||||
games: [],
|
||||
totalCount: 0,
|
||||
limit,
|
||||
skip
|
||||
games: []
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue