From a9072370ca1eb9a5cce928b1d487db0f307edea6 Mon Sep 17 00:00:00 2001
From: ivarlovlie <git@ivarlovlie.no>
Date: Tue, 20 Sep 2022 15:24:27 +0800
Subject: feat: Move old apps into it's own directory

---
 old-apps/portal/src/app/pages/profile/index.svelte | 167 +++++++++++++++++++++
 1 file changed, 167 insertions(+)
 create mode 100644 old-apps/portal/src/app/pages/profile/index.svelte

(limited to 'old-apps/portal/src/app/pages/profile/index.svelte')

diff --git a/old-apps/portal/src/app/pages/profile/index.svelte b/old-apps/portal/src/app/pages/profile/index.svelte
new file mode 100644
index 0000000..a7291d6
--- /dev/null
+++ b/old-apps/portal/src/app/pages/profile/index.svelte
@@ -0,0 +1,167 @@
+<script>
+    import {push} from "svelte-spa-router";
+    import {Bread, Crumb} from "$shared/components/breadcrumb/index";
+    import Layout from "$app/pages/_layout@loggedin.svelte";
+    import {update_profile} from "$shared/lib/api/user";
+    import Alert from "$shared/components/alert.svelte";
+    import Button from "$shared/components/button.svelte";
+    import {is_email} from "$shared/lib/helpers";
+    import {api_base} from "$shared/lib/configuration";
+    import {get_session_data} from "$shared/lib/session";
+
+    const archiveLink = api_base("_/account/archive");
+
+    let modal;
+    let understands = false;
+
+    let formIsLoading = false;
+    let formError;
+
+    let username = get_session_data()?.profile.username;
+    let usernameFieldMessage;
+    let usernameFieldMessageClass = "color-error";
+
+    let password;
+    let passwordFieldMessage;
+    let passwordFieldMessageClass = "color-error";
+
+    async function submit_form(e) {
+        e.preventDefault();
+        if (!username && !password) {
+            console.error("Not submitting because both values is empty");
+            return;
+        }
+
+        usernameFieldMessage = "";
+        passwordFieldMessage = "";
+
+        if (username && !is_email(username)) {
+            usernameFieldMessage = "Username has to be a valid email";
+            return;
+        }
+
+        if (password && password?.length < 6) {
+            passwordFieldMessage = "The new password must contain at least 6 characters";
+            return;
+        }
+
+        formIsLoading = true;
+
+        const response = await update_profile({
+            username,
+            password,
+        });
+
+        formIsLoading = false;
+
+        if (response.ok) {
+            if (password) {
+                passwordFieldMessage = "Successfully updated";
+                passwordFieldMessageClass = "color-success";
+                password = "";
+            }
+            if (username) {
+                usernameFieldMessage = "Successfully updated";
+                usernameFieldMessageClass = "color-success";
+                password = "";
+            }
+        } else {
+            formError = response.data.title ?? "An unknown error occured";
+        }
+    }
+
+    async function handle_delete_account_button_click() {
+        alert("Not implemented");
+        return;
+        if (understands && confirm("Are you absolutely sure that you want to delete your account?")) {
+        }
+    }
+
+    export const functions = {
+        open() {
+            modal.open();
+        },
+        close() {
+            // modal.close();
+        },
+    };
+</script>
+
+<Layout>
+    <Bread>
+        <Crumb name="Home"
+               withArrow="true"
+               isLink="true"
+               on:click={() => push("/")}/>
+        <Crumb name="Profile"/>
+    </Bread>
+
+    <main class="max-width-sm">
+        <section class="margin-bottom-md">
+            <p class="text-md margin-bottom-sm">Update your information</p>
+            <form on:submit={submit_form}
+                  autocomplete="new-password">
+                {#if formError}
+                    <small class="color-danger">{formError}</small>
+                {/if}
+                <div class="margin-bottom-sm">
+                    <label for="email"
+                           class="form-label margin-bottom-xxs">New username</label>
+                    <input type="email"
+                           class="form-control width-100%"
+                           id="email"
+                           placeholder={username}
+                           bind:value={username}/>
+                    {#if usernameFieldMessage}
+                        <small class={usernameFieldMessageClass}>{usernameFieldMessage}</small>
+                    {/if}
+                </div>
+                <div class="margin-bottom-sm">
+                    <label for="password"
+                           class="form-label margin-bottom-xxs">New password</label>
+                    <input type="password"
+                           class="form-control width-100%"
+                           id="password"
+                           bind:value={password}/>
+                    {#if passwordFieldMessage}
+                        <small class={passwordFieldMessageClass}>{passwordFieldMessage}</small>
+                    {/if}
+                </div>
+                <div class="flex justify-end">
+                    <Button text="Save"
+                            on:click={submit_form}
+                            variant="primary"
+                            loading={formIsLoading}/>
+                </div>
+            </form>
+        </section>
+        <section class="margin-bottom-md">
+            <p class="text-md margin-bottom-sm">Download your data</p>
+            <a class="btn btn--subtle"
+               href={archiveLink}
+               download>Click here to download your data</a>
+        </section>
+        <section>
+            <p class="text-md margin-bottom-sm">Delete account</p>
+            <div class="margin-bottom-sm">
+                <Alert
+                        message="Deleting your account and data means that all of your data (entries, categories, etc.) will be unrecoverable forever.<br>You should probably download your data before continuing."
+                        type="info"
+                />
+            </div>
+            <div class="form-check margin-bottom-sm">
+                <input type="checkbox"
+                       class="checkbox"
+                       id="the-consequences"
+                       bind:checked={understands}/>
+                <label for="the-consequences">I understand the consequences of deleting my account and data.</label>
+            </div>
+            <div class="flex justify-end">
+                <Button text="Delete everything"
+                        variant="accent"
+                        disabled={!understands}
+                        on:click={handle_delete_account_button_click}/>
+            </div>
+        </section>
+    </main>
+</Layout>
-- 
cgit v1.3

