aboutsummaryrefslogtreecommitdiff
path: root/fe/src/lib/forms/PreferencesForm.svelte
blob: 79663d1124d7b52825472694096458779cd9c548 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<script lang="ts">
  import { user, preferences, token } from "../../stores/auth";
  import { createEventDispatcher, onDestroy, onMount } from "svelte";
  import type { User } from "../../types";
  import { apiURL } from "../../utils";

  export let open: boolean;

  let sizes: Array<any>;
  let selectedSize: number = 1;
  let color: string = "#000000";

  const dispatch = createEventDispatcher();

  const unsubscribe = preferences.subscribe(
    (value: any) => {
      if (value) {
        color = value.color;
        selectedSize = value.size_id;
      }
    },
  );

  function closeDialog() {
    dispatch("close");
  }

  async function updateUserPreferences() {
    const res = await fetch(apiURL("user/preferences"), {
      method: "PATCH",
      headers: {
        Authorization: `Bearer ${$token}`,
      },
      body: JSON.stringify($preferences),
    });
  }

  async function getUserPreferences() {
    const res = await fetch(apiURL(`user/${($user as User)!.id}/preferences`),
      {
        method: "GET",
        headers: {
          Authorization: `Bearer ${$token}`,
        },
      },
    );
    const updatePreferences = await res.json();
    preferences.set(updatePreferences);
  }

  async function onPreferencesSave(): Promise<void> {
    preferences.update((value) => ({
      ...value!,
      size_id: selectedSize,
      color: color,
    }));

    await updateUserPreferences();
    await getUserPreferences();

    dispatch("close");
  }

  onMount(() => {
    fetch(apiURL("sizes"), {
      method: "GET",
      headers: {
        Authorization: `Bearer ${$token}`,
      },
    })
      .then((res) => res.json())
      .then((val) => (sizes = val));
  });

  onDestroy(() => {
    unsubscribe();
  });
</script>

<dialog {open} on:submit|preventDefault={onPreferencesSave}>
  <h2>User Preferences</h2>
  <form method="dialog">
    <div class="form input group">
      <label for="color">Color</label>
      <input
        id="color"
        name="color"
        type="color"
        bind:value={color}
      />
    </div>
    <div class="form input group">
      <label for="size">Bottle Size</label>
      <select
        bind:value={selectedSize}
      >
        {#if sizes}
          {#each sizes as size}
            <option value={size.id}>{size.size} {size.unit}</option>
          {/each}
        {/if}
      </select>
    </div>
    <button on:click={closeDialog}>Cancel</button>
    <button type="submit">Save</button>
  </form>
</dialog>

<style>
  /* dialog {
    background: white;
    color: black;
  }

  input[type="color"] {
    width: 4em;
    height: 4em;
  } */
</style>