blob: c6eca500b5be4dff8deff04b800f60a69d259b38 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
<script lang="ts" context="module">
import { ApiClient } from "$lib/api_client";
const PAGE_SIZE = "50";
export async function load({ fetch }) {
try {
const apiClient = new ApiClient(fetch);
const matches = await apiClient.get("/api/matches", {
count: PAGE_SIZE,
});
return {
props: {
matches,
},
};
} catch (error) {
return {
status: error.status,
error: new Error("failed to load matches"),
};
}
}
</script>
<script lang="ts">
import MatchList from "$lib/components/matches/MatchList.svelte";
export let matches: object[];
let loading = false;
async function loadNewer() {
if (matches.length == 0) {
return;
}
const firstTimestamp = matches[0]["timestamp"];
const apiClient = new ApiClient();
const reversedNewPage = await apiClient.get("/api/matches", {
count: PAGE_SIZE,
after: firstTimestamp,
});
if (reversedNewPage.length > 0) {
matches = reversedNewPage.reverse();
}
}
async function loadOlder() {
if (matches.length == 0) {
return;
}
const lastTimestamp = matches[matches.length - 1]["timestamp"];
const apiClient = new ApiClient();
const newPage = await apiClient.get("/api/matches", {
count: PAGE_SIZE,
before: lastTimestamp,
});
if (newPage.length > 0) {
matches = newPage;
}
}
</script>
<div class="container">
<MatchList {matches} />
<div class="page-controls">
<button on:click={loadNewer}>newer</button>
<button on:click={loadOlder}>older</button>
</div>
</div>
<style lang="scss">
.container {
width: 800px;
margin: 0 auto;
}
.page-controls {
display: flex;
justify-content: space-between;
margin: 12px;
}
</style>
|