diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-30 23:40:37 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-30 23:40:37 +0100 |
commit | f5e8b4093a1527967423a3af70f2b95d4b05008f (patch) | |
tree | fd000cd7689e54b9efc73f2913ec4d1817c098b0 /web/pw-server | |
parent | c6ca7cf2d1238c05f75a53934ea2f6c91efc3646 (diff) | |
download | planetwars.dev-f5e8b4093a1527967423a3af70f2b95d4b05008f.tar.xz planetwars.dev-f5e8b4093a1527967423a3af70f2b95d4b05008f.zip |
prototype bots pages
Diffstat (limited to 'web/pw-server')
-rw-r--r-- | web/pw-server/src/routes/bots/[bot_id].svelte | 34 | ||||
-rw-r--r-- | web/pw-server/src/routes/bots/index.svelte | 71 |
2 files changed, 105 insertions, 0 deletions
diff --git a/web/pw-server/src/routes/bots/[bot_id].svelte b/web/pw-server/src/routes/bots/[bot_id].svelte new file mode 100644 index 0000000..90fd78d --- /dev/null +++ b/web/pw-server/src/routes/bots/[bot_id].svelte @@ -0,0 +1,34 @@ +<script lang="ts" context="module"> + import { get_session_token } from "$lib/auth"; + + export async function load({ page }) { + const token = get_session_token(); + const res = await fetch(`/api/bots/${page.params["bot_id"]}`, { + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + }); + + if (res.ok) { + return { + props: { + bot: await res.json(), + }, + }; + } + + return { + status: res.status, + error: new Error("Could not load bot"), + }; + } +</script> + +<script lang="ts"> + export let bot: object; +</script> + +<div> + {bot["name"]} +</div> diff --git a/web/pw-server/src/routes/bots/index.svelte b/web/pw-server/src/routes/bots/index.svelte new file mode 100644 index 0000000..775652b --- /dev/null +++ b/web/pw-server/src/routes/bots/index.svelte @@ -0,0 +1,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> |