-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
88 lines (77 loc) · 2.32 KB
/
script.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
function addNewEntry(entryText) {
// create a new entry element
const newEntry = document.createElement("div");
// inject the text into the new element
newEntry.textContent = entryText;
// style the new element with .entry class
newEntry.classList.add("entry");
// append new entry to the list of entries the user sees
document.getElementById("entry-list").appendChild(newEntry);
}
function fetchEntries() {
fetch("http://localhost:3000/entries").then(response => {
return response.json();
}).then(entries => {
entries.forEach(entry => {
addNewEntry(entry.text)
});
});
}
// entry is an object like { text: "Hello World" }
function createEntry(entry) {
let hasError = false;
return fetch("http://localhost:3000/entries", {
method: "POST",
body: JSON.stringify(entry),
headers: {
"Content-Type": "application/json",
},
})
.then(response => {
if (!response.ok) {
hasError = true;
}
return response.json();
})
.then(responseJson => {
if (hasError) {
throw new Error(responseJson.error); // { "error": "The message" }
}
return responseJson;
});
}
fetchEntries();
// exercise: .catch((error) => { /* ... */ })
const submitButton = document.getElementById("entry-submit-button");
submitButton.addEventListener("click", (event) => {
// find out what the user typed in the text area
const textarea = document.getElementById("new-entry-textarea");
const entryText = textarea.value;
const errorSpan = document.getElementsByClassName("new-entry-error")[0];
// frontend validation
if (entryText === "") {
if (errorSpan) {
errorSpan.classList.remove("new-entry-error-hidden");
}
return;
}
else if (entryText.length > 256) {
if (errorSpan) {
errorSpan.textContent = "Entry text must not be longer than 256 characters";
errorSpan.classList.remove("new-entry-error-hidden");
}
return;
}
createEntry({ text: entryText })
.then((entry) => {
// clear the text area
textarea.value = "";
// clear any error message
errorSpan && errorSpan.classList.add("new-entry-error-hidden");
addNewEntry(entry.text);
})
.catch(error => {
errorSpan.textContent = error.message;
errorSpan.classList.remove("new-entry-error-hidden");
});
});