-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
109 lines (98 loc) · 2.58 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import bot from "./assets/bot.svg";
import user from "./assets/user.svg";
const hamburger = document.querySelector(".hamburger");
const form = document.querySelector("form");
const chatContainer = document.querySelector("#chat_container");
let loadInterval;
hamburger.onclick = function () {
let navbar = document.querySelector(".navbar");
navbar.classList.toggle("active");
};
function loader(elem) {
elem.textContent = "";
loadInterval = setInterval(() => {
elem.textContent += ".";
if (elem.textContent === "....") {
elem.textContent = "";
}
}, 300);
}
function typeText(elem, text) {
let index = 0;
let interval = setInterval(() => {
if (index < text.length) {
elem.innerHTML += text.charAt(index);
index++;
} else {
clearInterval(interval);
}
}, 20);
}
function generateId() {
const id = Date.now();
const random = Math.random();
return `id-${id}-${random}`;
}
function chat(isAi, value, id) {
return `
<div class="${isAi ? "wrapper ai" : "wrapper"}">
<div class="${isAi ? "chatAi" : "chat"}">
<div class="profile">
<img src="${isAi ? bot : user}" alt="icon"/>
</div>
<div class="message" id=${id}>
${value}
</div>
</div>
</div>
`;
}
const handleSubmit = async (e) => {
e.preventDefault();
const data = new FormData(form);
if (!data.get("prompt").trim()) {
alert("Please write something");
} else {
//user chat
chatContainer.innerHTML += chat(
false,
data.get("prompt").trim(),
Date.now()
);
form.reset();
//bot chat
const newId = generateId();
chatContainer.innerHTML += chat(true, " ", newId);
// to focus scroll to the bottom
chatContainer.scrollTop = chatContainer.scrollHeight;
const messageDiv = document.getElementById(newId);
loader(messageDiv);
// fetch data
const response = await fetch("https://vast-plum-codfish-gear.cyclic.app", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
prompt: data.get("prompt"),
}),
});
clearInterval(loadInterval);
messageDiv.innerHTML = "";
if (response.ok) {
const data = await response.json();
const parsedData = data.bot.trim();
typeText(messageDiv, parsedData);
} else {
const err = await response.text();
messageDiv.innerHTML = "Something went wrong";
alert(err);
}
}
};
form.addEventListener("submit", handleSubmit);
form.addEventListener("keyup", (e) => {
if (e.keyCode === 13) {
handleSubmit(e);
}
});