Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 2 | Chat App#84
Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 2 | Chat App#84JanefrancessC wants to merge 8 commits into
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
-
Code works well on normal circumstances. Well done.
-
Code could use some documentation (comments).
A useful rule of thumb is to ask yourself whether you could still explain or figure out how the code works few months from now. If the answer is uncertain, consider adding a comment to help future readers understand the intent and reasoning behind it.
-
The README has this requirement:
It must also support at least one additional feature.
In the PR description, can you list the extra features you implemented?
| async function dislikeMessage(msgId) { | ||
| try { | ||
| const response = await fetch(`${serverURL}/${msgId}/dislike`, { | ||
| method: "POST", | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| feedbackEl.innerHTML = `<p>${await response.text()}</p>`; | ||
| return; | ||
| } | ||
| const updatedMessage = await response.json(); | ||
|
|
||
| state.messages = state.messages.map((msg) => | ||
| msg.id === updatedMessage.id ? updatedMessage : msg, | ||
| ); | ||
| displayMessages(); | ||
| } catch (error) { | ||
| console.error(error.message); | ||
| } | ||
| } |
There was a problem hiding this comment.
The code in dislikeMessage() and likeMessage() is nearly identical and could be refactored to reduce duplication.
The same also applies to their counterparts on the server side.
There was a problem hiding this comment.
I will also modify this by creating a generic function, then using a conditional statement to increment either the like or dislike button.
Thank you for the thorough feedback. I have edited the PR to include the extra feature and will modify my code per the review. |
cjyuan
left a comment
There was a problem hiding this comment.
Changes look great. I especially like the internal documentation (comments) because they really make reading the code easier.
I only have a few more comments for you to consider. Change is optional.
| * longPoll - If true and there are no new messages, holds the connection open | ||
| * until there are new messages instead of returning empty array. | ||
| */ | ||
| app.get("/", (req, res) => { |
There was a problem hiding this comment.
I missed this in my previous review. Could you use an AI to find out
Why
/messageswould make a better URI than/for a service that get and add messages?
| app.post("/:id/:reaction", (req, res) => { | ||
| const id = Number(req.params.id); | ||
| const reaction = req.params.reaction; | ||
|
|
||
| const message = messages.find((msg) => msg.id === id); | ||
| if (!message) { | ||
| res.status(404).json({ error: "Message not found" }); | ||
| return; | ||
| } | ||
|
|
||
| if (reaction === "like") { | ||
| message.likes++; | ||
| } else if (reaction === "dislike") { | ||
| message.dislikes++; | ||
| } else { | ||
| res.status(400).json({ error: "Invalid reaction!" }); | ||
| return; | ||
| } | ||
| res.json(message); | ||
| }); |
There was a problem hiding this comment.
Could consider rearrange the code this way to prevent unnecessary operations from being performed when input is invalid:
app.post("/:id/:reaction", (req, res) => {
const id = Number(req.params.id);
const reaction = req.params.reaction;
// Validate id and reaction first
// if reaction is invalid
if (...) {
res.status(400).json({ error: "Invalid reaction!" });
return;
}
const message = messages.find((msg) => msg.id === id);
if (!message) {
res.status(404).json({ error: "Message not found" });
return;
}
if (reaction === "like") {
message.likes++;
} else if (reaction === "dislike") {
message.dislikes++;
}
res.json(message);
});
| const p = document.createElement("p"); | ||
|
|
||
| const strong = document.createElement("strong"); | ||
| strong.textContent = `${msg.user}: ${msg.message} `; |
There was a problem hiding this comment.
Note: An alternative is to implement a helper function
function escapeHtml(str) {
const div = document.createElement("div");
div.textContent = str;
return div.innerHTML;
}
to escape special characters and use your original approach to construct the desired element.
Learners, PR Template
Self checklist
Changelist
Backend link
Frontend link
Additional Feature