add filtering and infinite scroll to BotMatches

This commit is contained in:
Ilion Beyst 2022-10-30 16:23:18 +01:00
parent f429adb4f8
commit 8d556bbff1
2 changed files with 100 additions and 26 deletions

View file

@ -1,19 +1,84 @@
<script lang="ts"> <script lang="ts">
import type { BotMatch } from "$lib/matches"; import { ApiClient } from "$lib/api_client";
import { apiMatchtoBotMatch, BotMatch } from "$lib/matches";
import { onDestroy, onMount } from "svelte";
import BotMatchCard from "./BotMatchCard.svelte"; import BotMatchCard from "./BotMatchCard.svelte";
export let botMatches: BotMatch[]; export let botName: string;
export let opponentName: string | undefined;
export let mapName: string | undefined;
function match_url(match: object) { let botMatches: BotMatch[] = [];
return `/matches/${match["id"]}`; let nextCursor = undefined;
let loading = false;
let hasMore = true;
onMount(async () => {
window.addEventListener("scroll", onScroll);
window.addEventListener("resize", onScroll);
});
onDestroy(() => {
window.removeEventListener("scroll", onScroll);
window.removeEventListener("resize", onScroll);
});
function onScroll(e: Event) {
// const element = e.target as HTMLElement;
const element = window.document.body;
if (hasMore && element.scrollHeight - window.scrollY - element.clientHeight <= 300) {
fetchNextPage();
}
} }
async function fetchNextPage() {
if (loading) {
return;
}
loading = true;
const params = { bot: botName, count: "50" };
if (opponentName) {
params["opponent"] = opponentName;
}
if (mapName) {
params["map"] = mapName;
}
if (nextCursor) {
params["before"] = nextCursor;
}
const apiClient = new ApiClient();
let { matches, has_next } = await apiClient.get("/api/matches", params);
if (has_next) {
nextCursor = matches[matches.length - 1]["timestamp"];
} else {
hasMore = false;
}
botMatches = botMatches.concat(matches.map((m) => apiMatchtoBotMatch(botName, m)));
loading = false;
}
async function resetList(..._params: any[]) {
botMatches = [];
nextCursor = undefined;
hasMore = true;
await fetchNextPage();
}
$: resetList(botName, opponentName, mapName);
</script> </script>
<div> <div>
{#each botMatches as botMatch} {#each botMatches as botMatch}
<BotMatchCard {botMatch} /> <BotMatchCard {botMatch} />
{/each} {/each}
{#if loading}
<div class="loading-container">Loading...</div>
{/if}
</div> </div>
<style lang="scss"> <style lang="scss">
.loading-container {
text-align: center;
padding: 16px;
}
</style> </style>

View file

@ -1,26 +1,21 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
import { ApiClient } from "$lib/api_client"; import { ApiClient } from "$lib/api_client";
import type { Match } from "$lib/api_types";
const PAGE_SIZE = "50";
export async function load({ params, fetch }) { export async function load({ params, fetch }) {
try { try {
const apiClient = new ApiClient(fetch); const apiClient = new ApiClient(fetch);
const botName = params["bot_name"]; const botName = params["bot_name"];
let { matches, has_next } = await apiClient.get("/api/matches", { bot: botName }); const [allBots, allMaps] = await Promise.all([
apiClient.get("/api/bots"),
// TODO: should this be done client-side? apiClient.get("/api/maps"),
// if (query["after"]) { ]);
// matches = matches.reverse();
// }
return { return {
props: { props: {
matches,
botName, botName,
hasNext: has_next, allBots,
allMaps,
}, },
}; };
} catch (error) { } catch (error) {
@ -33,20 +28,35 @@
</script> </script>
<script lang="ts"> <script lang="ts">
import LinkButton from "$lib/components/LinkButton.svelte";
import BotMatchList from "$lib/components/matches/BotMatchList.svelte"; import BotMatchList from "$lib/components/matches/BotMatchList.svelte";
import { apiMatchtoBotMatch } from "$lib/matches"; import Select from "svelte-select";
export let matches: Match[]; export let botName: string;
export let botName: string | null; export let allBots: object[];
// whether a next page exists in the current iteration direction (before/after) export let allMaps: object[];
// export let hasNext: boolean;
$: botMatches = matches.map((match) => apiMatchtoBotMatch(botName, match)); let opponentName: string | undefined;
let mapName: string | undefined;
</script> </script>
<div class="container"> <div class="container">
<BotMatchList {botMatches} /> <div class="selections">
<Select
items={allBots}
placeholder="Select opponent"
label="name"
itemId="name"
bind:justValue={opponentName}
/>
<Select
items={allMaps}
placeholder="Select map"
label="name"
itemId="name"
bind:justValue={mapName}
/>
</div>
<BotMatchList {botName} {opponentName} {mapName} />
</div> </div>
<style lang="scss"> <style lang="scss">
@ -56,9 +66,8 @@
width: 100%; width: 100%;
} }
.page-controls { .selections {
display: flex; display: flex;
justify-content: center; padding: 16px 4px;
margin: 24px 0;
} }
</style> </style>