From e351ffdd2adf39c9ba11bbfcda7835da20acba5f Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Thu, 17 Nov 2022 13:53:22 +0700
Subject: fix: WIP use new api structure

---
 .../(main)/(public)/reset-password/+page.svelte    | 51 ++++++++++++----------
 1 file changed, 28 insertions(+), 23 deletions(-)

(limited to 'code/app/src/routes/(main)/(public)/reset-password/+page.svelte')

diff --git a/code/app/src/routes/(main)/(public)/reset-password/+page.svelte b/code/app/src/routes/(main)/(public)/reset-password/+page.svelte
index 32d4e21..3bdba80 100644
--- a/code/app/src/routes/(main)/(public)/reset-password/+page.svelte
+++ b/code/app/src/routes/(main)/(public)/reset-password/+page.svelte
@@ -1,38 +1,48 @@
 <script lang="ts">
-    import { create_forgot_password_request } from "$lib/api/account";
     import { Alert, Input, Button } from "$lib/components";
+    import X from "$lib/components/icons/x.svelte";
     import LL from "$lib/i18n/i18n-svelte";
-    import type { ErrorResult } from "$lib/models/ErrorResult";
+    import { PasswordResetService } from "$lib/services/password-reset-service";
 
     const formData = {
-        email: "",
+        email: {
+            value: "",
+            error: "",
+        },
     };
 
-    $: showErrorAlert =
-        (errorData?.text.length ?? 0 + errorData?.title.length ?? 0) > 0 &&
-        !showSuccessAlert;
-
-    const errorData = {
-        text: "",
+    const formError = {
         title: "",
-    } as ErrorResult;
+        subtitle: "",
+        set(title = "", subtitle = "") {
+            formError.title = title;
+            formError.subtitle = subtitle;
+        },
+    };
+
+    const service = new PasswordResetService();
 
     let loading = false;
     let showSuccessAlert = false;
+    $: showErrorAlert = formError.title || (formError.subtitle && !showSuccessAlert);
 
     async function submitFormAsync() {
-        errorData.text = "";
-        errorData.title = "";
+        formError.set();
         showSuccessAlert = false;
         loading = true;
-        const request = await create_forgot_password_request(formData.email);
+        const response = await service.create_request_async(formData.email.value);
         loading = false;
-        if (!request.ok) {
-            errorData.text = request.data.text ?? $LL.tryAgainSoon();
-            errorData.title = request.data.title ?? $LL.unexpectedError();
+        if (response.isCreated) {
+            showSuccessAlert = true;
             return;
         }
-        showSuccessAlert = true;
+        if (response.knownProblem) {
+            if (response.knownProblem.title) formError.title = response.knownProblem.title;
+            if (response.knownProblem.subtitle) formError.subtitle = response.knownProblem.subtitle;
+            for (const error of response.knownProblem.errors) {
+            
+            }
+        }
     }
 </script>
 
@@ -52,12 +62,7 @@
     <div class="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
         <div class="bg-white py-8 px-4 shadow sm:rounded-lg sm:px-10">
             <form class="space-y-6" on:submit|preventDefault={submitFormAsync}>
-                <Alert
-                    title={errorData.title}
-                    message={errorData.text}
-                    type="error"
-                    visible={showErrorAlert}
-                />
+                <Alert title={errorData.title} message={errorData.text} type="error" visible={showErrorAlert} />
 
                 <Alert
                     type="success"
-- 
cgit v1.3

