This commit is contained in:
Rachid Mrad 2024-09-03 21:09:17 -04:00
parent 94b5c0642c
commit e33daf7cc3
No known key found for this signature in database

View file

@ -510,9 +510,6 @@ document.addEventListener('DOMContentLoaded', function() {
const dropdown = document.getElementById("id_action_needed_reason");
const textarea = document.getElementById("id_action_needed_reason_email")
const domainRequestId = dropdown ? document.getElementById("domain_request_id").value : null
if(dropdown && textarea && domainRequestId) {
const texareaPlaceholder = document.querySelector(".field-action_needed_reason_email__placeholder");
const directEditButton = document.querySelector('.field-action_needed_reason_email__edit');
const modalTrigger = document.querySelector('.field-action_needed_reason_email__modal-trigger');
@ -522,24 +519,21 @@ document.addEventListener('DOMContentLoaded', function() {
<use xlink:href="/public/img/sprite.svg#check_circle"></use>
</svg>`;
let lastSentEmailContent = document.getElementById("last-sent-email-content");
const helpText = document.querySelector('.field-action_needed_reason_email .help');
// Get the list of emails associated with each action-needed dropdown value
const emailData = document.getElementById('action-needed-emails-data');
const actionNeededEmailData = emailData.textContent;
const actionNeededEmailsJson = JSON.parse(actionNeededEmailData);
// Get initial dropdown and email
const initialDropdownValue = dropdown ? dropdown.value : null;
const initialEmailValue = actionNeededEmailData ? actionNeededEmailData.value : null;
if(
!formLabel ||
!modalConfirm ||
!actionNeededEmailData) {
return;
// We will use the const to control the modal
let isEmailAlreadySentConst = lastSentEmailContent.value.replace(/\s+/g, '') === textarea.value.replace(/\s+/g, '');
// We will use the function to control the label and help
function isEmailAlreadySent() {
return lastSentEmailContent.value.replace(/\s+/g, '') === textarea.value.replace(/\s+/g, '');
}
if (!dropdown || !textarea || !domainRequestId || !formLabel || !modalConfirm || !emailData) return;
function updateUserInterface(reason) {
if (!reason) {
// No reason selected, we will set the label to "Email", show the "Make a selection" placeholder, hide the trigger, textarea, hide the help text
@ -566,33 +560,29 @@ document.addEventListener('DOMContentLoaded', function() {
showElement(textarea);
textarea.setAttribute('readonly', true);
showElement(helpText);
if (checkEmailAlreadySent()) {
if (isEmailAlreadySentConst) {
hideElement(directEditButton);
showElement(modalTrigger);
formLabel.innerHTML = greenCheckMark + "Email sent to the creator:"
helpText.innerHTML = "This email has been sent to the creator of this request"
} else {
showElement(directEditButton);
hideElement(modalTrigger);
}
if (isEmailAlreadySent()) {
formLabel.innerHTML = greenCheckMark + "Email sent to the creator:"
helpText.innerHTML = "This email has been sent to the creator of this request"
} else {
formLabel.innerHTML = "Auto-generated email that will be sent to the creator";
helpText.innerHTML = "This email will be sent to the creator of this request after saving";
}
}
}
function checkEmailAlreadySent() {
lastEmailSent = lastSentEmailContent.value.replace(/\s+/g, '')
currentEmailInTextArea = textarea.value.replace(/\s+/g, '')
return lastEmailSent === currentEmailInTextArea
}
// Init the UI
// Initialize UI
updateUserInterface(dropdown.value);
// Add a change listener to the action needed reason dropdown
dropdown.addEventListener("change", function() {
let reason = dropdown.value;
let emailBody = reason in actionNeededEmailsJson ? actionNeededEmailsJson[reason] : null;
const reason = dropdown.value;
const emailBody = reason in actionNeededEmailsJson ? actionNeededEmailsJson[reason] : null;
if (reason && emailBody) {
// If it's not the initial value
@ -606,14 +596,8 @@ document.addEventListener('DOMContentLoaded', function() {
updateUserInterface(reason);
});
modalConfirm.addEventListener("click", function() {
textarea.removeAttribute('readonly');
});
directEditButton.addEventListener("click", function() {
textarea.removeAttribute('readonly');
});
}
modalConfirm.addEventListener("click", () => textarea.removeAttribute('readonly'));
directEditButton.addEventListener("click", () => textarea.removeAttribute('readonly'));
});