From 94cb59310f5cde903d8c94942b0c56dceb80bf79 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Sat, 26 Nov 2022 19:28:56 +0900
Subject: refactor: Add and use FormError class to handle form error logic

---
 .../routes/(main)/(public)/sign-in/+page.svelte    | 53 ++++++++++------------
 1 file changed, 25 insertions(+), 28 deletions(-)

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

diff --git a/code/app/src/routes/(main)/(public)/sign-in/+page.svelte b/code/app/src/routes/(main)/(public)/sign-in/+page.svelte
index 01c65dd..cf6da75 100644
--- a/code/app/src/routes/(main)/(public)/sign-in/+page.svelte
+++ b/code/app/src/routes/(main)/(public)/sign-in/+page.svelte
@@ -1,16 +1,16 @@
 <script lang="ts">
     import { goto } from "$app/navigation";
-    import { http_account } from "$lib/api/account";
     import { Button, Checkbox, Input, Alert } from "$lib/components";
     import LL from "$lib/i18n/i18n-svelte";
     import pwKey from "$actions/pwKey";
-    import { isOk } from "rustic";
     import { onMount } from "svelte";
     import { signInPageMessageQueryKey, signInPageTestKeys, type SignInPageMessage } from ".";
     import { AccountService } from "$lib/services/account-service";
     import type { LoginPayload } from "$lib/services/abstractions/IAccountService";
+    import { FormError } from "$lib/models/internal/FormError";
 
     let loading = false;
+    let showErrorAlert = false;
     let messageType: SignInPageMessage | undefined = undefined;
 
     const accountService = new AccountService();
@@ -28,18 +28,16 @@
             value: false,
             errors: [],
         },
-    };
-
-    const formError = {
-        title: "",
-        subtitle: "",
-        set(title = "", subtitle = "") {
-            formError.title = title;
-            formError.subtitle = subtitle;
+        as_payload(): LoginPayload {
+            return {
+                password: formData.password.value,
+                username: formData.username.value,
+                persist: !formData.persist.value,
+            };
         },
     };
-    
-    $: showErrorAlert = (errorData.text?.length ?? 0 + errorData.title?.length ?? 0) > 0;
+
+    const formError = new FormError();
 
     onMount(() => {
         const searcher = new URLSearchParams(window.location.search);
@@ -50,22 +48,21 @@
         }
     });
 
-    async function submitFormAsync() {
-        errorData = { text: "", title: "" };
+    async function submit_form_async() {
+        formError.set();
+        showErrorAlert = formError.has_error();
         loading = true;
-        data.persist = !data.persist;
-        const loginResponse = await http_account.login_async(data);
-        if (isOk(loginResponse)) {
+        const loginResponse = await accountService.login_async(formData.as_payload());
+        if (loginResponse.isLoggedIn) {
             await goto("/home");
+        } else if (loginResponse.knownProblem) {
+            formError.set_from_known_problem(loginResponse.knownProblem);
         } else {
-            errorData.title = loginResponse.data.title;
-            errorData.text = loginResponse.data.subtitle;
-            if (!errorData.text && !errorData.title) {
-                errorData.text = $LL.tryAgainSoon();
-                errorData.title = $LL.unexpectedError();
-            }
+            formError.title = $LL.unexpectedError();
+            formError.subtitle = $LL.tryAgainSoon();
         }
         loading = false;
+        showErrorAlert = formError.has_error();
     }
 </script>
 
@@ -108,9 +105,9 @@
     <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">
             {#if showErrorAlert}
-                <Alert title={errorData.title} message={errorData.text} type="error" _pwKey={signInPageTestKeys.formErrorAlert} />
+                <Alert title={formError.title} message={formError.subtitle} type="error" _pwKey={signInPageTestKeys.formErrorAlert} />
             {/if}
-            <form class="space-y-6 mt-2" use:pwKey={signInPageTestKeys.signInForm} on:submit|preventDefault={submitFormAsync}>
+            <form class="space-y-6 mt-2" use:pwKey={signInPageTestKeys.signInForm} on:submit|preventDefault={submit_form_async}>
                 <Input
                     id="username"
                     _pwKey={signInPageTestKeys.usernameInput}
@@ -118,7 +115,7 @@
                     type="email"
                     label={$LL.emailAddress()}
                     required
-                    bind:value={data.username}
+                    bind:value={formData.username.value}
                 />
 
                 <Input
@@ -129,7 +126,7 @@
                     _pwKey={signInPageTestKeys.passwordInput}
                     autocomplete="current-password"
                     required
-                    bind:value={data.password}
+                    bind:value={formData.password.value}
                 />
 
                 <div class="flex items-center justify-between">
@@ -137,7 +134,7 @@
                         id="remember-me"
                         _pwKey={signInPageTestKeys.rememberMeCheckbox}
                         name="remember-me"
-                        bind:checked={data.persist}
+                        bind:checked={formData.persist.value}
                         label={$LL.signInPage.notMyComputer()}
                     />
                     <div class="text-sm">
-- 
cgit v1.3

