Spaces:
Sleeping
Sleeping
munyakarg commited on
Commit ·
09af50b
1
Parent(s): 4fd7f3f
supabase?
Browse files- static/script/script.js +107 -8
static/script/script.js
CHANGED
|
@@ -371,18 +371,117 @@ document.addEventListener("DOMContentLoaded", function () {
|
|
| 371 |
|
| 372 |
// Submit button
|
| 373 |
if (submitButton) {
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
// Continue button
|
| 380 |
const conButton = document.getElementById("conButton");
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
});
|
| 385 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
|
| 387 |
// Back buttons
|
| 388 |
const backButton = document.getElementById("backButton");
|
|
|
|
| 371 |
|
| 372 |
// Submit button
|
| 373 |
if (submitButton) {
|
| 374 |
+
submitButton.addEventListener("click", async function () {
|
| 375 |
+
const sessionId = sessionStorage.getItem('session_id');
|
| 376 |
+
|
| 377 |
+
// Collect Hispanic/Latino data
|
| 378 |
+
const hispanicCheckboxes = document.querySelectorAll('input[name="hispanic"]:checked');
|
| 379 |
+
const hispanicValues = Array.from(hispanicCheckboxes).map(cb => cb.value);
|
| 380 |
+
|
| 381 |
+
// Collect race data
|
| 382 |
+
const raceCheckboxes = document.querySelectorAll('input[name="race"]:checked');
|
| 383 |
+
const raceValues = Array.from(raceCheckboxes).map(cb => cb.value);
|
| 384 |
+
|
| 385 |
+
// Collect all subcategories
|
| 386 |
+
const subcategoryCheckboxes = document.querySelectorAll('.subcategories input[type="checkbox"]:checked');
|
| 387 |
+
const subcategoryValues = Array.from(subcategoryCheckboxes).map(cb => cb.value);
|
| 388 |
+
|
| 389 |
+
// Combine all race/ethnicity data
|
| 390 |
+
const allRaceData = [...hispanicValues, ...raceValues, ...subcategoryValues];
|
| 391 |
+
|
| 392 |
+
if (allRaceData.length === 0) {
|
| 393 |
+
alert('Please select at least one option');
|
| 394 |
+
return;
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
// Save race data and send to Supabase
|
| 398 |
+
try {
|
| 399 |
+
const saveResponse = await fetch('/save_survey', {
|
| 400 |
+
method: 'POST',
|
| 401 |
+
headers: {
|
| 402 |
+
'Content-Type': 'application/json',
|
| 403 |
+
},
|
| 404 |
+
body: JSON.stringify({
|
| 405 |
+
session_id: sessionId,
|
| 406 |
+
race: allRaceData.join(', ')
|
| 407 |
+
})
|
| 408 |
+
});
|
| 409 |
+
|
| 410 |
+
const saveData = await saveResponse.json();
|
| 411 |
+
|
| 412 |
+
if (saveData.success) {
|
| 413 |
+
// ✅ NOW send everything to Supabase
|
| 414 |
+
const sendResponse = await fetch('/send_results', {
|
| 415 |
+
method: 'POST',
|
| 416 |
+
headers: {
|
| 417 |
+
'Content-Type': 'application/json',
|
| 418 |
+
},
|
| 419 |
+
body: JSON.stringify({
|
| 420 |
+
session_id: sessionId,
|
| 421 |
+
contact_method: 'none', // No contact info for now
|
| 422 |
+
contact_info: ''
|
| 423 |
+
})
|
| 424 |
+
});
|
| 425 |
+
|
| 426 |
+
const sendData = await sendResponse.json();
|
| 427 |
+
|
| 428 |
+
if (sendData.success) {
|
| 429 |
+
console.log('✅ Data saved to Supabase!');
|
| 430 |
+
window.location.href = "thankyou.html";
|
| 431 |
+
} else {
|
| 432 |
+
console.error('Error sending to Supabase:', sendData.error);
|
| 433 |
+
// Still redirect even if Supabase fails
|
| 434 |
+
window.location.href = "thankyou.html";
|
| 435 |
+
}
|
| 436 |
+
} else {
|
| 437 |
+
alert('Error saving data. Please try again.');
|
| 438 |
+
}
|
| 439 |
+
} catch (error) {
|
| 440 |
+
console.error('Error:', error);
|
| 441 |
+
alert('Error saving data. Please try again.');
|
| 442 |
+
}
|
| 443 |
+
});
|
| 444 |
+
}
|
| 445 |
|
| 446 |
// Continue button
|
| 447 |
const conButton = document.getElementById("conButton");
|
| 448 |
+
if (conButton) {
|
| 449 |
+
conButton.addEventListener("click", async function () {
|
| 450 |
+
const sessionId = sessionStorage.getItem('session_id');
|
| 451 |
+
|
| 452 |
+
// Collect selected genders
|
| 453 |
+
const selectedGenders = [];
|
| 454 |
+
const genderCheckboxes = document.querySelectorAll('input[name="gender"]:checked');
|
| 455 |
+
genderCheckboxes.forEach(checkbox => {
|
| 456 |
+
selectedGenders.push(checkbox.value);
|
| 457 |
});
|
| 458 |
+
|
| 459 |
+
if (selectedGenders.length === 0) {
|
| 460 |
+
alert('Please select at least one option');
|
| 461 |
+
return;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
// Save gender data to backend
|
| 465 |
+
try {
|
| 466 |
+
await fetch('/save_survey', {
|
| 467 |
+
method: 'POST',
|
| 468 |
+
headers: {
|
| 469 |
+
'Content-Type': 'application/json',
|
| 470 |
+
},
|
| 471 |
+
body: JSON.stringify({
|
| 472 |
+
session_id: sessionId,
|
| 473 |
+
gender: selectedGenders.join(', ')
|
| 474 |
+
})
|
| 475 |
+
});
|
| 476 |
+
|
| 477 |
+
// Navigate to race page
|
| 478 |
+
window.location.href = "race.html";
|
| 479 |
+
} catch (error) {
|
| 480 |
+
console.error('Error saving gender:', error);
|
| 481 |
+
alert('Error saving data. Please try again.');
|
| 482 |
+
}
|
| 483 |
+
});
|
| 484 |
+
}
|
| 485 |
|
| 486 |
// Back buttons
|
| 487 |
const backButton = document.getElementById("backButton");
|