From 646e37570800ee77404e7114f7f8bd0eda30614d Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:49:46 -0600 Subject: [PATCH] Fix bugs --- src/registrar/admin.py | 18 ++++----- src/registrar/assets/js/get-gov-admin.js | 50 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/registrar/admin.py b/src/registrar/admin.py index 42d2872a2..e15228534 100644 --- a/src/registrar/admin.py +++ b/src/registrar/admin.py @@ -1735,18 +1735,18 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): original_obj = models.DomainRequest.objects.get(pk=obj.pk) # == Handle action_needed_reason == # - # Store the email that was sent out if one was sent and it isn't saved to a variable yet + default_email = self._get_action_needed_reason_default_email(obj, obj.action_needed_reason) - if default_email: + reason_changed = obj.action_needed_reason != original_obj.action_needed_reason + if reason_changed: + # Track that we sent out an email + request.session["action_needed_email_sent"] = True + # Set the action_needed_reason_email to the default. # Since this check occurs after save, if the user enters a value then # we won't update. - reason_changed = obj.action_needed_reason != original_obj.action_needed_reason - if reason_changed: - request.session["action_needed_email_sent"] = True - logger.info("added session object") - if default_email == obj.action_needed_reason_email: - obj.action_needed_reason_email = default_email + if default_email and default_email == obj.action_needed_reason_email: + obj.action_needed_reason_email = default_email # == Handle status == # if obj.status == original_obj.status: @@ -1960,7 +1960,7 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin): email_sent = request.session.get("action_needed_email_sent", False) extra_context["action_needed_email_sent"] = email_sent if email_sent: - email_sent = request.session["action_needed_email_sent"] = False + request.session["action_needed_email_sent"] = False # Call the superclass method with updated extra_context return super().change_view(request, object_id, form_url, extra_context) diff --git a/src/registrar/assets/js/get-gov-admin.js b/src/registrar/assets/js/get-gov-admin.js index fbbea7003..503055a89 100644 --- a/src/registrar/assets/js/get-gov-admin.js +++ b/src/registrar/assets/js/get-gov-admin.js @@ -535,10 +535,11 @@ function initializeWidgetOnList(list, parentId) { const emptyReasonText = "-"; const noEmailText = "No email will be sent."; const domainRequestId = actionNeededReasonDropdown ? document.querySelector("#domain_request_id").value : null - if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData) { + const emailSentSessionVariableName = `actionNeededEmailSent-${domainRequestId}`; + + if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData && domainRequestId) { // Add a change listener to the action needed reason dropdown handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData); - document.addEventListener('DOMContentLoaded', function() { let reason = actionNeededReasonDropdown.value; noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText; @@ -552,14 +553,21 @@ function initializeWidgetOnList(list, parentId) { showElement(noEmailMessage); } + let emailWasSent = document.getElementById("action-needed-email-sent") - console.log(`email ${emailWasSent.value} vs session ${sessionStorage.getItem("actionNeededEmailSent")} vs id ${domainRequestId}`) - if (emailWasSent && emailWasSent.value) { - // add the session object - if (sessionStorage.getItem(`actionNeededEmailSent-${domainRequestId}`) === null) { - sessionStorage.setItem(`actionNeededEmailSent-${domainRequestId}`, domainRequestId); - } + if (emailWasSent && emailWasSent.value === "True") { + // An email was sent out - store that information in a session variable + addOrRemoveSessionBoolean(emailSentSessionVariableName, add=true) + } + + if (sessionStorage.getItem(emailSentSessionVariableName) !== null) { + // Show the readonly field, hide the editable field + showReadonly(actionNeededEmail.parentElement) + console.log("adding data") + }else { + // No email was sent out -- show the editable field hideReadonly(actionNeededEmail.parentElement) + console.log("removing data") } }); } @@ -582,13 +590,22 @@ function initializeWidgetOnList(list, parentId) { // Reset the session object on change since change refreshes the email content. // Only do this if we change the action needed reason, or if we: // change the reason => modify email content => change back to old reason. - if (oldDropdownValue != actionNeededReasonDropdown.value || oldEmailValue != actionNeededEmail.value) { - let emailSent = sessionStorage.getItem(`actionNeededEmailSent-${domainRequestId}`) + if (oldDropdownValue !== actionNeededReasonDropdown.value || oldEmailValue !== actionNeededEmail.value) { + let emailSent = sessionStorage.getItem(emailSentSessionVariableName) if (emailSent !== null){ - sessionStorage.removeItem(`actionNeededEmailSent-${domainRequestId}`); + console.log("removing data") + addOrRemoveSessionBoolean(emailSentSessionVariableName, add=false) } - showReadonly(actionNeededEmail.parentElement) } + + if (sessionStorage.getItem(emailSentSessionVariableName) !== null) { + // Show the readonly field, hide the editable field + showReadonly(actionNeededEmail.parentElement) + }else { + // No email was sent out -- show the editable field + hideReadonly(actionNeededEmail.parentElement) + } + }else { // Show the no email message hideElement(actionNeededEmail); @@ -605,17 +622,16 @@ function initializeWidgetOnList(list, parentId) { function showReadonly(actionNeededEmailParent) { let readonlyView = document.querySelector("#action-needed-reason-email-readonly") if (readonlyView) { - hideElement(readonlyView) - showElement(actionNeededEmailParent) + showElement(readonlyView) + hideElement(actionNeededEmailParent) } } function hideReadonly(actionNeededEmailParent) { let readonlyView = document.querySelector("#action-needed-reason-email-readonly") if (readonlyView) { - showElement(readonlyView) - hideElement(actionNeededEmailParent) + hideElement(readonlyView) + showElement(actionNeededEmailParent) } } - })();