Minor JS refactor

This commit is contained in:
zandercymatics 2024-03-04 08:37:52 -07:00
parent 8408ea0f68
commit 73673abae1
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -61,46 +61,51 @@ function openInNewTab(el, removeAttribute = false){
* This intentionally does not interact with createPhantomModalFormButtons() * This intentionally does not interact with createPhantomModalFormButtons()
*/ */
(function (){ (function (){
function displayModalOnDropdownClick(){ function displayModalOnDropdownClick(linkClickedDisplaysModal, statusDropdown, cancelButton, valueToCheck){
// Grab the invisible element that will hook to the modal.
// This doesn't technically need to be done with one, but this is simpler to manage.
let linkClickedDisplaysModal = document.getElementById("invisible-ineligible-modal-toggler")
let statusDropdown = document.getElementById("id_status")
// If these exist all at the same time, we're on the right page // If these exist all at the same time, we're on the right page
if (linkClickedDisplaysModal && statusDropdown && statusDropdown.value){ if (linkClickedDisplaysModal && statusDropdown && statusDropdown.value){
// Store the previous value in the event the user cancels.
// We only need to do this if we're on the correct page.
let previousValue = statusDropdown.value;
// Because the modal button does not have the class "dja-form-placeholder",
// it will not be affected by the createPhantomModalFormButtons() function.
let cancelButton = document.querySelector('button[name="_cancel_application_ineligible"]');
if (cancelButton){ if (cancelButton){
// Store the previous value in the event the user cancels.
// We only need to do this if cancel button is specified.
let previousValue = statusDropdown.value;
cancelButton.addEventListener('click', function() { cancelButton.addEventListener('click', function() {
// Revert the dropdown to its previous value // Revert the dropdown to its previous value
statusDropdown.value = previousValue; statusDropdown.value = previousValue;
}); });
}else {
console.log("displayModalOnDropdownClick() -> Cancel button was null")
}
// Add a change event listener to the dropdown. // Add a change event listener to the dropdown.
statusDropdown.addEventListener('change', function() { statusDropdown.addEventListener('change', function() {
// Check if "Ineligible" is selected // Check if "Ineligible" is selected
if (this.value && this.value.toLowerCase() === "ineligible") { if (this.value && this.value.toLowerCase() === valueToCheck) {
// Display the modal. // Display the modal.
linkClickedDisplaysModal.click() linkClickedDisplaysModal.click()
} }
// Update previousValue if another option is selected and confirmed
console.log(`This is the previous val NOW: ${previousValue}`)
}); });
}else{
} else{ console.error("displayModalOnDropdownClick() -> Some inputs are null")
console.error("displayModalOnDropdownClick() -> No cancel button defined.")
}
} }
} }
displayModalOnDropdownClick(); // When the status dropdown is clicked and is set to "ineligible", toggle a confirmation dropdown.
function hookModalToIneligibleStatus(){
// Grab the invisible element that will hook to the modal.
// This doesn't technically need to be done with one, but this is simpler to manage.
let modalButton = document.getElementById("invisible-ineligible-modal-toggler")
let statusDropdown = document.getElementById("id_status")
// Because the modal button does not have the class "dja-form-placeholder",
// it will not be affected by the createPhantomModalFormButtons() function.
let cancelButton = document.querySelector('button[name="_cancel_application_ineligible"]');
let valueToCheck = "ineligible"
displayModalOnDropdownClick(modalButton, statusDropdown, cancelButton, valueToCheck);
}
hookModalToIneligibleStatus()
})(); })();
/** An IIFE for pages in DjangoAdmin which may need custom JS implementation. /** An IIFE for pages in DjangoAdmin which may need custom JS implementation.