-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
64 lines (52 loc) · 1.76 KB
/
Copy pathclient.js
File metadata and controls
64 lines (52 loc) · 1.76 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
const form = document.getElementById('dog-form');
const loadingElement = document.querySelector('.loading');
const woofsElement = document.querySelector('.woofs');
const API_URL = 'http://localhost:5500/woofs';
loadingElement.style.display = 'none';
listAllWoofs();
form.addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(form);
const name = formData.get('name');
const content = formData.get('content');
const woof = {
name,
content
};
form.style.display = 'none';
loadingElement.style.display = '';
fetch(API_URL, {
method: 'POST',
body: JSON.stringify(woof),
headers: {
'content-type': 'application/json'
}
}).then(response => response.json())
.then(createdWoof => {
form.reset();
form.style.display = '';
loadingElement.style.display = '';
listAllWoofs();
});
});
function listAllWoofs() {
woofsElement.innerHTML = '';
fetch(API_URL)
.then(response => response.json())
.then(woofs => {
woofs.reverse();
woofs.forEach(woof => {
const div = document.createElement('div');
const header = document.createElement('h3');
header.textContent = woof.name;
const contents = document.createElement('p');
contents.textContent = woof.content;
const date = document.createElement('small');
date.textContent = new Date(woof.created);
div.appendChild(header);
div.appendChild(contents);
div.appendChild(date);
woofsElement.appendChild(div);
});
});
}