From 9cae9c1d2a0b4f7fa72f3075541b9ffafe1a7275 Mon Sep 17 00:00:00 2001
From: Zach Berwaldt <zberwaldt@tutamail.com>
Date: Fri, 15 Mar 2024 18:49:43 -0400
Subject: Add routes for preference, clean up and add types

---
 fe/src/lib/PreferencesForm.svelte | 145 ++++++++++++++++++++++++++++----------
 1 file changed, 106 insertions(+), 39 deletions(-)

(limited to 'fe/src/lib/PreferencesForm.svelte')

diff --git a/fe/src/lib/PreferencesForm.svelte b/fe/src/lib/PreferencesForm.svelte
index 95e04c1..875393c 100644
--- a/fe/src/lib/PreferencesForm.svelte
+++ b/fe/src/lib/PreferencesForm.svelte
@@ -1,51 +1,118 @@
 <script lang="ts">
-    import { preferences } from '../stores/auth';
-    import type { Size, Preference } from '../types';
-    import { createEventDispatcher } from 'svelte';
-   export let open: boolean; 
-
-    let preference: Preference = {
-        color: "#00FF00",
-        size: {
-            size: 8,
-            unit: 'oz'
-        }
-    }
-
-    const dispatch = createEventDispatcher();
-    
-    preferences.subscribe((value) => {
-        preference = value;
+  import { user, preferences, token } from "../stores/auth";
+  import { createEventDispatcher, onDestroy, onMount } from "svelte";
+  import type { User } from "../types";
+
+  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) => {
+        console.log('update value: ', value);
+      color = value.color;
+      selectedSize = value.size_id;
+    },
+  );
+
+  function closeDialog() {
+    dispatch("close");
+  }
+
+  async function updateUserPreferences() {
+    const res = await fetch("http://localhost:8080/api/v1/user/preferences", {
+      method: "PATCH",
+      headers: {
+        Authorization: `Bearer ${$token}`,
+      },
+      body: JSON.stringify($preferences),
     });
+  }
 
-    function onPreferencesSave(): void {
-        preferences.set(preference);
-        dispatch('close')
-    }
+  async function getUserPreferences() {
+    const res = await fetch(
+      `http://localhost:8080/api/v1/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("http://localhost:8080/api/v1/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={preference.color}/>
-        </div>
-        <div class="form input group">
-            <label for="size">Bottle Size</label>
-            <input id="size" name="size" type="number" min="8" max="48" step="8" bind:value={preference.size.size}/>
-        </div>
-        <button type="submit">Save</button>
-    </form>
+  <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 {
+  dialog {
     background: white;
     color: black;
-}
+  }
 
-input[type="color"] {
-   width: 100%;
-   height: 100%;
-}
+  input[type="color"] {
+    width: 4em;
+    height: 4em;
+  }
 </style>
-- 
cgit v1.1

