aboutsummaryrefslogtreecommitdiff
path: root/fe/src/lib/DataView.svelte
blob: 2b1b8b9bf86aaa7eafa75dfc6f3a3e6a6a9bce98 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<script lang="ts">
    import { onDestroy, onMount } from "svelte";
    import { token } from "../stores/auth";
    import { addFormOpen } from "../stores/forms";
    import Table from "./Table.svelte";
    import Chart from "chart.js/auto";
    import Card from "./Card.svelte";
    import Column from "./Column.svelte";
    import AddForm from "./forms/AddForm.svelte";

    let json: Promise<any>;
    let totals: Promise<any>;
    let userStats: Promise<any>;

    let barCanvasRef: HTMLCanvasElement;
    let lineCanvasRef: HTMLCanvasElement;
    let barChart: any;
    let lineChart: any;

    let lastSevenDays: string[];

    async function fetchData() {
        const res = await fetch("http://localhost:8080/api/v1/stats/", {
            method: "GET",
            headers: {
                Authorization: `Bearer ${$token}`
            }
        });
        if (res.ok) {
            json = res.json();
        } else {
            throw new Error("There was a problem with your request");
        }
    }

    async function fetchTotals() {
        const res = await fetch("http://localhost:8080/api/v1/stats/totals/", {
            method: 'GET',
            mode: 'no-cors',
            headers: {
                Authorization: `Bearer ${$token}`
            }
        });

        if (res.ok) {
            totals = res.json();
        } else {
            throw new Error("There was a problem with your request");
        }
    }

    async function fetchStatsForUser() {
        const res = await fetch("http://localhost:8080/api/v1/stats/user/1aa668f3-7527-4a67-9c24-fdf307542eeb", {
            method: "GET",
            headers: {
                Authorization: `Bearer ${$token}`
            }
        });

        if (res.ok) {
            userStats = res.json();
        } else {
            throw new Error("There was a problem with your request");
        }
    }

    function getLastSevenDays() {
        const result = [];
        for (let i = 0; i < 7; i++) {
            let d = new Date();
            d.setDate(d.getDate() - i);
            result.push(d.toISOString().substring(0, 10));
        }
        return result;
    }

    function closeDialog() {
        addFormOpen.set(false);
    }

    function onStatisticAdd() {
        closeDialog();
        fetchData();
    }

    onMount(() => {
        fetchData();
//        fetchTotals();
        fetchStatsForUser();
        lastSevenDays = getLastSevenDays();
        barChart = new Chart(barCanvasRef, {
            type: "bar",
            data: {
                labels: lastSevenDays,
                datasets: [
                    {
                        label: "Zach",
                        data: [1, 2, 8, 2, 5, 5, 1],
                        backgroundColor: "rgba(255, 192, 192, 0.2)",
                        borderColor: "rgba(75, 192, 192, 1)",
                        borderWidth: 1
                    }, {
                        label: "Parker",
                        data: [6, 1, 1, 4, 3, 5, 1],
                        backgroundColor: "rgba(75, 192, 192, 0.2)",
                        borderColor: "rgba(75, 192, 192, 1)",
                        borderWidth: 1
                    }
                ]
            },
            options: {
                responsive: true
            }
        });
        lineChart = new Chart(lineCanvasRef, {
            type: "line",
            data: {
                labels: lastSevenDays,
                datasets: [
                    {
                        label: "Zach",
                        data: [1, 2, 8, 2, 5, 5, 1],
                        backgroundColor: "rgba(255, 192, 192, 0.2)",
                        borderColor: "rgba(75, 192, 192, 1)",
                        borderWidth: 1
                    }, {
                        label: "Parker",
                        data: [6, 1, 1, 4, 3, 5, 1],
                        backgroundColor: "rgba(75, 192, 192, 0.2)",
                        borderColor: "rgba(75, 192, 192, 1)",
                        borderWidth: 1
                    }
                ]
            },
            options: {
                responsive: true
            }
        });
    });

    onDestroy(() => {
        if (barChart) barChart.destroy();
        if (lineChart) lineChart.destroy();
        barChart = null;
        lineChart = null;
    });
</script>

<Column --width="500px">
    <Card>
        <canvas bind:this={barCanvasRef} width="" />
    </Card>
    <Card>
        <canvas bind:this={lineCanvasRef} />
    </Card>
</Column>

<AddForm open={$addFormOpen} on:submit={onStatisticAdd} on:close={closeDialog} />
<Column>
    <Card>
        {#await json then data}
            <Table {data} nofooter />
        {:catch error}
            <p>{error}</p>
        {/await}
    </Card>
    <Card>
        <button on:click={() => fetchTotals()}>Get totals</button>
        {#await totals then data}
            {JSON.stringify(data)}
        {:catch error}
            <p>{error}</p>
        {/await}
    </Card>
</Column>
<!-- <Chart /> -->


<style>
    dialog {
        background: red;
        box-shadow: 0 20px 5em 10px rgba(0, 0, 0, 0.8);
    }

    dialog::backdrop {
        padding: 20px;
        box-shadow: 20px 20px rgba(0, 0, 0, 0.8);
        background-color: red;
    }
</style>