forked from ZCW-Java8-1/YouAreEllGfxClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (56 loc) · 2.12 KB
/
index.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
import MessageService from "./message-service.js";
let userId = "Ogomes21";
const messageService = new MessageService();
window.addEventListener("load", function () {
document.getElementById("greeting").innerHTML = `Welcome, ${userId}!`;
messageService.getAllMessages()
.then(successCallback, errorCallback);
function successCallback(response) {
// This data comes from the resolve method
populateMessages(response);
}
function errorCallback(response) {
// This data comes from the reject method
console.log(response);
}
});
function populateMessages(messages) {
messages.forEach(message => {
addMessageToThread(message);
})
}
function createFormListener() {
const form = document.getElementById("new-message-form");
form.onsubmit = function (event) {
// stop the regular form submission
event.preventDefault();
const data = {
fromid: userId,
message: form.message.value
};
messageService.createNewMessage(data)
.then(successCallback, errorCallback);
function successCallback(response){
// This data comes from the resolve method
addMessageToThread(response);
}
function errorCallback(response) {
// This data comes from the reject method
console.log(response);
}
}
};
function addMessageToThread(message) {
const messageListItem = document.createElement("LI");
const userIdHeading = document.createElement("h3");
const messageParagraph = document.createElement("p");
const messageContent = document.createTextNode(message.message);
const userIdContent = document.createTextNode(message.fromid);
userIdHeading.appendChild(userIdContent);
messageParagraph.appendChild(messageContent);
messageListItem
.appendChild(userIdHeading)
.appendChild(messageParagraph);
document.getElementById("message-list").appendChild(messageListItem);
}
createFormListener();