Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
122 lines (103 loc) · 3.45 KB
/
Copy pathscript.js
File metadata and controls
122 lines (103 loc) · 3.45 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
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
const database = firebase.database().ref();
const allMessages = document.getElementById("all-messages");
const usernameElem = document.getElementById("username");
const messageElem = document.getElementById("message");
const emailElem = document.getElementById("email");
const imgSrcElem = document.getElementById("profile");
const sendBtn = document.getElementById("send-btn");
sendBtn.onclick = updateDB;
/**
* @TODO create a function called updateDB which takes
* one parameter, the event, that:
* - gets the values of the input elements and stores
* the data in a temporary object with the keys USERNAME
* and MESSAGE
* - console.logs the object above
* - writes this object to the database
* - resets the value of #message input element
*/
function updateDB(event) {
event.preventDefault();
const data = {
USERNAME: usernameElem.value + ':',
MESSAGE: messageElem.value,
EMAIL: emailElem.value,
IMGSRC: imgSrcElem.value
};
console.log(data);
database.push(data);
messageElem.value = "";
}
/**
* @TODO add the addMessageToBoard function as an event
* handler for the "child_added" event on the database
* object
*/
database.on("child_added", addMessageToBoard);
/**
* @TODO create a function called addMessageToBoard that
* takes one parameter rowData which:
* - console.logs the data within rowData
* - creates a new HTML element for a single message
* containing the appropriate data
* - appends this HTML to the div with id
* #all-messages (we should have a reference already!)
*
*/
function addMessageToBoard(rowData) {
// Store the value of rowData inside object named 'data'
const data = rowData.val();
// console.log data
console.log(data);
// Create a variable named singleMessage
let singleMessage = makeSingleMessageHTML(data.IMGSRC, data.USERNAME, data.MESSAGE, data.EMAIL);
// that stores function call for makeSingleMessageHTML()
// Append the new message HTML element to allMessages
allMessages.appendChild(singleMessage);
}
function makeSingleMessageHTML(imgSrc, usernameTxt, messageTxt, emailTxt) {
const parentDiv = document.createElement("div");
parentDiv.classList.add("single-message");
const image = document.createElement("img");
image.src = imgSrc;
image.classList.add("single-message-img");
parentDiv.appendChild(image);
const usernameP = document.createElement("p");
usernameP.innerHTML = usernameTxt;
// Append username
usernameP.style.fontWeight = "bold";
parentDiv.appendChild(usernameP);
const emailP = document.createElement("p");
emailP.style.fontSize = "small";
emailP.innerHTML = emailTxt;
parentDiv.appendChild(emailP);
// Create message P Tag
const messageP = document.createElement("p");
messageP.innerHTML = messageTxt;
parentDiv.appendChild(messageP);
// Return Parent Div
const TimestampP = document.createElement("p");
TimestampP.style.fontSize = "x-small";
TimestampP.innerHTML = new Date().toLocaleString();
parentDiv.appendChild(TimestampP);
return parentDiv;
}
/**
* @BONUS add an onkeyup event handler to the form HTML
* element so the user can also submit the form with the
* Enter key
*
* @BONUS use an arrow function
*/
document.getElementById("text-box-for-message").onkeyup = (event) => {
if (event.key === "Enter") {
updateDB(event);
}
if (event.key === "Delete") {
database
.remove()
.then(() => {
allMessages.innerHTML = "";
})
}
}