Readonly on send

This commit is contained in:
zandercymatics 2024-07-12 15:11:42 -06:00
parent bac9dc94aa
commit 58605f11e5
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 48 additions and 16 deletions

View file

@ -1742,8 +1742,11 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
# Since this check occurs after save, if the user enters a value then # Since this check occurs after save, if the user enters a value then
# we won't update. # we won't update.
reason_changed = obj.action_needed_reason != original_obj.action_needed_reason reason_changed = obj.action_needed_reason != original_obj.action_needed_reason
if reason_changed and default_email == obj.action_needed_reason_email: if reason_changed:
obj.action_needed_reason_email = default_email 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
# == Handle status == # # == Handle status == #
if obj.status == original_obj.status: if obj.status == original_obj.status:
@ -1953,6 +1956,12 @@ class DomainRequestAdmin(ListHeaderAdmin, ImportExportModelAdmin):
extra_context["action_needed_reason_emails"] = self.get_all_action_needed_reason_emails_as_json(obj) extra_context["action_needed_reason_emails"] = self.get_all_action_needed_reason_emails_as_json(obj)
extra_context["has_profile_feature_flag"] = flag_is_active(request, "profile_feature") extra_context["has_profile_feature_flag"] = flag_is_active(request, "profile_feature")
# Denote if an action needed email was sent or not
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
# Call the superclass method with updated extra_context # Call the superclass method with updated extra_context
return super().change_view(request, object_id, form_url, extra_context) return super().change_view(request, object_id, form_url, extra_context)

View file

@ -36,6 +36,15 @@ function openInNewTab(el, removeAttribute = false){
} }
}; };
// Adds or removes a boolean from our session
function addOrRemoveSessionBoolean(name, add){
if (add) {
sessionStorage.setItem(name, "true");
}else {
sessionStorage.removeItem(name);
}
}
// <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>> // <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
// Event handlers. // Event handlers.
@ -418,15 +427,6 @@ function initializeWidgetOnList(list, parentId) {
object.classList.add("display-none"); object.classList.add("display-none");
} }
} }
// Adds or removes a boolean from our session
function addOrRemoveSessionBoolean(name, add){
if (add) {
sessionStorage.setItem(name, "true");
}else {
sessionStorage.removeItem(name);
}
}
})(); })();
/** An IIFE for toggling the submit bar on domain request forms /** An IIFE for toggling the submit bar on domain request forms
@ -529,9 +529,12 @@ function initializeWidgetOnList(list, parentId) {
let actionNeededReasonDropdown = document.querySelector("#id_action_needed_reason"); let actionNeededReasonDropdown = document.querySelector("#id_action_needed_reason");
let actionNeededEmail = document.querySelector("#id_action_needed_reason_email"); let actionNeededEmail = document.querySelector("#id_action_needed_reason_email");
let actionNeededEmailData = document.getElementById('action-needed-emails-data').textContent; let actionNeededEmailData = document.getElementById('action-needed-emails-data').textContent;
let noEmailMessage = document.getElementById("no-email-message"); let noEmailMessage = document.querySelector("#no-email-message");
const emptyReasonText = "-" const oldDropdownValue = actionNeededReasonDropdown ? actionNeededReasonDropdown.value : null;
const noEmailText = "No email will be sent." const oldEmailValue = actionNeededEmailData ? actionNeededEmailData.value : null;
const emptyReasonText = "-";
const noEmailText = "No email will be sent.";
const domainRequestId = actionNeededReasonDropdown ? document.querySelector("#domain_request_id").value : null
if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData) { if(actionNeededReasonDropdown && actionNeededEmail && actionNeededEmailData) {
// Add a change listener to the action needed reason dropdown // Add a change listener to the action needed reason dropdown
handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData); handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, actionNeededEmailData);
@ -548,6 +551,16 @@ function initializeWidgetOnList(list, parentId) {
hideElement(actionNeededEmail); hideElement(actionNeededEmail);
showElement(noEmailMessage); 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);
}
actionNeededEmail.readOnly = true
}
}); });
} }
@ -558,8 +571,6 @@ function initializeWidgetOnList(list, parentId) {
// Show the "no email will be sent" text only if a reason is actually selected. // Show the "no email will be sent" text only if a reason is actually selected.
noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText; noEmailMessage.innerHTML = reason ? noEmailText : emptyReasonText;
console.log(`reaso: ${reason} vs in ${reason in actionNeededEmailsJson}`)
console.log(noEmailMessage)
if (reason && reason in actionNeededEmailsJson) { if (reason && reason in actionNeededEmailsJson) {
let emailBody = actionNeededEmailsJson[reason]; let emailBody = actionNeededEmailsJson[reason];
if (emailBody) { if (emailBody) {
@ -567,6 +578,17 @@ function initializeWidgetOnList(list, parentId) {
actionNeededEmail.value = emailBody actionNeededEmail.value = emailBody
showElement(actionNeededEmail); showElement(actionNeededEmail);
hideElement(noEmailMessage); hideElement(noEmailMessage);
// 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 (emailSent !== null){
sessionStorage.removeItem(`actionNeededEmailSent-${domainRequestId}`);
}
actionNeededEmail.readOnly = false;
}
}else { }else {
// Show the no email message // Show the no email message
hideElement(actionNeededEmail); hideElement(actionNeededEmail);

View file

@ -147,6 +147,7 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
{% if field.field.name == "action_needed_reason_email" %} {% if field.field.name == "action_needed_reason_email" %}
{{ field.field }} {{ field.field }}
<p id="no-email-message" class="{% if original_object.action_needed_reason %}display-none{% endif %}">No email will be sent.</p> <p id="no-email-message" class="{% if original_object.action_needed_reason %}display-none{% endif %}">No email will be sent.</p>
<input id="action-needed-email-sent" class="display-none" value="{{action_needed_email_sent}}">
{% else %} {% else %}
{{ field.field }} {{ field.field }}
{% endif %} {% endif %}