Minor rewrite

This commit is contained in:
zandercymatics 2024-06-20 09:17:47 -06:00
parent 2e5f564ae6
commit 305bad2575
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
3 changed files with 52 additions and 41 deletions

View file

@ -8,6 +8,25 @@
// <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>> // <<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>>
// Helper functions. // Helper functions.
/**
* Hide element
*
*/
const hideElement = (element) => {
if (!element.classList.contains("display-none"))
element.classList.add('display-none');
};
/**
* Show element
*
*/
const showElement = (element) => {
if (element.classList.contains("display-none"))
element.classList.remove('display-none');
};
/** Either sets attribute target="_blank" to a given element, or removes it */ /** Either sets attribute target="_blank" to a given element, or removes it */
function openInNewTab(el, removeAttribute = false){ function openInNewTab(el, removeAttribute = false){
if(removeAttribute){ if(removeAttribute){
@ -521,30 +540,35 @@ function initializeWidgetOnList(list, parentId) {
})(); })();
/** An IIFE that hooks up to the "show email" button /** An IIFE that hooks up to the "show email" button
* which shows the auto generated email on action needed reason * which shows the auto generated email on action needed reason
*/ */
(function () { (function () {
let statusDropdown = document.getElementById("id_status"); let statusDropdown = document.getElementById("id_status");
let actionNeededReasonDropdown = document.getElementById("id_action_needed_reason");
let actionNeededEmail = document.getElementById("id_action_needed_reason_email")
let reasonFormRow = actionNeededEmail.closest(".form-row");
if(actionNeededReasonDropdown && actionNeededEmail && reasonFormRow) {
statusDropdown.addEventListener('change', function() { statusDropdown.addEventListener('change', function() {
// TODO we should also handle when action needed if (statusDropdown.value != "action needed") {
if (statusDropdown.value != "action needed"){ // Hide the email field by default
formRow.classList.add("display-none") hideElement(reasonFormRow)
}else {
showElement(reasonFormRow)
} }
}); });
let actionNeededDropdownReason = document.getElementById("id_action_needed_reason"); if (statusDropdown.value == "action needed")
// Store the domain request id on this record for simplicity handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, reasonFormRow);
let showEmailButton = document.getElementById("show_action_needed_email"); }
let actionNeededEmail = document.getElementById("id_action_needed_reason_email")
let formRow = actionNeededEmail.closest('.form-row'); // TODO fix edge case where nothing is selected
if(actionNeededDropdownReason && showEmailButton && actionNeededEmail && formRow) { function handleChangeActionNeededEmail(actionNeededReasonDropdown, actionNeededEmail, reasonFormRow) {
actionNeededDropdownReason.addEventListener('change', function() { actionNeededReasonDropdown.addEventListener('change', function() {
// TODO on change if not actionneeded on status, hide show email button // TODO on change if not actionneeded on status, hide show email button
const pk = showEmailButton.getAttribute("domain-request-id") const pk = document.querySelector("#domain_request_id").value
const reason = actionNeededDropdownReason.value const reason = actionNeededReasonDropdown.value
fetch(`/get-domain-requests-json/${pk}/action-needed-email/${reason}`) fetch(`/get-domain-requests-json/${pk}/action-needed-email/${reason}`)
.then(response => response.json()) .then(response => response.json())
.then(data => { .then(data => {
@ -558,13 +582,11 @@ function initializeWidgetOnList(list, parentId) {
actionNeededEmail.value = data.email_body_text actionNeededEmail.value = data.email_body_text
// Show the text field // Show the text field
if(actionNeededEmail.classList.contains("display-none")) { showElement(actionNeededEmail);
actionNeededEmail.classList.remove("display-none")
}
// Hide the message // Hide the "no email" message
if(noEmailMessage && !noEmailMessage.classList.contains("display-none")) { if(noEmailMessage) {
noEmailMessage.classList.add("display-none") hideElement(noEmailMessage);
} }
}else if (data && !data.email_body_text) { }else if (data && !data.email_body_text) {
@ -576,22 +598,16 @@ function initializeWidgetOnList(list, parentId) {
} }
// Hide the text field // Hide the text field
if(!actionNeededEmail.classList.contains("display-none")) { hideElement(actionNeededEmail);
actionNeededEmail.classList.add("display-none")
}
// Show the message // Show the message
if(noEmailMessage.classList.contains("display-none")) { showElement(noEmailMessage);
noEmailMessage.classList.remove("display-none")
} }
}
console.log(data) showElement(reasonFormRow)
}); });
}); });
showEmailButton.addEventListener('click', function() {
formRow.classList.remove("display-none")
});
} }
})(); })();

View file

@ -5,7 +5,8 @@
{% block field_sets %} {% block field_sets %}
{# Create an invisible <a> tag so that we can use a click event to toggle the modal. #} {# Create an invisible <a> tag so that we can use a click event to toggle the modal. #}
<a id="invisible-ineligible-modal-toggler" class="display-none" href="#toggle-set-ineligible" aria-controls="toggle-set-ineligible" data-open-modal></a> <a id="invisible-ineligible-modal-toggler" class="display-none" href="#toggle-set-ineligible" aria-controls="toggle-set-ineligible" data-open-modal></a>
{# Store the current object id so we can access it easier #}
<input id="domain_request_id" class="display-none" value="{{original.id}}" />
{% for fieldset in adminform %} {% for fieldset in adminform %}
{% comment %} {% comment %}
TODO: this will eventually need to be changed to something like this TODO: this will eventually need to be changed to something like this

View file

@ -113,12 +113,6 @@ This is using a custom implementation fieldset.html (see admin/fieldset.html)
<use xlink:href="/public/img/sprite.svg#expand_more"></use> <use xlink:href="/public/img/sprite.svg#expand_more"></use>
</svg> </svg>
</button> </button>
<button id="show_action_needed_email" domain-request-id="{{original_object.id}}" type="button" class="{% if field.field.value != "action needed" %}display-none{% endif %} collapse-toggle--dgsimple usa-button usa-button--unstyled margin-top-2 margin-bottom-1 margin-left-1">
<svg class="usa-icon" aria-hidden="true" focusable="false" role="img" width="24" height="24">
<use xlink:href="/public/img/sprite.svg#mail"></use>
</svg>
<span>Show email</span>
</button>
{% comment %} {% comment %}
<div class="usa-button-group"> <div class="usa-button-group">
<li class="usa-button-group__item"> <li class="usa-button-group__item">