Skip to content

Commit 298813e

Browse files
committed
refactor(frontend): extract renderMessages and tidy polling/render flow
- Move DOM creation and update logic into new renderMessages(data) function - getAllMessages now delegates rendering, schedules next poll, and logs fetch errors
1 parent 284e6a8 commit 298813e

1 file changed

Lines changed: 49 additions & 43 deletions

File tree

chat-app/frontend/script.js

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ let lastIdSeen = -1;
66
async function getAllMessages() {
77
try {
88
const response = await fetch(
9-
`${API_BASE_URL}/messages?since=${lastIdSeen}`);
9+
`${API_BASE_URL}/messages?since=${lastIdSeen}`,
10+
);
1011

1112
// check if the response is not ok
1213
if (!response.ok) {
@@ -17,58 +18,63 @@ async function getAllMessages() {
1718
// we only get here if the response was ok
1819
const data = await response.json();
1920

20-
const messageContainer = document.getElementById("all-messages");
21+
renderMessages(data);
2122

22-
data.forEach((message) => {
23-
const elementId = "msg-" + message.id;
23+
setTimeout(getAllMessages, 0);
24+
} catch (error) {
25+
setTimeout(getAllMessages, 0);
26+
console.error("Error fetching messages:", error);
27+
}
28+
}
2429

25-
const existingElement = document.getElementById(elementId);
30+
function renderMessages(data) {
31+
const messageContainer = document.getElementById("all-messages");
2632

27-
if (existingElement) {
28-
// find the specific span that hold the likes
29-
const likeSpan = document.getElementById("likes-count-" + message.id);
33+
data.forEach((message) => {
34+
const elementId = "msg-" + message.id;
3035

31-
// update only that span
32-
if (likeSpan) {
33-
likeSpan.textContent = `(${message.likes} Likes) `;
34-
}
35-
} else {
36-
const newElement = document.createElement("div");
37-
newElement.id = "msg-" + message.id;
36+
const existingElement = document.getElementById(elementId);
3837

39-
// layer 1: the text
40-
const textSpan = document.createElement("span");
41-
textSpan.textContent = `${message.sender}: ${message.text} `;
38+
if (existingElement) {
39+
// find the specific span that hold the likes
40+
const likeSpan = document.getElementById("likes-count-" + message.id);
4241

43-
//Layer 2: the counter (this is the one we will update later)
44-
const likeSpan = document.createElement("span");
45-
likeSpan.id = "likes-count-" + message.id;
42+
// update only that span
43+
if (likeSpan) {
4644
likeSpan.textContent = `(${message.likes} Likes) `;
47-
48-
// Layer 3: the button
49-
const likeButton = document.createElement("button");
50-
likeButton.textContent = "Like";
51-
52-
likeButton.addEventListener("click", async () => {
53-
await fetch(`${API_BASE_URL}/messages/${message.id}/like`, {
54-
method: "POST",
55-
});
45+
}
46+
} else {
47+
const newElement = document.createElement("div");
48+
newElement.id = "msg-" + message.id;
49+
50+
// layer 1: the text
51+
const textSpan = document.createElement("span");
52+
textSpan.textContent = `${message.sender}: ${message.text} `;
53+
54+
//Layer 2: the counter (this is the one we will update later)
55+
const likeSpan = document.createElement("span");
56+
likeSpan.id = "likes-count-" + message.id;
57+
likeSpan.textContent = `(${message.likes} Likes) `;
58+
59+
// Layer 3: the button
60+
const likeButton = document.createElement("button");
61+
likeButton.textContent = "Like";
62+
63+
likeButton.addEventListener("click", async () => {
64+
await fetch(`${API_BASE_URL}/messages/${message.id}/like`, {
65+
method: "POST",
5666
});
67+
});
5768

58-
// put it all together
59-
newElement.appendChild(textSpan);
60-
newElement.appendChild(likeSpan);
61-
newElement.appendChild(likeButton);
62-
messageContainer.appendChild(newElement);
69+
// put it all together
70+
newElement.appendChild(textSpan);
71+
newElement.appendChild(likeSpan);
72+
newElement.appendChild(likeButton);
73+
messageContainer.appendChild(newElement);
6374

64-
lastIdSeen = message.id;
65-
}
66-
});
67-
setTimeout(getAllMessages, 0);
68-
} catch (error) {
69-
setTimeout(getAllMessages, 0);
70-
console.error("Error fetching messages:", error);
71-
}
75+
lastIdSeen = message.id;
76+
}
77+
});
7278
}
7379

7480
getAllMessages();

0 commit comments

Comments
 (0)