-
Notifications
You must be signed in to change notification settings - Fork 411
/
Copy pathscript.js
148 lines (128 loc) · 4.39 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
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
148
// DOM selectors 👇
const chat = document.getElementById("chat");
const userInput = document.getElementById("name-input");
const form = document.getElementById("form");
// Function that will add a chat bubble in the correct place based on who the sender is
const showMessage = (message, sender, isLoading = false) => {
const messageClass = isLoading ? "loading-message" : "";
if (sender === "user") {
chat.innerHTML += `
<section class="user-msg">
<div class="bubble user-bubble">
<p>${message}</p>
</div>
<img src="assets/user.png" alt="User" />
</section>
`;
} else if (sender === "bot") {
chat.innerHTML += `
<section class="bot-msg">
<img src="assets/bot.png" alt="Bot" />
<div class="bubble bot-bubble ${messageClass}">
<p>${message}</p>
</div>
</section>
`;
}
chat.scrollTop = chat.scrollHeight;
};
// Function to start the conversation
const greetUser = () => {
showMessage("Hey you, what's your name?", "bot");
};
let numberOfFormSubmits = 0; // initializing the logic to trigger new messages in the form
// Function to handle the form submission
const handleSubmit = (event) => {
event.preventDefault();
numberOfFormSubmits++;
switch (numberOfFormSubmits) {
case 1: // Name input
const name = userInput.value;
showMessage(name, "user");
showMessage(`Nice to meet you, ${name}`, "bot");
userInput.value = "";
setTimeout(() => {
showMessage(
"What would you like to eat? Pizza, Burger, or Salad?",
"bot"
);
updateFormForFoodSelection();
}, 1000);
break;
case 2: // Food selection
const selectedFood = document.getElementById("food").value;
showMessage(selectedFood, "user");
showMessage(`${selectedFood} coming up!`, "bot");
setTimeout(() => {
showMessage("What would you like to drink?", "bot");
updateFormForDrinkSelection();
}, 1000);
break;
case 3: // Drink selection and order confirmation
const selectedDrink = document.getElementById("drink").value;
showMessage(selectedDrink, "user");
showMessage(`${selectedDrink} is a great choice!`, "bot");
setTimeout(() => {
showMessage("Are you sure you want to order?", "bot");
updateFormForOrderConfirmation(); // This function also triggers confirmation message
}, 1000);
break;
// add more questions here
}
}
const updateFormForFoodSelection = () => {
form.innerHTML = `
<label for="food">Select your food:</label>
<select id="food" name="food" required>
<option value="" disabled selected>Choose your food</option>
<option value="Salad">Salad</option>
<option value="Burger">Burger</option>
<option value="Pasta">Pasta</option>
</select>
<button id="btn" class="send-btn" type="submit">Send</button>
`;
}
const updateFormForDrinkSelection = () => {
form.innerHTML = `
<label for="drink">Select your drink:</label>
<select id="drink" name="drink" required>
<option value="" disabled selected>Choose your drink</option>
<option value="Coca Cola">Coca Cola</option>
<option value="Water">Water</option>
<option value="Wine">Wine</option>
</select>
<button id="btn" class="send-btn" type="submit">Send</button>
`;
}
const updateFormForOrderConfirmation = () => {
form.innerHTML = `
<button type="button" id="confirmation" onclick="orderConfirmation('Yes')">Yes</button>
<button type="button" id="confirmation" onclick="orderConfirmation('No')">No</button>
`;
}
const orderConfirmation = (confirmation) => {
showMessage(confirmation, "user");
if (confirmation === "Yes") {
showMessage("Preparing your order", "bot", true);
setTimeout(() => {
const loadingMessage = document.querySelector(".loading-message");
if (loadingMessage) {
loadingMessage.classList.remove("loading-message");
loadingMessage.querySelector("p").textContent =
"Thanks for ordering! The food will be ready soon 🍽️";
}
clearForm();
}, 3000);
} else {
setTimeout(() => {
showMessage("Please come back another time!", "bot");
clearForm();
}, 1000);
}
}
const clearForm = () => {
form.innerHTML = "";
}
// Eventlisteners👇
form.addEventListener("submit", (event) => handleSubmit(event));
setTimeout(greetUser, 1000);