diff options
Diffstat (limited to 'fe/src/lib/forms/PreferencesForm.svelte')
-rw-r--r-- | fe/src/lib/forms/PreferencesForm.svelte | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/fe/src/lib/forms/PreferencesForm.svelte b/fe/src/lib/forms/PreferencesForm.svelte new file mode 100644 index 0000000..79663d1 --- /dev/null +++ b/fe/src/lib/forms/PreferencesForm.svelte | |||
@@ -0,0 +1,119 @@ | |||
1 | <script lang="ts"> | ||
2 | import { user, preferences, token } from "../../stores/auth"; | ||
3 | import { createEventDispatcher, onDestroy, onMount } from "svelte"; | ||
4 | import type { User } from "../../types"; | ||
5 | import { apiURL } from "../../utils"; | ||
6 | |||
7 | export let open: boolean; | ||
8 | |||
9 | let sizes: Array<any>; | ||
10 | let selectedSize: number = 1; | ||
11 | let color: string = "#000000"; | ||
12 | |||
13 | const dispatch = createEventDispatcher(); | ||
14 | |||
15 | const unsubscribe = preferences.subscribe( | ||
16 | (value: any) => { | ||
17 | if (value) { | ||
18 | color = value.color; | ||
19 | selectedSize = value.size_id; | ||
20 | } | ||
21 | }, | ||
22 | ); | ||
23 | |||
24 | function closeDialog() { | ||
25 | dispatch("close"); | ||
26 | } | ||
27 | |||
28 | async function updateUserPreferences() { | ||
29 | const res = await fetch(apiURL("user/preferences"), { | ||
30 | method: "PATCH", | ||
31 | headers: { | ||
32 | Authorization: `Bearer ${$token}`, | ||
33 | }, | ||
34 | body: JSON.stringify($preferences), | ||
35 | }); | ||
36 | } | ||
37 | |||
38 | async function getUserPreferences() { | ||
39 | const res = await fetch(apiURL(`user/${($user as User)!.id}/preferences`), | ||
40 | { | ||
41 | method: "GET", | ||
42 | headers: { | ||
43 | Authorization: `Bearer ${$token}`, | ||
44 | }, | ||
45 | }, | ||
46 | ); | ||
47 | const updatePreferences = await res.json(); | ||
48 | preferences.set(updatePreferences); | ||
49 | } | ||
50 | |||
51 | async function onPreferencesSave(): Promise<void> { | ||
52 | preferences.update((value) => ({ | ||
53 | ...value!, | ||
54 | size_id: selectedSize, | ||
55 | color: color, | ||
56 | })); | ||
57 | |||
58 | await updateUserPreferences(); | ||
59 | await getUserPreferences(); | ||
60 | |||
61 | dispatch("close"); | ||
62 | } | ||
63 | |||
64 | onMount(() => { | ||
65 | fetch(apiURL("sizes"), { | ||
66 | method: "GET", | ||
67 | headers: { | ||
68 | Authorization: `Bearer ${$token}`, | ||
69 | }, | ||
70 | }) | ||
71 | .then((res) => res.json()) | ||
72 | .then((val) => (sizes = val)); | ||
73 | }); | ||
74 | |||
75 | onDestroy(() => { | ||
76 | unsubscribe(); | ||
77 | }); | ||
78 | </script> | ||
79 | |||
80 | <dialog {open} on:submit|preventDefault={onPreferencesSave}> | ||
81 | <h2>User Preferences</h2> | ||
82 | <form method="dialog"> | ||
83 | <div class="form input group"> | ||
84 | <label for="color">Color</label> | ||
85 | <input | ||
86 | id="color" | ||
87 | name="color" | ||
88 | type="color" | ||
89 | bind:value={color} | ||
90 | /> | ||
91 | </div> | ||
92 | <div class="form input group"> | ||
93 | <label for="size">Bottle Size</label> | ||
94 | <select | ||
95 | bind:value={selectedSize} | ||
96 | > | ||
97 | {#if sizes} | ||
98 | {#each sizes as size} | ||
99 | <option value={size.id}>{size.size} {size.unit}</option> | ||
100 | {/each} | ||
101 | {/if} | ||
102 | </select> | ||
103 | </div> | ||
104 | <button on:click={closeDialog}>Cancel</button> | ||
105 | <button type="submit">Save</button> | ||
106 | </form> | ||
107 | </dialog> | ||
108 | |||
109 | <style> | ||
110 | /* dialog { | ||
111 | background: white; | ||
112 | color: black; | ||
113 | } | ||
114 | |||
115 | input[type="color"] { | ||
116 | width: 4em; | ||
117 | height: 4em; | ||
118 | } */ | ||
119 | </style> | ||