-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathscript.js
More file actions
186 lines (157 loc) · 6.17 KB
/
script.js
File metadata and controls
186 lines (157 loc) · 6.17 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
// DOM selectors (variables that point to selected DOM elements) goes here 👇
const chat = document.getElementById('chat')
// Functions goes here 👇
// A function that will add a chat bubble in the correct place based on who the sender is
const showMessage = (message, sender) => {
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">
<p>${message}</p>
</div>
</section>
`;
}
// Scroll to the last message when too many messages are in the chat box
chat.scrollTop = chat.scrollHeight;
};
// Function to start the conversation
const greetUser = () => {
showMessage("Hello there, what's your name?", 'bot');
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<form id="name-form">
<input id="name-input" type="text" placeholder="Enter your name" required />
<button class="send-btn" type="submit">Submit</button>
</form>
`;
document.getElementById('name-form').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
const userName = document.getElementById('name-input').value;
if (userName) {
showMessage(userName, 'user');
setTimeout(() => {
showMessage(`Nice to meet you, ${userName}! I'm a travel bot, let's plan a nice trip. Will you be traveling solo, with friends, or as a couple?`, 'bot');
askForTripType(); // Ask for the trip type next
}, 1000);
}
});
};
// Function to ask for trip type (Solo, friends or couple)
const askForTripType = () => {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<form id="trip-form">
<label for="trip-type">Choose your trip type:</label>
<select id="trip-type" required>
<option value="">Select...</option>
<option value="Solo">Solo</option>
<option value="Friends">Friends</option>
<option value="Couple">Couple</option>
</select>
<button class="send-btn" type="submit">Submit</button>
</form>
`;
document.getElementById('trip-form').addEventListener('submit', function(event) {
event.preventDefault();
const tripType = document.getElementById('trip-type').value;
if (tripType === "Solo") {
showMessage("Solo", 'user');
suggestSoloDestinations(); // Suggest solo destinations
} else if (tripType === "Friends") {
showMessage("With Friends", 'user');
suggestFriendsDestinations(); // Suggest friends destinations
} else if (tripType === "Couple") {
showMessage("As a Couple", 'user');
suggestCoupleDestinations(); // Suggest couple destinations
} else {
showMessage("Please select a valid option.", 'bot');
}
});
};
// Function to suggest solo destinations and let the user choose
const suggestSoloDestinations = () => {
showMessage("How about a trip to Japan or New Zealand?", 'bot');
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<button id="japan-btn">Japan</button>
<button id="nz-btn">New Zealand</button>
`;
// Event listeners for the destination choice
document.getElementById('japan-btn').addEventListener('click', () => {
confirmChoice("Japan");
});
document.getElementById('nz-btn').addEventListener('click', () => {
confirmChoice("New Zealand");
});
};
// Function to suggest destinations for friends and let the user choose
const suggestFriendsDestinations = () => {
showMessage("How about a fun trip to Thailand or Spain?", 'bot');
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<button id="thailand-btn">Thailand</button>
<button id="spain-btn">Spain</button>
`;
// Event listeners for the destination choice
document.getElementById('thailand-btn').addEventListener('click', () => {
confirmChoice("Thailand");
});
document.getElementById('spain-btn').addEventListener('click', () => {
confirmChoice("Spain");
});
};
// Function to suggest destinations for couples and let the user choose
const suggestCoupleDestinations = () => {
showMessage("How about a romantic trip to Italy or Greece?", 'bot');
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<button id="italy-btn">Italy</button>
<button id="greece-btn">Greece</button>
`;
// Event listeners for the destination choice
document.getElementById('italy-btn').addEventListener('click', () => {
confirmChoice("Italy");
});
document.getElementById('greece-btn').addEventListener('click', () => {
confirmChoice("Greece");
});
};
// Function to confirm the user's choice and provide a validation step
const confirmChoice = (destination) => {
showMessage(`You selected ${destination}. Do you want to confirm this choice?`, 'bot');
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.innerHTML = `
<button id="confirm-btn">Confirm</button>
<button id="change-btn">Change destination</button>
`;
document.getElementById('confirm-btn').addEventListener('click', () => {
showMessage(`Confirmed! You will travel to ${destination}.`, 'user');
setTimeout(() => endConversation(), 1000);
});
document.getElementById('change-btn').addEventListener('click', () => {
if (destination === "Japan" || destination === "New Zealand") {
suggestSoloDestinations();
} else if (destination === "Thailand" || destination === "Spain") {
suggestFriendsDestinations();
} else if (destination === "Italy" || destination === "Greece") {
suggestCoupleDestinations();
}
});
};
// Function to end the conversation
const endConversation = () => {
showMessage("Awesome choice! I'll send the tickets to your mail right away. Thank you for trusting me!", 'bot');
};
// Start the conversation with a delay
setTimeout(greetUser, 1000);