-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (78 loc) · 2.45 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
const userName = {
name
};
document.addEventListener("keypress", (tecla) => {
if (tecla.key === "Enter") {
const button = document.querySelector(".bottom-bar ion-icon");
button.click();
}
});
function registerUser() {
userName.name = document.querySelector(".nome").value;
if (userName.name !== '') {
const requisicao = axios.post(
"https://mock-api.driven.com.br/api/v6/uol/participants",
userName
);
console.log(requisicao);
requisicao.then(enterChat);
requisicao.catch(registerUser);
}
}
function enterChat() {
setInterval(keepAlive, 5000);
setInterval(getMessages, 3000);
const mensagens = document.querySelector(".messages");
mensagens.classList.remove("hidden");
const topbar = document.querySelector(".top-bar");
topbar.classList.remove("hidden");
const bottombar = document.querySelector(".bottom-bar");
bottombar.classList.remove("hidden");
const home = document.querySelector(".home");
home.classList.add("hidden");
}
function getMessages(resposta) {
let message = {};
const promessa = axios.get(
"https://mock-api.driven.com.br/api/v6/uol/messages"
);
const mensagens = document.querySelector(".messages");
promessa.then((promessa) => {
mensagens.innerHTML = '';
for (let i = 0; i < promessa.data.length; i++) {
message = promessa.data[i];
mensagens.innerHTML +=
`<div class=${message.type}>
<div class="time">
(${message.time})
</div>
<a>${message.from}</a> para <a>${message.to}:</a>
${message.text}`;
}
mensagens.scrollIntoView({
block: "end",
inline: "end",
behavior: "smooth"
});
});
}
function keepAlive() {
axios.post("https://mock-api.driven.com.br/api/v6/uol/status", userName);
}
function sendMessage() {
const elementoTexto = document.querySelector(".bottom-bar input");
console.log("botão funciona");
console.log(elementoTexto.value);
const message = {
from: userName.name,
to: 'Todos',
text: elementoTexto.value,
type: 'message'
};
const resposta = axios.post(
"https://mock-api.driven.com.br/api/v6/uol/messages",
message
);
resposta.then(getMessages);
resposta.catch(window.location.reload);
}