BCStudentSoftwareDevTeam / celts

Web app to support the CELTS program at Berea College
BSD 3-Clause "New" or "Revised" License
1 stars 8 forks source link

Bonner Cohort Invite #1295

Open Mbeweg opened 4 months ago

Mbeweg commented 4 months ago

In the Bonner training event, the selected/invited cohort does not stay checked. image image

Mbeweg commented 4 months ago

We worked on the cohort checkboxes. Using the createEvents.js file, we added a new function that enables the checkbox of the selected cohort to remain checked when saved. To ensure that cohort checkboxes remain checked even after the page is refreshed we used the saveSelectedCohorts function to collect the values of all checked checkboxes and store them in localStorage whenever a checkbox changes. The loadSelectedCohorts function retrieves these values from localStorage and checks the corresponding checkboxes when the page loads. By attaching the saveSelectedCohorts function to the change event of the checkboxes and calling loadSelectedCohorts when the document is ready, the code maintains the state of the checkboxes across page reloads. This is the code below;

function saveSelectedCohorts() {
  const selectedCohorts = [];
  $("input[name='cohorts[]']:checked").each(function () {
    selectedCohorts.push($(this).val());
  });
  localStorage.setItem("selectedCohorts", JSON.stringify(selectedCohorts));
}

// Attach the change event to all cohort checkboxes
$(document).on("change", "input[name='cohorts[]']", saveSelectedCohorts);

function loadSelectedCohorts() {
  const selectedCohorts = JSON.parse(localStorage.getItem("selectedCohorts")) || [];
  selectedCohorts.forEach(function (year) {
    $(`input[name='cohorts[]'][value='${year}']`).prop("checked", true);
  }); ``