-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
147 lines (119 loc) · 4.91 KB
/
app.js
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
const loadButton = document.getElementById('load');
loadButton.addEventListener('click', loadCatches);
const catchesDiv = document.getElementById('catches');
const authToken = sessionStorage.getItem('authToken');
const userId = sessionStorage.getItem('userId');
loadCatches();
if (authToken !== null) {
let guestDiv = document.getElementById('guest');
guestDiv.innerHTML = '';
let newAnchor = document.createElement('a');
newAnchor.textContent = 'Logout';
newAnchor.addEventListener('click', () => {
sessionStorage.removeItem('authToken');
sessionStorage.removeItem('userId');
window.location.pathname = './index.html';
})
guestDiv.appendChild(newAnchor);
const addButton = document.getElementById('add')
addButton.removeAttribute('disabled');
document.getElementById('addForm')
.addEventListener('submit', addNewCatch);
}
async function loadCatches() {
let response = await fetch('http://localhost:3030/data/catches');
let data = await response.json();
catchesDiv.innerHTML = '';
data.forEach(obj => createCatchDOM(obj));
}
async function addNewCatch(e) {
e.preventDefault();
if (authToken === null)
return alert('Unauthorized action!');
let formData = new FormData(e.target);
const angler = formData.get('angler');
const weight = formData.get('weight');
const species = formData.get('species');
const location = formData.get('location');
const bait = formData.get('bait');
const captureTime = formData.get('captureTime');
if (!angler || !weight || !species || !location || !bait || !captureTime)
return alert('Invalid form!');
let response = await fetch ('http://localhost:3030/data/catches ', {
method: 'POST',
headers: {'Content-Type': 'application/json', 'X-Authorization': authToken},
body: JSON.stringify({angler, weight, species, location, bait, captureTime})
});
if (response.ok) {
await loadCatches();
}
else {
return alert('Invalid form!');
}
[...document.querySelector('#addForm').children]
.forEach(ch => ch.value = '');
}
function createCatchDOM(obj) {
let newDiv = document.createElement('div');
newDiv.className = 'catch';
newDiv.innerHTML = `<label>Angler</label>
<input type="text" class="angler" value="${obj.angler}" />
<hr>
<label>Weight</label>
<input type="number" class="weight" value="${obj.weight}" />
<hr>
<label>Species</label>
<input type="text" class="species" value="${obj.species}" />
<hr>
<label>Location</label>
<input type="text" class="location" value="${obj.location}" />
<hr>
<label>Bait</label>
<input type="text" class="bait" value="${obj.bait}" />
<hr>
<label>Capture Time</label>
<input type="number" class="captureTime" value="${obj.captureTime}" />
<hr>`;
let updateButton = document.createElement('button');
updateButton.className = 'update';
updateButton.textContent = 'Update'
let deleteButton = document.createElement('button');
deleteButton.className = 'delete';
deleteButton.textContent = 'Delete'
if (obj._ownerId === userId) {
updateButton.addEventListener('click', () => updateCatch(newDiv, obj, obj._id));
deleteButton.addEventListener('click', () => removeCatch(obj, obj._id));
}
else {
updateButton.disabled = 'true';
deleteButton.disabled = 'true';
}
newDiv.appendChild(updateButton);
newDiv.appendChild(deleteButton);
catchesDiv.appendChild(newDiv);
}
async function updateCatch(DOMObj, obj, id) {
const angler = DOMObj.querySelector('.angler').value;
const weight = DOMObj.querySelector('.weight').value;
const species = DOMObj.querySelector('.species').value;
const location = DOMObj.querySelector('.location').value;
const bait = DOMObj.querySelector('.bait').value;
const captureTime = DOMObj.querySelector('.captureTime').value;
if (obj._ownerId !== userId)
return alert('Unauthorized action.');
let response = await fetch(`http://localhost:3030/data/catches/${id}`, {
method: 'PUT',
headers: {'Content-Type': 'application/json', 'X-Authorization': authToken},
body: JSON.stringify({angler, weight, species, location, bait, captureTime})
});
await loadCatches();
}
async function removeCatch(obj, id) {
if (obj._ownerId !== userId)
return alert('Suspicious operation.');
let response = await fetch(`http://localhost:3030/data/catches/${id}`, {
method: 'DELETE',
headers: {'Content-Type': 'application/json', 'X-Authorization': authToken}
});
await loadCatches();
}