Initial logic

Generalize our javascript handler for the hide rejection reason field
This commit is contained in:
zandercymatics 2024-05-31 15:19:28 -06:00
parent daeabaf833
commit c772c71707
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 68 additions and 26 deletions

View file

@ -1584,6 +1584,8 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
# The opposite of this condition is acceptable (rejected -> other status and rejection_reason) # The opposite of this condition is acceptable (rejected -> other status and rejection_reason)
# because we clean up the rejection reason in the transition in the model. # because we clean up the rejection reason in the transition in the model.
error_message = FSMDomainRequestError.get_error_message(FSMErrorCodes.NO_REJECTION_REASON) error_message = FSMDomainRequestError.get_error_message(FSMErrorCodes.NO_REJECTION_REASON)
elif obj.status == models.DomainRequest.DomainRequestStatus.ACTION_NEEDED and not obj.action_needed_reason:
error_message = FSMDomainRequestError.get_error_message(FSMErrorCodes.NO_ACTION_NEEDED_REASON)
else: else:
# This is an fsm in model which will throw an error if the # This is an fsm in model which will throw an error if the
# transition condition is violated, so we roll back the # transition condition is violated, so we roll back the

View file

@ -296,28 +296,46 @@ function initializeWidgetOnList(list, parentId) {
} }
/** An IIFE for admin in DjangoAdmin to listen to changes on the domain request /** An IIFE for admin in DjangoAdmin to listen to changes on the domain request
* status select and to show/hide the rejection reason * status select and to show/hide fields like rejection reason or action needed reason
*/ */
(function (){ (function (){
let rejectionReasonFormGroup = document.querySelector('.field-rejection_reason') // Hides or shows a given field based off of the current value of the given status selector,
// and stores its state in the session.
function showHideFieldsOnStatusChange(elementToHide, statusToShowOn, sessionObjectName) {
if (elementToHide) {
let statusSelect = document.getElementById('id_status')
let shouldHide = statusSelect.value != statusToShowOn
// Initial handling of parentFormGroup display
hideOrShowDomObject(elementToHide, hideObject=shouldHide)
// Listen to change events and handle rejectionReasonFormGroup display, then save status to session storage
statusSelect.addEventListener('change', function() {
// Hide the object only if we're in an invalid state
shouldHide = statusSelect.value != statusToShowOn
if (rejectionReasonFormGroup) { // Hide the action needed field if we're on a different status type
let statusSelect = document.getElementById('id_status') hideOrShowDomObject(elementToHide, hideObject=shouldHide)
// Initial handling of rejectionReasonFormGroup display // Add a key to our session storage to track if we should hide the object automatically
if (statusSelect.value != 'rejected') // (to catch the edge case where you click the back button)
rejectionReasonFormGroup.style.display = 'none'; if (!shouldHide){
sessionStorage.removeItem(sessionObjectName);
}else {
sessionStorage.setItem(sessionObjectName, 'true');
}
});
}
}
// Listen to change events and handle rejectionReasonFormGroup display, then save status to session storage function hideOrShowDomObject(object, hideObject){
statusSelect.addEventListener('change', function() { if (object){
if (statusSelect.value == 'rejected') { if (hideObject){
rejectionReasonFormGroup.style.display = 'block'; object.classList.add("display-none");
sessionStorage.removeItem('hideRejectionReason'); }else {
} else { object.classList.remove("display-none");
rejectionReasonFormGroup.style.display = 'none';
sessionStorage.setItem('hideRejectionReason', 'true');
} }
}); }
} }
// Listen to Back/Forward button navigation and handle rejectionReasonFormGroup display based on session storage // Listen to Back/Forward button navigation and handle rejectionReasonFormGroup display based on session storage
@ -325,17 +343,39 @@ function initializeWidgetOnList(list, parentId) {
// When you navigate using forward/back after changing status but not saving, when you land back on the DA page the // When you navigate using forward/back after changing status but not saving, when you land back on the DA page the
// status select will say (for example) Rejected but the selected option can be something else. To manage the show/hide // status select will say (for example) Rejected but the selected option can be something else. To manage the show/hide
// accurately for this edge case, we use cache and test for the back/forward navigation. // accurately for this edge case, we use cache and test for the back/forward navigation.
const observer = new PerformanceObserver((list) => { function handleBackButtonObserver(fieldsToObserve) {
list.getEntries().forEach((entry) => { const observer = new PerformanceObserver((list) => {
if (entry.type === "back_forward") { list.getEntries().forEach((entry) => {
if (sessionStorage.getItem('hideRejectionReason')) if (entry.type === "back_forward") {
document.querySelector('.field-rejection_reason').style.display = 'none'; fieldsToObserve.forEach((fieldName) => {
else fieldClass = `.field-${fieldName}`
document.querySelector('.field-rejection_reason').style.display = 'block'; field = document.querySelector(fieldClass)
} if (field) {
shouldHideField = sessionStorage.getItem(`hide_${fieldName}`)
hideOrShowDomObject(field, hideObject=shouldHideField)
}else {
console.error(`Could not find field with class ${fieldClass}`)
}
});
}
});
}); });
}); observer.observe({ type: "navigation" });
observer.observe({ type: "navigation" }); }
function handleStatusChanges() {
// Show/hide the rejection reason
let rejectionReasonFormGroup = document.querySelector('.field-rejection_reason')
showHideFieldsOnStatusChange(rejectionReasonFormGroup, "rejected", "hide_rejection_reason");
// Show/hude the action needed reason
let actionNeededReasonFormGroup = document.querySelector('.field-action_needed_reason');
showHideFieldsOnStatusChange(actionNeededReasonFormGroup, "action_needed", "hide_action_needed_reason");
}
handleStatusChanges();
fieldsToObserve = ["rejection_reason", "action_needed_reason"]
handleBackButtonObserver(fieldsToObserve);
})(); })();
/** An IIFE for toggling the submit bar on domain request forms /** An IIFE for toggling the submit bar on domain request forms