-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
147 lines (113 loc) · 3.6 KB
/
Copy pathapp.js
File metadata and controls
147 lines (113 loc) · 3.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const bubble = document.getElementById("chatBubble");
const container = document.getElementById("chatContainer");
const minimizeBtn = document.getElementById("minimizeBtn");
let isOpen = false;
let welcomeRendered = false;
function toggleChat() {
isOpen = !isOpen;
container.classList.toggle("open", isOpen);
bubble.classList.toggle("open", isOpen);
if (isOpen && !welcomeRendered) {
renderWelcome();
welcomeRendered = true;
}
if (isOpen) setTimeout(() => txtInput.focus(), 260);
}
bubble.addEventListener("click", toggleChat);
minimizeBtn.addEventListener("click", toggleChat);
const chatBody = document.querySelector(".chat-body");
const txtInput = document.querySelector("#txtInput");
const send = document.querySelector(".send");
let isEmailMode = false;
send.addEventListener("click", () => {
if (!isEmailMode) handleUserMessage();
});
txtInput.addEventListener("keyup", (e) => {
if (e.key === "Enter" && !isEmailMode) {
handleUserMessage();
}
});
function handleUserMessage() {
const text = txtInput.value.trim();
if (!text) return;
addMessage(text, "user");
txtInput.value = "";
setTimeout(() => {
const res = responseObj[text.toLowerCase()];
if (typeof res === "function") {
res();
} else {
addMessage(res || "Please try something else");
}
scrollToBottom();
}, 500);
}
function addMessage(text, type = "bot") {
const div = document.createElement("div");
div.className = type === "user" ? "user-message" : "chatbot-message";
div.textContent = text;
chatBody.append(div);
}
function scrollToBottom() {
chatBody.scrollTop = chatBody.scrollHeight;
}
function renderWelcome() {
const wrapper = document.createElement("div");
wrapper.className = "chatbot-message welcome-wrapper";
wrapper.append("Hi! How can I help you today?");
const label = document.createElement("span");
label.className = "recommended-label";
label.textContent = "Recommended";
const btn = document.createElement("button");
btn.className = "option-btn";
btn.textContent = "Receive notifications";
btn.onclick = () => {
btn.remove();
label.remove();
startEmailFlow();
};
wrapper.append(label);
wrapper.append(btn);
chatBody.append(wrapper);
}
function startEmailFlow() {
isEmailMode = true;
addMessage("Please enter your email:");
txtInput.placeholder = "your@email.com";
txtInput.focus();
const submitEmail = async () => {
const email = txtInput.value.trim();
if (!email.includes("@") || !email.includes(".")) {
addMessage("Invalid email. Try again.");
return;
}
addMessage(email, "user");
txtInput.value = "";
addMessage("Sending... ⏳");
try {
await emailjs.send(
"YOUR_SERVICE_ID", // EMAILJS SERVICE ID
"YOUR_TEMPLATE_ID", // EMAILJS TEMPLATE ID
{
user_email: email,
user: "test"
}
);
chatBody.lastChild.remove();
addMessage("Email sent");
} catch (error) {
chatBody.lastChild.remove();
addMessage("Failed to send");
console.error(error);
}
isEmailMode = false;
send.removeEventListener("click", sendHandler);
txtInput.removeEventListener("keyup", enterHandler);
};
const sendHandler = () => submitEmail();
const enterHandler = (e) => {
if (e.key === "Enter") submitEmail();
};
send.addEventListener("click", sendHandler);
txtInput.addEventListener("keyup", enterHandler);
}