<script>
    import LogoutButton from "$lib/navigation/LogoutButton.svelte";
    import Header from "$lib/navigation/header.svelte";
    import Loading from "$lib/navigation/loading.svelte";
    import Nav from "$lib/navigation/nav.svelte";
    import { getApiUrl } from "$lib/scripts/upstream";
    import { uuid } from "$lib/stores/uuid.js";
    import { onMount } from "svelte";
    const title = "Your Data";

    let data = [
        {
            domain: "User not Found",
            atime: "User not Found",
        },
    ];

    let isLoading = false;

    async function fetchData() {
        if ($uuid != "null") {
            const url = `${getApiUrl()}/api/v2/user/${$uuid}`;
            const res = await fetch(url);
            const json = await res.json();
            if (json.length) {
                data = json;
            }
        }
    }

    onMount(async () => {
        isLoading = true;
        await fetchData();
        isLoading = false;
    });
</script>

<Header {title} />

<p>OwlBoard stores as little data about you as possible to offer the service.</p>
<p>Your randomly generated UUID is not displayed, this is stored in your browser and on the OwlBoard server.</p>
<p>The data below is the entirity of the data we hold about you and constitutes a response to a `Subject Access Request` under GDPR legislation.</p>
<br /><br />

{#if isLoading}
    <Loading />
{:else if data[0].domain != "User not Found"}
    <p class="api_response">Registration Domain: {data[0]["domain"]}</p>
    <p class="api_response">Access Time: {data[0]["atime"]}</p>
    <LogoutButton />
    <p>
        Clicking the logout button will delete your data from your browser. You will then be able to make a new account, your old account will remain inactive and be deleted after
        90 days.
    </p>
{:else}
    <p class="api_response">You are not registered, we don't have any data stored.</p>
{/if}

<Nav />

<style>
    p {
        margin: 10px;
    }
    .api_response {
        color: white;
    }
</style>
