-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
67 lines (54 loc) · 1.93 KB
/
Copy pathindex.js
File metadata and controls
67 lines (54 loc) · 1.93 KB
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
const authFormEl = document.getElementById('auth-form');
const addNoteButtonEl = document.getElementById("add-note");
const addNoteFormEl = document.getElementById("add-note-form");
const notesListEl = document.getElementById('notes-list');
let tvClient = null;
let tvUserId = null;
const setState = async (newState) => {
authFormEl.style.display = 'none';
addNoteButtonEl.style.display = 'none';
addNoteFormEl.style.display = 'none';
notesListEl.style.display = 'none';
switch (newState) {
case '#notes':
const response = await tvClient.listDocuments(TV_VAULT_ID, true);
if (response.items.length > 0) {
notesListEl.innerHTML = response.items.map(
item => `<div class="note">${item.document.note}</div>`
).join('');
}
notesListEl.style.display = '';
addNoteButtonEl.style.display = '';
break;
case '#add-note':
addNoteFormEl.style.display = '';
break;
case '#login':
default:
authFormEl.style.display = '';
break;
}
};
authFormEl.addEventListener('submit', async e => {
e.preventDefault();
tvClient = await TrueVaultClient.login(TV_ACCOUNT_ID, this.username.value, this.password.value);
const response = await tvClient.readCurrentUser();
tvUserId = response.id;
location.hash = '#notes';
});
addNoteButtonEl.addEventListener('click', async e => {
e.preventDefault();
location.hash = '#add-note';
});
addNoteFormEl.addEventListener('submit', async e => {
e.preventDefault();
const document = {'note': this.note.value};
await tvClient.createDocument(TV_VAULT_ID, null, document, tvUserId);
this.note.value = '';
location.hash = '#notes';
});
window.onpopstate = async () => setState(location.hash);
const refreshState = async () => {
await setState(location.hash);
};
refreshState();