-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (71 loc) · 2.66 KB
/
script.js
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
const roomList = document.getElementById("room-list");
const searchInput = document.getElementById("search-input");
const searchButton = document.getElementById("search-button");
// Function to create HTML elements for room display
function createRoomElement(room) {
const roomContainer = document.createElement("div");
roomContainer.classList.add("room");
roomContainer.classList.add("room-box");
const roomName = document.createElement("h2");
roomName.textContent = room.name;
const roomDescription = document.createElement("p");
roomDescription.textContent = room.description;
const roomAPIUrl = document.createElement("a");
roomAPIUrl.href = "https://chat.justchatorg.repl.co/"+room.name;
roomAPIUrl.textContent = "Join the Room";
roomAPIUrl.classList.add("join-button")
roomContainer.appendChild(roomName);
roomContainer.appendChild(roomDescription);
roomContainer.appendChild(roomAPIUrl);
return roomContainer;
}
// Function to fetch and display rooms from the API
function displayRooms(page, resultsPerPage) {
const url = `https://justct.pythonanywhere.com/get/${page}/${resultsPerPage}`;
fetch(url, { mode: "cors" })
.then(response => response.json())
.then(data => {
roomList.innerHTML = "";
if (data.length === 0) {
const noResults = document.createElement("p");
noResults.textContent = "No rooms found.";
roomList.appendChild(noResults);
} else {
data.forEach(room => {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
});
}
})
.catch(error => {
console.log("Error:", error);
});
}
document.addEventListener("DOMContentLoaded", function() {
displayRooms(1, 10);
});
// Event listener for search button click
searchButton.addEventListener("click", function() {
const searchQuery = searchInput.value.trim();
if (searchQuery !== "") {
const url = `https://justct.pythonanywhere.com/search/${searchQuery}`;
fetch(url, { mode: "cors" })
.then(response => response.json())
.then(data => {
roomList.innerHTML = "";
if (data.length === 0) {
const noResults = document.createElement("p");
noResults.textContent = "No rooms found.";
roomList.appendChild(noResults);
} else {
data.forEach(room => {
const roomElement = createRoomElement(room);
roomList.appendChild(roomElement);
});
}
})
.catch(error => {
console.log("Error:", error);
});
}
});