-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (41 loc) · 1.43 KB
/
app.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
// dom queries
const chatList = document.querySelector('.chat-list');
const newChatForm = document.querySelector('.new-chat');
const newNameForm = document.querySelector('.new-name');
const updateMsg = document.querySelector('.update-msg');
const rooms = document.querySelector('.chat-rooms');
// class instances
const chatUI = new ChatUI(chatList);
// add a new chat
newChatForm.addEventListener('submit', e => {
e.preventDefault();
const message = newChatForm.message.value.trim();
chatroom.addChat(message)
.then(() => newChatForm.reset())
.catch(err => console.log(err));
});
// update the username
newNameForm.addEventListener('submit', e => {
e.preventDefault();
// update name via chatroom
const newName = newNameForm.name.value.trim();
chatroom.updateName(newName);
// reset the form
newNameForm.reset();
// show then hide the update message
updateMsg.innerText = `Your name was updated to ${newName}`;
setTimeout(() => updateMsg.innerText = '', 3000);
});
// update the chat room
rooms.addEventListener('click', e => {
if(e.target.tagName === 'BUTTON'){
chatUI.clear();
chatroom.updateRoom(e.target.getAttribute('id'));
chatroom.getChats(chat => chatUI.render(chat));
}
});
// check local storage for name
const username = localStorage.username ? localStorage.username : 'anon';
const chatroom = new Chatroom('gaming', username);
// get chats & render
chatroom.getChats(data => chatUI.render(data));