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
|
<script lang="ts" context="module">
import { get_session_token } from "$lib/auth";
/** @type {import('@sveltejs/kit').Load} */
export async function load({ params, fetch, session, stuff }) {
const token = get_session_token();
const res = await fetch("/api/bots/my_bots", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
});
if (res.ok) {
return {
props: {
bots: await res.json(),
},
};
}
return {
status: res.status,
error: new Error("Could not load bots"),
};
}
</script>
<script lang="ts">
import { goto } from "$app/navigation";
export let bots: object[];
let name: string | undefined;
async function createBot() {
const token = get_session_token();
const res = await fetch("/api/bots", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
name: name,
}),
});
if (res.ok) {
let bot = await res.json();
goto(`/bots/${bot["id"]}`);
} else {
new Error("creation failed");
}
}
</script>
<form on:submit|preventDefault={createBot}>
<label for="name">Name</label>
<input name="name" bind:value={name} />
<button type="submit">Create</button>
</form>
<ul>
{#each bots as bot}
<li>
<a target="_blank" href={`bots/${bot["id"]}`}>
{bot["name"]}
</a>
</li>
{/each}
</ul>
|