Skip to content

Commit 1518e8b

Browse files
committed
feat: Implement like and dislike buttons logic
1 parent 90e2c3b commit 1518e8b

4 files changed

Lines changed: 99 additions & 14 deletions

File tree

chat-app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
.DS_Store

chat-app/README.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ https://sdc.codeyourfuture.io/decomposition/sprints/2/prep/
77
You must complete and deploy a chat application. You have two weeks to complete this.
88

99
It must support at least the following requirements:
10-
* As a user, I can send add a message to the chat.
11-
* As a user, when I open the chat I see the messages that have been sent by any user.
12-
* As a user, when someone sends a message, it gets added to what I see.
10+
11+
- As a user, I can send add a message to the chat.
12+
- As a user, when I open the chat I see the messages that have been sent by any user.
13+
- As a user, when someone sends a message, it gets added to what I see.
1314

1415
It must also support at least one additional feature.
1516

@@ -27,8 +28,8 @@ Exploring and understanding different ways of sending information between a clie
2728

2829
### How to submit
2930

30-
* Fork the Module-Decomposition repository
31-
* Develop and deploy your chat app
32-
* Create a pull request back into the original Module-Decomposition repo, including:
33-
* A link to the deployed frontend on the CYF hosting environment
34-
* A link to the deployed backend on the CYF hosting environment
31+
- Fork the Module-Decomposition repository
32+
- Develop and deploy your chat app
33+
- Create a pull request back into the original Module-Decomposition repo, including:
34+
- A link to the deployed frontend on the CYF hosting environment
35+
- A link to the deployed backend on the CYF hosting environment

chat-app/backend/index.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,31 @@ app.use(cors());
66
app.use(express.json());
77

88
const port = 3000;
9+
let nextMessageId = 1;
910
const messages = [
1011
{
12+
id: nextMessageId++,
1113
message: "Hello",
1214
user: "Jane",
1315
time: Date.now() - 6000,
16+
likes: 1,
17+
dislikes: 2,
1418
},
1519
{
20+
id: nextMessageId++,
1621
message: "Hey",
1722
user: "John",
1823
time: Date.now() - 3000,
24+
likes: 1,
25+
dislikes: 2,
1926
},
2027
{
28+
id: nextMessageId++,
2129
message: "Hi",
2230
user: "Bob",
2331
time: Date.now(),
32+
likes: 1,
33+
dislikes: 2,
2434
},
2535
];
2636
const callbacksForNewMessages = [];
@@ -57,18 +67,47 @@ app.post("/messages", (req, res) => {
5767
return;
5868
}
5969

60-
messages.push({
70+
const newMessage = {
71+
id: nextMessageId++,
6172
message,
6273
user,
6374
time: Date.now(),
64-
});
75+
likes: 0,
76+
dislikes: 0,
77+
};
78+
79+
messages.push(newMessage);
6580
while (callbacksForNewMessages.length > 0) {
6681
const callback = callbacksForNewMessages.pop();
6782
callback([messages[messages.length - 1]]);
6883
}
6984
res.status(201).json({ success: true });
7085
});
7186

87+
app.post("/messages/:id/like", (req, res) => {
88+
const id = Number(req.params.id);
89+
const message = messages.find((msg) => msg.id === id);
90+
91+
if (!message) {
92+
res.status(404).json({ error: "Message not found" });
93+
return;
94+
}
95+
message.likes++;
96+
res.json(message);
97+
});
98+
99+
app.post("/messages/:id/dislike", (req, res) => {
100+
const id = Number(req.params.id);
101+
const message = messages.find((msg) => msg.id === id);
102+
103+
if (!message) {
104+
res.status(404).json({ error: "Message not found" });
105+
return;
106+
}
107+
message.dislikes++;
108+
res.json(message);
109+
});
110+
72111
app.listen(port, () => {
73112
console.log(`Chat server listening on port ${port}`);
74113
});

chat-app/frontend/script.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ let messageEl = document.getElementById("message");
55
let displayBox = document.getElementById("display-message");
66
let feedbackEl = document.getElementById("feedback");
77

8-
const serverURL = `https://janefrancessc-chat-application-backend.hosting.codeyourfuture.io/messages`;
9-
// const serverURL = "http://127.0.0.1:3000/messages";
8+
// const serverURL = `https://janefrancessc-chat-application-backend.hosting.codeyourfuture.io/messages`;
9+
const serverURL = "http://127.0.0.1:3000/messages";
1010
const state = { messages: [] };
1111
let pollingMode = "regular";
1212
let longPoll = false;
@@ -19,7 +19,7 @@ pollingForm.addEventListener("change", (e) => {
1919
longPoll = pollingMode === "long";
2020

2121
feedbackEl.innerHTML = `
22-
<p>${longPoll ? "Using long polling" : "Using regular polling"}</p>
22+
<p>${longPoll ? "Using long polling!" : "Using regular polling!"}</p>
2323
`;
2424
setTimeout(() => {
2525
feedbackEl.innerHTML = "";
@@ -60,7 +60,49 @@ async function keepFetchingMessages() {
6060
if (longPoll) {
6161
keepFetchingMessages();
6262
} else {
63-
setTimeout(keepFetchingMessages, 100);
63+
setTimeout(keepFetchingMessages, 2000);
64+
}
65+
}
66+
67+
async function likeMessage(msgId) {
68+
try {
69+
const response = await fetch(`${serverURL}/${msgId}/like`, {
70+
method: "POST",
71+
});
72+
73+
if (!response.ok) {
74+
feedbackEl.innerHTML = `<p>${await response.text()}</p>`;
75+
return;
76+
}
77+
const updatedMessage = await response.json();
78+
79+
state.messages = state.messages.map((msg) =>
80+
msg.id === updatedMessage.id ? updatedMessage : msg,
81+
);
82+
displayMessages();
83+
} catch (error) {
84+
console.error(error.message);
85+
}
86+
}
87+
88+
async function dislikeMessage(msgId) {
89+
try {
90+
const response = await fetch(`${serverURL}/${msgId}/dislike`, {
91+
method: "POST",
92+
});
93+
94+
if (!response.ok) {
95+
feedbackEl.innerHTML = `<p>${await response.text()}</p>`;
96+
return;
97+
}
98+
const updatedMessage = await response.json();
99+
100+
state.messages = state.messages.map((msg) =>
101+
msg.id === updatedMessage.id ? updatedMessage : msg,
102+
);
103+
displayMessages();
104+
} catch (error) {
105+
console.error(error.message);
64106
}
65107
}
66108

@@ -73,6 +115,8 @@ function displayMessages() {
73115
<strong>${msg.user}: ${msg.message} </strong>
74116
<small>${new Date(msg.time).toLocaleString()}</small>
75117
</p>
118+
<button onclick="likeMessage(${msg.id})">${msg.likes} 👍 </button>
119+
<button onclick="dislikeMessage(${msg.id})">${msg.dislikes} 👎 </button>
76120
</div>
77121
`,
78122
)

0 commit comments

Comments
 (0)