-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.svelte
114 lines (97 loc) · 2.15 KB
/
index.svelte
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
<script context="module" lang="ts">
export async function preload() {
let today: number;
let history: Day[];
let sessionExpired = false;
let error: Error = null;
try {
const res = await this.fetch(`/api/mood`);
if (res.status == 200) {
({ today, history } = await res.json());
} else if (res.status === 401) {
sessionExpired = true;
}
} catch (e) {
error = e;
}
return { today, history, sessionExpired, error };
}
</script>
<script lang="ts">
import LoggedInHome from "../components/LoggedInHome.svelte";
import { stores } from "@sapper/app";
import type { Day } from "../types";
export let today: number;
export let history: Day[];
export let sessionExpired: boolean;
export let error: Error = null;
const onError = (e: Error) => {
error = e;
};
const onLoginExpired = () => {
sessionExpired = true;
onError(new Error("Your session has expired. Please log in again."));
};
const storeMood = (score: number) => {
fetch("/api/mood", {
method: "PUT",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ today: score })
}).then((res) => {
if (res.status == 401) {
onLoginExpired();
} else {
onError(res.status != 200 ? new Error("Failed to store") : null);
}
}, onError);
};
const { session } = stores();
</script>
<style lang="scss">
.container {
display: flex;
margin-top: 2em;
.content {
margin: 0 auto;
#login {
background-color: #328598;
color: white;
font-weight: 700;
border: none;
border-radius: 2px;
padding: 1em;
font-size: 1.2em;
cursor: pointer;
}
}
}
.error {
text-align: center;
position: fixed;
top: 0;
left: 0;
right: 0;
padding: 0.5em;
background-color: #f55252;
color: white;
}
</style>
<svelte:head>
<title>Mood Tracker</title>
</svelte:head>
<div class="container">
<div class="content">
{#if error}
<div class="error">{error.message}</div>
{/if}
{#if $session.user && !sessionExpired}
<LoggedInHome {storeMood} {today} {history} />
{:else}
<button
id="login"
on:click={() => (document.location.href = '/login')}>Log in</button>
{/if}
</div>
</div>