aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/routes/bots/new.svelte
blob: c243fe1453b2c6dc8195797330a0787cc76e7d9a (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<script lang="ts">
  import { goto } from "$app/navigation";
  import { get_session_token } from "$lib/auth";
  import { currentUser } from "$lib/stores/current_user";
  import { onMount } from "svelte";
  let botName: string | undefined = undefined;
  let saveErrors: string[] = [];

  onMount(() => {
    // ensure user is logged in
    if (!$currentUser) {
      goto("/login");
    }
  });

  async function createBot() {
    saveErrors = [];

    // TODO: how can we handle this with the new ApiClient?
    let response = await fetch("/api/bots", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${get_session_token()}`,
      },
      body: JSON.stringify({
        name: botName,
      }),
    });

    let responseData = await response.json();
    if (response.ok) {
      let bot = responseData;
      goto(`/bots/${bot["name"]}`);
    } else {
      const error = responseData["error"];
      if (error["type"] === "validation_failed") {
        saveErrors = error["validation_errors"];
      } else if (error["type"] === "bot_name_taken") {
        saveErrors = ["Bot name is already taken"];
      } else {
        // unexpected error
        throw responseData;
      }
    }
  }
</script>

<div class="container">
  <div class="create-bot-form">
    <h4>Create new bot</h4>
    <input type="text" class="bot-name-input" placeholder="bot name" bind:value={botName} />
    {#if saveErrors.length > 0}
      <ul>
        {#each saveErrors as errorText}
          <li class="error-text">{errorText}</li>
        {/each}
      </ul>
    {/if}
    <button class="submit-button save-button" on:click={createBot}>Save</button>
  </div>
</div>

<style lang="scss">
  .container {
    width: 400px;
    max-width: 80%;
    margin: 50px auto;
  }

  .create-bot-form h4 {
    margin-bottom: 0.3em;
  }

  .error-text {
    color: red;
  }

  .submit-button {
    padding: 6px 16px;
    border-radius: 4px;
    border: 0;
    font-size: 18pt;
    display: block;
    margin: 10px auto;
    background-color: lightgreen;
    cursor: pointer;
  }

  .bot-name-input {
    width: 100%;
    font-size: 16px;
    padding: 8px 16px;
    box-sizing: border-box;
    margin: 10px 0;
    border: 1px solid rgb(216, 219, 223);
    border-radius: 3px;
  }
</style>