aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/routes/+page.svelte
blob: 7b0a4951bbb4bf256c97409b5ee74c376414f2b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<script lang="ts">
    import Button from "$lib/ui/button.svelte";
    import { clear_auth_session, init_auth_session } from "./sb1.remote";
    import type { PageProps } from "./$types";
    import { Temporal } from "temporal-polyfill";
    import { instantAsHtmlInputValueString } from "$lib/helpers";

    let { data }: PageProps = $props();
    let navigating = $state(false);
    let form = $state({
        budgetId: "",
        mappings: [] as Array<{ sb1: string; actual: string }>,
        delta: instantAsHtmlInputValueString(Temporal.Now.instant().subtract("PT24H")),
        dry: true,
    });
    async function run() {}

    async function authorize() {
        navigating = true;
        const url = await init_auth_session();
        location.href = url;
    }

    async function logout() {
        navigating = true;
        await clear_auth_session();
        location.reload();
    }

    function mappingChanged(sb1: string, actual: string) {
        let mappings = form.mappings;
        if (mappings.find((c) => c.sb1 === sb1)) mappings = mappings.filter((c) => c.sb1 !== sb1);
        mappings.push({ sb1, actual });
        form.mappings = mappings;
    }

    $inspect(form);
</script>

<main>
    {#if data.sb1.accounts?.length}
        <form onsubmit={run}>
            <h3>Importer</h3>
            <fieldset>
                <h4>Budsjett</h4>
                {#each data.actual.budgets as budget}
                    {@const id = `budget-${budget.id}`}
                    <input name="budget" {id} value={budget.id} type="radio" bind:group={form.budgetId} />
                    <label for={id}>{budget.name}({budget.id})</label><br />
                {/each}
                <h4>Kontoer</h4>
                {#each data.sb1.accounts as account}
                    {@const actualId = `mapping-${account.key}-actual`}
                    <div>
                        <code>{account.name}</code>
                        <span>&#8594;</span>
                        <label for={actualId}>Actual</label>
                        <select name={actualId} id={actualId} onchange={(e) => mappingChanged(account.key, e.currentTarget.value)}>
                            <option value="-" selected>-</option>
                            {#each data.actual.accounts as actual}
                                <option value={actual.id}>
                                    {actual.name}
                                </option>
                            {/each}
                        </select>
                    </div>
                {/each}
                <h4>Ellers</h4>
                <label for="delta">Importer transaksjoner siden</label>
                <input type="date" id="delta" bind:value={form.delta} /><br />
                <input type="checkbox" id="dry" bind:checked={form.dry} /><label for="dry">Tørrkjøring</label><br /><br />
                <input type="submit" />
            </fieldset>
        </form>
        <h3>Annet</h3>
        <Button onclick={logout} loading={navigating}>Logg ut</Button>
    {:else}
        <Button onclick={authorize} loading={navigating}>Autentisér hos Sparebanken 1</Button>
    {/if}
</main>