summaryrefslogtreecommitdiff
path: root/src/server/web/cli.js
blob: 3dff1d4860431a305e7c1857c62e8816a94275d4 (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
'use strict';

window.onload = async () => {
	const account = JSON.parse(localStorage.getItem('account'));
	const i = account.token;

	const api = (endpoint, data = {}) => {
		const promise = new Promise((resolve, reject) => {
			// Append a credential
			if (i) data.i = i;
	
			// Send request
			fetch(endpoint.indexOf('://') > -1 ? endpoint : `/api/${endpoint}`, {
				method: 'POST',
				body: JSON.stringify(data),
				credentials: 'omit',
				cache: 'no-cache'
			}).then(async (res) => {
				const body = res.status === 204 ? null : await res.json();
	
				if (res.status === 200) {
					resolve(body);
				} else if (res.status === 204) {
					resolve();
				} else {
					reject(body.error);
				}
			}).catch(reject);
		});
		
		return promise;
	};

	document.getElementById('submit').addEventListener('click', () => {
		api('notes/create', {
			text: document.getElementById('text').value
		}).then(() => {
			location.reload();
		});
	});

	api('notes/timeline').then(notes => {
		const tl = document.getElementById('tl');
		for (const note of notes) {
			const el = document.createElement('div');
			const name = document.createElement('header');
			name.textContent = `${note.user.name} @${note.user.username}`;
			const text = document.createElement('div');
			text.textContent = `${note.text}`;
			el.appendChild(name);
			el.appendChild(text);
			tl.appendChild(el);
		}
	});
};