Update static/script.js

#2
by ajaybolloju - opened
Files changed (1) hide show
  1. static/script.js +190 -74
static/script.js CHANGED
@@ -1,7 +1,8 @@
1
  let conversation = [
2
- { role: 'bot', message: "Hi there! I'm Chat Bot! May I know your name?" }
3
  ];
4
  let userName = ''; // To store the user's name
 
5
 
6
  function addMessage(role, message) {
7
  const chatMessages = document.getElementById('chatMessages');
@@ -34,6 +35,32 @@ function sendMessage() {
34
  }
35
  }
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  function displayForm() {
38
  const chatMessages = document.getElementById('chatMessages');
39
  if (!chatMessages) {
@@ -56,20 +83,69 @@ function displayForm() {
56
  <input type="email" id="email" placeholder="Enter your email" /><br/>
57
  <label for="issueDescription">Issue Description:</label>
58
  <textarea id="issueDescription" placeholder="Describe your issue"></textarea><br/>
59
- <button onclick="submitForm()">Submit</button>
60
  </div>
61
  `;
62
  chatMessages.appendChild(formDiv);
63
  chatMessages.scrollTop = chatMessages.scrollHeight;
64
  }
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  function submitForm() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  const mobileNumber = document.getElementById('mobileNumber').value.trim();
68
  const email = document.getElementById('email').value.trim();
69
  const issueDescription = document.getElementById('issueDescription').value.trim();
70
 
71
- if (!mobileNumber || !email || !issueDescription) {
72
- addMessage('bot', 'Please fill in all fields before submitting.');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  return;
74
  }
75
 
@@ -80,6 +156,8 @@ function submitForm() {
80
  issueDescription: issueDescription
81
  };
82
 
 
 
83
  fetch('/submit-case', {
84
  method: 'POST',
85
  headers: {
@@ -87,17 +165,29 @@ function submitForm() {
87
  },
88
  body: JSON.stringify(formData),
89
  })
90
- .then(response => response.json())
 
 
 
91
  .then(data => {
 
92
  if (data.success) {
93
  addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
 
 
 
 
94
  } else {
95
  addMessage('bot', 'There was an error submitting your case. Please try again later.');
 
 
96
  }
97
  })
98
  .catch(error => {
99
  console.error('Error submitting form:', error);
100
  addMessage('bot', 'There was an error submitting your case. Please try again later.');
 
 
101
  });
102
  }
103
 
@@ -128,51 +218,120 @@ function displayOptions(options) {
128
  chatMessages.scrollTop = chatMessages.scrollHeight;
129
  }
130
 
131
- function handleResponse(userInput) {
132
- const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
133
- let botResponse = '';
134
- let options = [];
135
-
136
- // Handle name input (first user response after bot's greeting)
137
- if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
138
- userName = userInput; // Store the user's name
139
- botResponse = `Nice to meet you, ${userInput}! 😊 How can I assist you today?`;
140
- options = [
141
  { text: 'What is TioNat?', class: 'blue' },
142
  { text: 'How do I create an account on TioNat?', class: 'blue' },
143
  { text: 'How can I track my order?', class: 'blue' },
144
  { text: 'Do you ship internationally?', class: 'blue' },
145
- { text: 'What payment methods do you accept?', class: 'blue' },
 
 
146
  { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
147
  { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
148
  { text: 'How do I know which supplement is right for me?', class: 'blue' },
149
  { text: 'Are there any side effects of using your supplements?', class: 'blue' },
150
- { text: 'Do you offer products for specific health conditions?', class: 'blue' },
 
 
151
  { text: 'How do I use your health care products?', class: 'blue' },
152
  { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
153
  { text: 'Are your health care products FDA-approved?', class: 'blue' },
154
  { text: 'Are your personal care products cruelty-free?', class: 'blue' },
155
- { text: 'What ingredients do you use in your skincare products?', class: 'blue' },
 
 
156
  { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
157
  { text: 'How do I find the right skincare routine?', class: 'blue' },
158
  { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
159
  { text: 'How long does it take to receive my order?', class: 'blue' },
160
- { text: 'Do you offer free shipping?', class: 'blue' },
 
 
161
  { text: 'How do I return a product?', class: 'blue' },
162
  { text: 'How can I contact customer service?', class: 'blue' },
163
  { text: 'Can I track my return?', class: 'blue' },
164
  { text: 'How can I get a product recommendation?', class: 'blue' },
165
- { text: 'How do I reset my password?', class: 'blue' },
 
 
166
  { text: 'Is my payment information secure?', class: 'blue' },
167
  { text: 'How do I update my account information?', class: 'blue' },
168
  { text: 'Can I leave a review for a product?', class: 'blue' },
169
- { text: 'How do I report a product issue?', class: 'blue' },
170
- { text: 'Other', class: 'green' }
171
- ];
172
- } else if (lastMessage.includes('what is tionat?')) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  botResponse = 'TioNat is an e-commerce platform offering a wide range of products in Nutritional Care, Health Care, and Personal Care categories. We aim to improve the quality of life with top-notch, reliable, and scientifically-backed products.';
174
  } else if (lastMessage.includes('how do i create an account on tionat?')) {
175
- botResponse = 'To create an account on TioNat, simply click on the "Sign Up" button on the top-right corner of our website. Enter your name, email address, and a password. Once registered, you can start shopping!';
176
  } else if (lastMessage.includes('how can i track my order?')) {
177
  botResponse = 'After placing your order, you will receive a tracking number via email. You can also track your order from your TioNat account under the "Order History" section.';
178
  } else if (lastMessage.includes('do you ship internationally?')) {
@@ -209,7 +368,7 @@ function handleResponse(userInput) {
209
  botResponse = 'Orders typically take 3-7 business days to process and ship, depending on your location. You can check your tracking number for more accurate delivery details.';
210
  } else if (lastMessage.includes('do you offer free shipping?')) {
211
  botResponse = 'Yes, we offer free shipping on orders above a certain amount. Check the shipping details at checkout to confirm eligibility.';
212
- } else if (lastMessage.includes('how do I return a product?')) {
213
  botResponse = 'If you are not satisfied with your purchase, you can return most products within 30 days. Please visit our Returns & Exchange page for detailed instructions.';
214
  } else if (lastMessage.includes('how can i contact customer service?')) {
215
  botResponse = 'You can reach our customer service team via email at support@tionat.com or call us at +1-800-123-4567. We\'re here to assist you with any queries.';
@@ -227,57 +386,14 @@ function handleResponse(userInput) {
227
  botResponse = 'Yes! After purchasing a product, you can leave a review directly on the product page. We value your feedback and it helps other customers make informed decisions.';
228
  } else if (lastMessage.includes('how do i report a product issue?')) {
229
  botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.';
230
- } else if (lastMessage.includes('other')) {
231
- botResponse = "Please provide more details about your query so our service agent can assist you.";
232
- addMessage('bot', botResponse);
233
- displayForm();
234
- return; // Stop further processing since we're showing the form
235
  } else {
236
- // Instead of showing the form, guide the user to select "Other"
237
- botResponse = "I'm sorry, I don't understand your query. Please select the 'Other' option to proceed.";
238
  addMessage('bot', botResponse);
239
-
240
- // Redisplay the FAQ options if they aren't already visible
241
- if (conversation.length > 2 && !document.querySelector('.option-button')) {
242
- options = [
243
- { text: 'What is TioNat?', class: 'blue' },
244
- { text: 'How do I create an account on TioNat?', class: 'blue' },
245
- { text: 'How can I track my order?', class: 'blue' },
246
- { text: 'Do you ship internationally?', class: 'blue' },
247
- { text: 'What payment methods do you accept?', class: 'blue' },
248
- { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
249
- { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
250
- { text: 'How do I know which supplement is right for me?', class: 'blue' },
251
- { text: 'Are there any side effects of using your supplements?', class: 'blue' },
252
- { text: 'Do you offer products for specific health conditions?', class: 'blue' },
253
- { text: 'How do I use your health care products?', class: 'blue' },
254
- { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
255
- { text: 'Are your health care products FDA-approved?', class: 'blue' },
256
- { text: 'Are your personal care products cruelty-free?', class: 'blue' },
257
- { text: 'What ingredients do you use in your skincare products?', class: 'blue' },
258
- { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
259
- { text: 'How do I find the right skincare routine?', class: 'blue' },
260
- { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
261
- { text: 'How long does it take to receive my order?', class: 'blue' },
262
- { text: 'Do you offer free shipping?', class: 'blue' },
263
- { text: 'How do I return a product?', class: 'blue' },
264
- { text: 'How can I contact customer service?', class: 'blue' },
265
- { text: 'Can I track my return?', class: 'blue' },
266
- { text: 'How can I get a product recommendation?', class: 'blue' },
267
- { text: 'How do I reset my password?', class: 'blue' },
268
- { text: 'Is my payment information secure?', class: 'blue' },
269
- { text: 'How do I update my account information?', class: 'blue' },
270
- { text: 'Can I leave a review for a product?', class: 'blue' },
271
- { text: 'How do I report a product issue?', class: 'blue' },
272
- { text: 'Other', class: 'green' }
273
- ];
274
- displayOptions(options);
275
  }
276
- return; // Stop further processing
277
  }
278
 
279
  addMessage('bot', botResponse);
280
- if (options.length > 0) {
281
- displayOptions(options);
282
- }
283
- }
 
1
  let conversation = [
2
+ { role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }
3
  ];
4
  let userName = ''; // To store the user's name
5
+ let isSubmitting = false; // Flag to prevent multiple submissions
6
 
7
  function addMessage(role, message) {
8
  const chatMessages = document.getElementById('chatMessages');
 
35
  }
36
  }
37
 
38
+ // Add Enter key event listener for the input field
39
+ document.addEventListener('DOMContentLoaded', () => {
40
+ const userInput = document.getElementById('userInput');
41
+ const clearButton = document.getElementById('clearButton');
42
+ if (userInput) {
43
+ userInput.addEventListener('keydown', (event) => {
44
+ if (event.key === 'Enter' && !event.shiftKey) {
45
+ event.preventDefault();
46
+ sendMessage();
47
+ }
48
+ });
49
+ }
50
+ if (clearButton) {
51
+ clearButton.addEventListener('click', clearChat);
52
+ }
53
+ });
54
+
55
+ function clearChat() {
56
+ const chatMessages = document.getElementById('chatMessages');
57
+ chatMessages.innerHTML = '<div class="bot-message">Hi there! I\'m TioNat Assistant! May I know your name?</div>';
58
+ conversation = [{ role: 'bot', message: "Hi there! I'm TioNat Assistant! May I know your name?" }];
59
+ userName = '';
60
+ document.getElementById('userInput').value = '';
61
+ isSubmitting = false; // Reset submission flag on chat clear
62
+ }
63
+
64
  function displayForm() {
65
  const chatMessages = document.getElementById('chatMessages');
66
  if (!chatMessages) {
 
83
  <input type="email" id="email" placeholder="Enter your email" /><br/>
84
  <label for="issueDescription">Issue Description:</label>
85
  <textarea id="issueDescription" placeholder="Describe your issue"></textarea><br/>
86
+ <button id="submitButton" onclick="submitForm()">Submit</button>
87
  </div>
88
  `;
89
  chatMessages.appendChild(formDiv);
90
  chatMessages.scrollTop = chatMessages.scrollHeight;
91
  }
92
 
93
+ function resetForm() {
94
+ const mobileNumber = document.getElementById('mobileNumber');
95
+ const email = document.getElementById('email');
96
+ const issueDescription = document.getElementById('issueDescription');
97
+ const submitButton = document.getElementById('submitButton');
98
+
99
+ if (mobileNumber) mobileNumber.value = '';
100
+ if (email) email.value = '';
101
+ if (issueDescription) issueDescription.value = '';
102
+ if (submitButton) submitButton.disabled = false;
103
+ isSubmitting = false; // Reset the flag after form reset
104
+ console.log('Form reset after 4 seconds');
105
+ }
106
+
107
  function submitForm() {
108
+ if (isSubmitting) {
109
+ console.log('Submission already in progress, ignoring additional click');
110
+ return;
111
+ }
112
+
113
+ const submitButton = document.getElementById('submitButton');
114
+ if (!submitButton) {
115
+ console.error('Submit button not found!');
116
+ return;
117
+ }
118
+
119
+ // Disable the button and set the flag
120
+ isSubmitting = true;
121
+ submitButton.disabled = true;
122
+ console.log('Submit button clicked, starting submission process');
123
+
124
  const mobileNumber = document.getElementById('mobileNumber').value.trim();
125
  const email = document.getElementById('email').value.trim();
126
  const issueDescription = document.getElementById('issueDescription').value.trim();
127
 
128
+ // Validate mobile number (must be 10 digits)
129
+ const mobileRegex = /^\d{10}$/;
130
+ if (!mobileNumber || !mobileRegex.test(mobileNumber)) {
131
+ addMessage('bot', 'Please enter a valid 10-digit mobile number.');
132
+ isSubmitting = false;
133
+ submitButton.disabled = false;
134
+ console.log('Validation failed: Invalid mobile number');
135
+ return;
136
+ }
137
+ if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
138
+ addMessage('bot', 'Please enter a valid email address.');
139
+ isSubmitting = false;
140
+ submitButton.disabled = false;
141
+ console.log('Validation failed: Invalid email');
142
+ return;
143
+ }
144
+ if (!issueDescription) {
145
+ addMessage('bot', 'Please describe your issue.');
146
+ isSubmitting = false;
147
+ submitButton.disabled = false;
148
+ console.log('Validation failed: Issue description missing');
149
  return;
150
  }
151
 
 
156
  issueDescription: issueDescription
157
  };
158
 
159
+ console.log('Sending form data to server:', formData);
160
+
161
  fetch('/submit-case', {
162
  method: 'POST',
163
  headers: {
 
165
  },
166
  body: JSON.stringify(formData),
167
  })
168
+ .then(response => {
169
+ console.log('Received response from server:', response);
170
+ return response.json();
171
+ })
172
  .then(data => {
173
+ console.log('Parsed response data:', data);
174
  if (data.success) {
175
  addMessage('bot', 'Thank you! Your case has been submitted. Our service agent will get back to you soon.');
176
+ // Reset the form after 4 seconds
177
+ setTimeout(() => {
178
+ resetForm();
179
+ }, 4000);
180
  } else {
181
  addMessage('bot', 'There was an error submitting your case. Please try again later.');
182
+ isSubmitting = false;
183
+ submitButton.disabled = false;
184
  }
185
  })
186
  .catch(error => {
187
  console.error('Error submitting form:', error);
188
  addMessage('bot', 'There was an error submitting your case. Please try again later.');
189
+ isSubmitting = false;
190
+ submitButton.disabled = false;
191
  });
192
  }
193
 
 
218
  chatMessages.scrollTop = chatMessages.scrollHeight;
219
  }
220
 
221
+ function displayCategories() {
222
+ const categories = [
223
+ { text: 'About TioNat and Account Setup', class: 'blue', questions: [
 
 
 
 
 
 
 
224
  { text: 'What is TioNat?', class: 'blue' },
225
  { text: 'How do I create an account on TioNat?', class: 'blue' },
226
  { text: 'How can I track my order?', class: 'blue' },
227
  { text: 'Do you ship internationally?', class: 'blue' },
228
+ { text: 'What payment methods do you accept?', class: 'blue' }
229
+ ]},
230
+ { text: 'Nutritional Supplements', class: 'blue', questions: [
231
  { text: 'What is the best supplement for boosting immunity?', class: 'blue' },
232
  { text: 'Are your nutritional supplements vegan-friendly?', class: 'blue' },
233
  { text: 'How do I know which supplement is right for me?', class: 'blue' },
234
  { text: 'Are there any side effects of using your supplements?', class: 'blue' },
235
+ { text: 'Do you offer products for specific health conditions?', class: 'blue' }
236
+ ]},
237
+ { text: 'Health Care Products', class: 'blue', questions: [
238
  { text: 'How do I use your health care products?', class: 'blue' },
239
  { text: 'Do your products come with a money-back guarantee?', class: 'blue' },
240
  { text: 'Are your health care products FDA-approved?', class: 'blue' },
241
  { text: 'Are your personal care products cruelty-free?', class: 'blue' },
242
+ { text: 'What ingredients do you use in your skincare products?', class: 'blue' }
243
+ ]},
244
+ { text: 'Personal Care and Skincare', class: 'blue', questions: [
245
  { text: 'Can I use your personal care products for sensitive skin?', class: 'blue' },
246
  { text: 'How do I find the right skincare routine?', class: 'blue' },
247
  { text: 'Can I modify or cancel my order after placing it?', class: 'blue' },
248
  { text: 'How long does it take to receive my order?', class: 'blue' },
249
+ { text: 'Do you offer free shipping?', class: 'blue' }
250
+ ]},
251
+ { text: 'Returns and Customer Support', class: 'blue', questions: [
252
  { text: 'How do I return a product?', class: 'blue' },
253
  { text: 'How can I contact customer service?', class: 'blue' },
254
  { text: 'Can I track my return?', class: 'blue' },
255
  { text: 'How can I get a product recommendation?', class: 'blue' },
256
+ { text: 'How do I reset my password?', class: 'blue' } // Moved here
257
+ ]},
258
+ { text: 'Security and Account Management', class: 'blue', questions: [
259
  { text: 'Is my payment information secure?', class: 'blue' },
260
  { text: 'How do I update my account information?', class: 'blue' },
261
  { text: 'Can I leave a review for a product?', class: 'blue' },
262
+ { text: 'How do I report a product issue?', class: 'blue' }
263
+ ]},
264
+ { text: 'Other', class: 'green' }
265
+ ];
266
+
267
+ const chatMessages = document.getElementById('chatMessages');
268
+ if (!chatMessages) {
269
+ console.error('Chat messages container not found for categories!');
270
+ return;
271
+ }
272
+
273
+ categories.forEach(category => {
274
+ const messageDiv = document.createElement('div');
275
+ messageDiv.className = 'bot-message';
276
+ const button = document.createElement('button');
277
+ button.textContent = category.text;
278
+ button.className = `option-button ${category.class}`;
279
+
280
+ button.onclick = () => {
281
+ addMessage('user', category.text);
282
+ conversation.push({ role: 'user', message: category.text });
283
+ if (category.text === 'Other') {
284
+ addMessage('bot', "Please provide more details about your query so our service agent can assist you.");
285
+ displayForm();
286
+ } else {
287
+ displayOptions(category.questions);
288
+ }
289
+ };
290
+
291
+ messageDiv.appendChild(button);
292
+ chatMessages.appendChild(messageDiv);
293
+ });
294
+
295
+ chatMessages.scrollTop = chatMessages.scrollHeight;
296
+ }
297
+
298
+ function handleResponse(userInput) {
299
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
300
+ let botResponse = '';
301
+
302
+ // Handle name input (first user response after bot's greeting)
303
+ if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
304
+ userName = userInput;
305
+ botResponse = `Nice to meet you, ${userInput}! 😊 Please select a category to explore common questions or choose 'Other' to submit a custom query.`;
306
+ addMessage('bot', botResponse);
307
+ displayCategories();
308
+ return;
309
+ }
310
+
311
+ // Handle category selection and FAQ responses
312
+ if (lastMessage.includes('about tionat and account setup')) {
313
+ return; // Questions are already displayed via displayOptions
314
+ } else if (lastMessage.includes('nutritional supplements')) {
315
+ return;
316
+ } else if (lastMessage.includes('health care products')) {
317
+ return;
318
+ } else if (lastMessage.includes('personal care and skincare')) {
319
+ return;
320
+ } else if (lastMessage.includes('returns and customer support')) {
321
+ return;
322
+ } else if (lastMessage.includes('security and account management')) {
323
+ return;
324
+ } else if (lastMessage.includes('other')) {
325
+ addMessage('bot', "Please provide more details about your query so our service agent can assist you.");
326
+ displayForm();
327
+ return;
328
+ }
329
+
330
+ // Handle FAQ responses
331
+ if (lastMessage.includes('what is tionat?')) {
332
  botResponse = 'TioNat is an e-commerce platform offering a wide range of products in Nutritional Care, Health Care, and Personal Care categories. We aim to improve the quality of life with top-notch, reliable, and scientifically-backed products.';
333
  } else if (lastMessage.includes('how do i create an account on tionat?')) {
334
+ botResponse = 'To create an account on TioNat, simply click on the "Sign Up" button on the top-right corner of our website. Enter your name, email address, and a password. Once registered, you can start shopping!';
335
  } else if (lastMessage.includes('how can i track my order?')) {
336
  botResponse = 'After placing your order, you will receive a tracking number via email. You can also track your order from your TioNat account under the "Order History" section.';
337
  } else if (lastMessage.includes('do you ship internationally?')) {
 
368
  botResponse = 'Orders typically take 3-7 business days to process and ship, depending on your location. You can check your tracking number for more accurate delivery details.';
369
  } else if (lastMessage.includes('do you offer free shipping?')) {
370
  botResponse = 'Yes, we offer free shipping on orders above a certain amount. Check the shipping details at checkout to confirm eligibility.';
371
+ } else if (lastMessage.includes('how do i return a product?')) {
372
  botResponse = 'If you are not satisfied with your purchase, you can return most products within 30 days. Please visit our Returns & Exchange page for detailed instructions.';
373
  } else if (lastMessage.includes('how can i contact customer service?')) {
374
  botResponse = 'You can reach our customer service team via email at support@tionat.com or call us at +1-800-123-4567. We\'re here to assist you with any queries.';
 
386
  botResponse = 'Yes! After purchasing a product, you can leave a review directly on the product page. We value your feedback and it helps other customers make informed decisions.';
387
  } else if (lastMessage.includes('how do i report a product issue?')) {
388
  botResponse = 'If you receive a defective or damaged product, please contact our customer service immediately for assistance with returns or exchanges.';
 
 
 
 
 
389
  } else {
390
+ botResponse = "I'm sorry, I don't understand your query. Please select a category to explore common questions or choose 'Other' to submit a custom query.";
 
391
  addMessage('bot', botResponse);
392
+ if (!document.querySelector('.option-button')) {
393
+ displayCategories();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  }
395
+ return;
396
  }
397
 
398
  addMessage('bot', botResponse);
399
+ }