aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/matches/new.svelte
blob: ebc0e154527b099e7b6df77044c96daa20265603 (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 { get_session_token } from "$lib/auth";
  import { mount_component } from "svelte/internal";

  export async function load({ page }) {
    const token = get_session_token();
    const res = await fetch("/api/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 bot"),
    };
  }
</script>

<script lang="ts">
  import Select from "svelte-select";
  import { goto } from "$app/navigation";
  export let bots: object[];
  let items: object[];
  let players: object[] = [];
  let selected: object | null = null;

  $: items = bots.map((bot) => {
    return {
      value: bot["id"],
      label: bot["name"],
    };
  });

  function addPlayer() {
    if (selected === null) {
      return;
    }

    players.push(selected);
    players = players;
  }

  async function submitMatch() {
    const token = get_session_token();
    const res = await fetch("/api/matches", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${token}`,
      },
      body: JSON.stringify({
        players: players.map((player) => player["value"]),
      }),
    });

    if (res.ok) {
      // TODO
      goto("/matches");
    } else {
      alert(res.statusText);
    }
  }
</script>

Select players:
<Select {items} bind:value={selected} />
<button on:click={addPlayer}>add</button>
<h3>Selected Players</h3>
<ol>
  {#each players as player}
    <li>{player["label"]}</li>
  {/each}
</ol>
<button on:click={submitMatch}>Play</button>