-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (49 loc) · 1.69 KB
/
index.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
const chatList = document.querySelector(".chat-list");
const addChatForm = document.querySelector(".new-chat");
const updateUsernameForm = document.querySelector(".new-name");
const updateMessage = document.querySelector(".update-message");
const chatRooms = document.querySelector(".chat-rooms");
const chatUi = new ChatUI(chatList);
const defaultRoom = "general";
const defaultUsername = "Anonymous";
const usernameFromLocalStorage = localStorage.username;
const username = usernameFromLocalStorage ? usernameFromLocalStorage : defaultUsername;
const chatroom = new Chatroom(defaultRoom, username);
addChatForm.addEventListener("submit", (event) => {
event.preventDefault();
const message = addChatForm.message.value.trim();
chatroom.addChat(message)
.then(() => {
addChatForm.reset();
})
.catch((error) => {
console.log("Error is: ", error);
});
});
updateUsernameForm.addEventListener("submit", (event) => {
event.preventDefault();
const username = updateUsernameForm.name.value.trim();
updateUsernameForm.reset();
chatroom.updateUsername(username);
updateMessage.innerHTML = `
Your name has been updated to
<span class="text-info fs-4 text-capitalize">${username}</span> !!
`;
setTimeout(() => {
updateMessage.innerText = '';
}, 3000);
});
chatRooms.addEventListener('click', (event) => {
if (event.target.tagName === "BUTTON") {
chatUi.clear();
const room = event.target.getAttribute('id');
chatroom.updateRoom(room);
loadChats();
}
});
const loadChats = () => {
chatroom.getChats((data) => {
chatUi.render(data);
});
}
loadChats();