Skip to content

Commit b91b76c

Browse files
committed
Refactor API URL into a constant
1 parent 64062c8 commit b91b76c

1 file changed

Lines changed: 11 additions & 17 deletions

File tree

‎chat-app/frontend/app.js‎

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const sendButton = document.getElementById("send-button");
55

66
let lastMessageId = -1;
77
let clientId = localStorage.getItem("clientId");
8+
const API_URL = "https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy";
89

910
if (!clientId) {
1011
clientId = crypto.randomUUID();
@@ -82,9 +83,7 @@ function createMessageElement(message) {
8283

8384
// Get messages from server
8485
async function getMessages() {
85-
const response = await fetch(
86-
`https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages?since=${lastMessageId}`,
87-
);
86+
const response = await fetch(`${API_URL}/messages?since=${lastMessageId}`);
8887

8988
const messages = await response.json();
9089

@@ -106,18 +105,15 @@ async function getMessages() {
106105

107106
// React to message
108107
async function reactToMessage(messageId, reaction) {
109-
await fetch(
110-
`https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages/${messageId}/reaction`,
111-
{
112-
method: "POST",
113-
headers: {
114-
"Content-Type": "application/json",
115-
},
116-
body: JSON.stringify({
117-
reaction: reaction,
118-
}),
108+
await fetch(`${API_URL}/messages/${messageId}/reaction`, {
109+
method: "POST",
110+
headers: {
111+
"Content-Type": "application/json",
119112
},
120-
);
113+
body: JSON.stringify({
114+
reaction: reaction,
115+
}),
116+
});
121117
}
122118

123119
// Update reaction: like / dislike
@@ -143,9 +139,7 @@ sendButton.addEventListener("click", async () => {
143139
return;
144140
}
145141

146-
await fetch(
147-
"https://x2fkdg4qtvw2zk6tfpgmud7g.trainees.hosting.cyf.academy/messages",
148-
142+
await fetch(`${API_URL}/messages`,
149143
{
150144
method: "POST",
151145
headers: {

0 commit comments

Comments
 (0)