forked from CrookshanksAcademy/CrookshanksAcademy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
50 lines (43 loc) · 1.32 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
49
50
const chatBody = document.querySelector(".chat-body");
const txtInput = document.querySelector("#txtInput");
const send = document.querySelector(".send");
send.addEventListener("click", () => renderUserMessage());
txtInput.addEventListener("keyup", (event) => {
if (event.keyCode === 13) {
renderUserMessage();
}
});
const renderUserMessage = () => {
const userInput = txtInput.value;
renderMessageEle(userInput, "user");
txtInput.value = "";
setTimeout(() => {
renderChatbotResponse(userInput);
setScrollPosition();
}, 600);
};
const renderChatbotResponse = (userInput) => {
const res = getChatbotResponse(userInput);
renderMessageEle(res);
};
const renderMessageEle = (txt, type) => {
let className = "user-message";
if (type !== "user") {
className = "chatbot-message";
}
const messageEle = document.createElement("div");
const txtNode = document.createTextNode(txt);
messageEle.classList.add(className);
messageEle.append(txtNode);
chatBody.append(messageEle);
};
const getChatbotResponse = (userInput) => {
return responseObj[userInput] == undefined
? "Please try something else"
: responseObj[userInput];
};
const setScrollPosition = () => {
if (chatBody.scrollHeight > 0) {
chatBody.scrollTop = chatBody.scrollHeight;
}
};