-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (168 loc) · 7.13 KB
/
Copy pathscript.js
File metadata and controls
203 lines (168 loc) · 7.13 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//Global LET variables that will be used in the confirmation
let userName = ''
let selectedCoffee = ''
let sizeChoice = ''
let coffeeShop = ''
// DOM selectors (variables that point to selected DOM elements) goes here 👇
const chat = document.getElementById('chat')
const startButton = document.getElementById('start-button')
const inputWrapper = document.getElementById('input-wrapper')
const userInput = document.getElementById("user-input")
const nameForm = document.getElementById('name-form')
// 10. Handle summary
const showSummary = () => {
showMessage(`Thank you, ${userName}, for your order of ${coffeeChoice} (${sizeChoice} size), which you will pick up at Coffee JoE in ${coffeeShop}.`, 'bot')
//Remove input field after Confirmation
document.getElementById('name-form').remove()
}
// 9. Handle Coffeeshop input
const userAnswer = (selectCoffeeShop) => {
coffeeShop = selectCoffeeShop
showMessage(` Coffee JoE in ${coffeeShop}.`, 'user')
showMessage(`You have selected ${coffeeShop}.`, 'bot')
//Remove Coffeeshop buttons
document.getElementById("stockholm").remove()
document.getElementById("gothenburg").remove()
document.getElementById("kiruna").remove()
document.getElementById("falun").remove()
setTimeout(showSummary, 3000) // Move on to Summary
}
// 8. Select Coffee Shop
const handleCoffeeShopInput = () => {
inputWrapper.classList.add("hidden")
showMessage("Where would you like to pick up your coffee?", 'bot')
chat.innerHTML += `
<section class="bot-msg">
<button id="stockholm" class="selection-button">Stockholm</button>
<button id="gothenburg" class="selection-button">Gothenburg</button>
<button id="kiruna" class="selection-button">Kiruna</button>
<button id="falun" class="selection-button">Falun</button>
</section>
`
document.getElementById('stockholm').addEventListener("click", () => userAnswer('Stockholm'))
document.getElementById('gothenburg').addEventListener("click", () => userAnswer('Gothenburg'))
document.getElementById('kiruna').addEventListener("click", () => userAnswer('Kiruna'))
document.getElementById('falun').addEventListener("click", () => userAnswer('Falun'))
}
// 7. Handle coffee size input
const handleSizeChoice = (size) => {
sizeChoice = size
showMessage(`${size} size!`, 'user')
document.getElementById("regularButton").remove()
document.getElementById("smallButton").remove()
showMessage("Great choice! Your coffee is almost on the way.", 'bot')
setTimeout(handleCoffeeShopInput, 3000) // Move on to Coffee shop question
}
// 6. Coffee size
const askForSize = () => {
inputWrapper.classList.add("hidden")
showMessage("What size would you like?", 'bot')
// Add two Size buttons
chat.innerHTML += `
<section class="bot-msg">
<button id="regularButton" class="selection-button">Regular</button>
<button id="smallButton" class="selection-button">Small</button>
</section>
`
document.getElementById("regularButton").addEventListener("click", () => handleSizeChoice("Regular"))
document.getElementById("smallButton").addEventListener("click", () => handleSizeChoice("Small"))
}
// 5. Handle Coffee choice input
const handleCoffeeInput = (selectedCoffee) => {
coffeeChoice = selectedCoffee // Update the global variable
if (coffeeChoice === "Pumpkin Spice Latte") {
showMessage(`${coffeeChoice}, please.`, 'user')
showMessage("Ah, Pumpkin Spice Latte, perfect choice!", 'bot')
} else if (coffeeChoice === "Espresso") {
showMessage(`${coffeeChoice}, please.`, 'user')
showMessage("An Espresso coming up! Excellent choice.", 'bot')
} else if (coffeeChoice === "Cappuccino") {
showMessage(`${coffeeChoice}, please.`, 'user')
showMessage("Creamy Cappuccino it is!", 'bot')
} else if (coffeeChoice === "Americano") {
showMessage(`${coffeeChoice}, please.`, 'user')
showMessage("A classic Americano for you!", 'bot')
} else {
showMessage(`${coffeeChoice}, please.`, 'user')
showMessage(`${coffeeChoice} sounds great, but we don't have that option right now!`, 'bot')
return
}
document.getElementById("pumpkin").remove()
document.getElementById("cappuccino").remove()
document.getElementById("americano").remove()
document.getElementById("espresso").remove()
setTimeout(askForSize, 2000) // Move on to Ask for size
}
// 4. Select Coffee
const selectCoffee = () => {
inputWrapper.classList.add("hidden")
showMessage(`Nice to meet you, ${userName}.`, 'bot')
setTimeout(() => {
showMessage(`Please start your order by selecting your coffee. Here are your options:`, 'bot')
chat.innerHTML += `
<section class="bot-msg">
<button id="pumpkin" class="selection-button">Pumpkin Spice Latte</button>
<button id="cappuccino" class="selection-button">Cappuccino</button>
<button id="americano" class="selection-button">Americano</button>
<button id="espresso" class="selection-button">Espresso</button>
</section>
`
document.getElementById("pumpkin").addEventListener("click", () => handleCoffeeInput("Pumpkin Spice Latte"))
document.getElementById("cappuccino").addEventListener("click", () => handleCoffeeInput("Cappuccino"))
document.getElementById("americano").addEventListener("click", () => handleCoffeeInput("Americano"))
document.getElementById("espresso").addEventListener("click", () => handleCoffeeInput("Espresso"))
nameForm.removeEventListener("submit", handleUserInput) // Remove Event listener for Name
}, 1500) //Move on to Handle Coffee choice input
}
// 3. Handle user name input
const handleUserInput = (event) => {
event.preventDefault()
userName = userInput.value
showMessage(userName, "user")
userInput.value = "" // Clear input field
document.getElementById('input-wrapper').remove()
setTimeout(selectCoffee, 1000) // Move on to Select coffee
}
// Funtion to show messages from bot and user
const showMessage = (message, sender) => {
// Posted message from the user
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>
`
// Posted message from the bot
} else if (sender === 'bot') {
chat.innerHTML += `
<section class="bot-msg">
<img src="assets/bot.png" alt="Bot" />
<div class="bubble bot-bubble">
<p>${message}</p>
</div>
</section>
`
}
// Scroll to the last message when chat is too long.
// requestAnimationFrame to avoid scrolling to see selection-buttons in the chat
requestAnimationFrame(() => {
chat.scrollTop = chat.scrollHeight
})
}
startButton.addEventListener("click", () => {
startChat()
document.getElementById('name-form').addEventListener("submit", handleUserInput)
})
// 2. Greet user
const greetUser = () => {
showMessage("Hello fellow Java Junkie! What's your name?", 'bot')
}
//1. Click Start Conversation button
const startChat = () => {
greetUser()
startButton.classList.add("hidden") // Hide Start conversation button when clicked
document.getElementById('name-form').classList.add('show') // Show name input field
}