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

---
 .../(main)/(public)/reset-password/+page.svelte       | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 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 8bda3dc..eb02b45 100644
--- a/code/app/src/routes/(main)/(public)/reset-password/+page.svelte
+++ b/code/app/src/routes/(main)/(public)/reset-password/+page.svelte
@@ -1,6 +1,7 @@
 <script lang="ts">
     import { Alert, Input, Button } from "$lib/components";
     import LL from "$lib/i18n/i18n-svelte";
+    import { FormError } from "$lib/models/internal/FormError";
     import { PasswordResetService } from "$lib/services/password-reset-service";
 
     const formData = {
@@ -10,22 +11,15 @@
         },
     };
 
-    const formError = {
-        title: "",
-        subtitle: "",
-        set(title = "", subtitle = "") {
-            formError.title = title;
-            formError.subtitle = subtitle;
-        },
-    };
+    const formError = new FormError();
 
     const resetRequests = new PasswordResetService();
 
     let loading = false;
     let showSuccessAlert = false;
-    $: showErrorAlert = (formError.title !== "" || formError.subtitle !== "") && !showSuccessAlert;
+    $: showErrorAlert = formError.has_error() && !showSuccessAlert;
 
-    async function submitFormAsync() {
+    async function submit_form_async() {
         formError.set();
         showSuccessAlert = false;
         loading = true;
@@ -34,8 +28,7 @@
         if (response.isCreated) {
             showSuccessAlert = true;
         } else if (response.knownProblem) {
-            if (response.knownProblem.title) formError.title = response.knownProblem.title;
-            if (response.knownProblem.subtitle) formError.subtitle = response.knownProblem.subtitle;
+            formError.set_from_known_problem(response.knownProblem);
             for (const error of Object.entries(response.knownProblem.errors)) {
                 if (error[0] === "email") {
                     error[1].forEach(formData.email.errors.push);
@@ -63,7 +56,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}>
+            <form class="space-y-6" on:submit|preventDefault={submit_form_async}>
                 <Alert title={formError.title} message={formError.subtitle} type="error" visible={showErrorAlert} />
 
                 <Alert
-- 
cgit v1.3

