summaryrefslogtreecommitdiff
path: root/frontend/sw.js
blob: fa6acddb2139ffbe3ae1dd8deb78c3814afea0c7 (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
self.addEventListener('push', (e) => {
	console.log(e.data);
	const received_data = e.data.json();
	console.log(received_data);
	if (received_data.requireInteraction) {
		self.registration.showNotification(received_data.title, {
			body: received_data.body,
			vibrate: [200, 100, 200],
			requireInteraction: received_data.requireInteraction,
			actions: [
				{
					title: "I've collected my laundry!",
					action: "collect",
				}
			],
			data: {timerId: received_data.timerId},
		});
	} else {
		self.registration.showNotification(received_data.title, {
			body: received_data.body,
			vibrate: [200, 100, 200],
		});
	}
});

self.addEventListener("notificationclick", (event) => {
	if (event.action === "collect") {
		timerId = event.notification.data.timerId;
		console.log("finishing timer! w/ id "+timerId.toString());
		fetch(`${API_URL}/finish`, {
			method: "POST",
			credentials: "include",
			headers: {
				"Content-Type": "application/json"
			},
			body: JSON.stringify({id: timerId}),
		});
		clients.openWindow("/timer/");
		event.notification.close();
	} else {
		clients.openWindow("/timer/");
		event.notification.close();
	}
});