let fieldNameContainer = container.querySelector('.field-name-container');
let choicesContainer = container.querySelector('.choices-container');
let buttonContainer = container.querySelector('.button-container');
let toggleContainer = container.querySelector('.toggle-container');
if (!fieldNameContainer) {
fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
container.appendChild(fieldNameContainer);
}
if (!choicesContainer) {
choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
container.appendChild(choicesContainer);
}
const eventContainer = container.closest('.event-container');
if (eventContainer) {
const modal = document.getElementById('composition');
if (modal) {
modal.setAttribute('is-open', 'true');
modal.querySelector('fw-modal-title').innerHTML = `<span>Delete Event</span>`;
modal.querySelector('fw-modal-content').innerHTML = `
<span>Are you sure you want to delete this event?</span>
`;
modal.addEventListener(
'fwSubmit',
() => {
eventContainer.remove();
console.log("Event container successfully removed from DOM.");
if (Array.isArray(ticketworkflowconditions)) {
ticketworkflowconditions.splice(ruleIndex, 1);
console.log(`Event at index ${ruleIndex} successfully removed from ticketworkflowconditions array.`);
} else if (ticketworkflowconditions[ruleIndex]) {
delete ticketworkflowconditions[ruleIndex];
console.log(`Event at index ${ruleIndex} successfully removed from ticketworkflowconditions object.`);
}
console.log("Updated ticketworkflowconditions:", JSON.stringify(ticketworkflowconditions, null, 2));
showSuccessToast("Event successfully deleted!");
modal.removeAttribute('is-open');
},
{ once: true }
);
}
}
});
function updateIsEnabledState(selectedEventValue, isChecked) {
if (!selectedEventValue) {
console.error("Event is not selected.");
return;
}
let data = {
...ticketworkflowconditions[ruleIndex]
};
ticketworkflowconditions[ruleIndex] = {
...data,
checked: isChecked
};
console.log(`Toggle for ${selectedEventValue} set to ${isChecked ? 'enabled' : 'disabled'}.`);
console.log(`Updated ticketworkflowconditions after toggle change: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
// Dynamically update Add Button visibility after adding a dropdown
checkAddButtonVisibility();
});
// Delete Button Logics
deleteButton.addEventListener("click", () => {
console.log(Delete button clicked for mapping at index ${fieldIndex});
// Get modal
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = <span>Delete Mapping</span>;
modal.querySelector("fw-modal-content").innerHTML = <span>Are you sure you want to delete this field mapping?</span> ;
// Add event listener for the modal's "Yes" (submit) button
modal.addEventListener(
"fwSubmit",
() => {
if (fieldIndex === 0) {
console.log("Resetting the first field and clearing its choices.");
fieldDropdown.value = "";
fieldDropdown.setAttribute("placeholder", "Choose a field");
fieldDropdown.disabled = false;
choicesContainer.innerHTML = '<br><br><br><div class="temporary style" style="margin-bottom: 9px;"></div>';
choicesContainer.style.display = "block";
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
const combination = findRule.event.combination;
combination[fieldIndex] = { field: null };
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
}
modal.removeAttribute("is-open");
checkAddButtonVisibility();
return;
}
// For other fields, perform DELETE logic
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
const currentDropdowns = container.querySelectorAll(".field-name-dropdown");
const currentDropdownIndex = Array.from(currentDropdowns).indexOf(fieldDropdown);
if (currentDropdownIndex !== -1) {
const combination = findRule.event.combination;
combination.splice(currentDropdownIndex, 1); // Remove the combination at the correct index
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
}
}
// UI Cleanup: Remove field dropdown, choices, and delete button
fieldNameContainer.removeChild(fieldDropdown);
choicesContainer.innerHTML = ""; // Clear the choices container
buttonContainer.removeChild(deleteButtonWrapper);
modal.removeAttribute("is-open"); // Close the modal
// Dynamically recheck Add Button visibility after a field is deleted
checkAddButtonVisibility();
},
{ once: true } // Listener should execute only once
);
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open"); // Close modal
console.log("Delete operation was cancelled.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
});
// If all fields are selected, remove the Add Button
if (areAllFieldsSelected) {
console.log("All fields are selected. Hiding Add Button.");
if (addButton.parentNode) {
addButton.parentNode.appendChild(addButton);
addButton.disabled = false;
// addButton.style.visibility = "hidden";
}
} else {
console.log("Fields are still available. Add Button will be shown.");
if (dropdowns.length > 0 && lastDropdownWrapper) {
if (!lastDropdownWrapper.contains(addButton)) {
lastDropdownWrapper.appendChild(addButton);
addButton.style.visibility = "visible";
}
}
addButton.disabled = false;
}
}
if (selectedFieldName) {
console.log(Existing mapping detected for field: ${selectedFieldName});
const selectedField = fields.find((field) => field.name === selectedFieldName);
if (selectedField) {
console.log(Field: ${selectedField.label} (${selectedField.name}) is being preloaded with existing choices.);
// // Map choices and pre-select based on selectedChoices
// selectedField.choices.forEach((choice) => {
// const option = document.createElement('fw-select-option');
// option.setAttribute('value', choice.id);
// option.textContent = choice.value;
// // // Check if this choice is in the selectedChoices array
// // if (selectedChoices && selectedChoices.includes(String(choice.id))) {
// // option.setAttribute('selected', 'true');
// // console.log(Existing value mapped: ${choice.value} (ID: ${choice.id}) for field: ${selectedFieldName});
// // }
in this, make sure selectedChoices should be handled as arrray not as object
Certainly! To ensure that selectedChoices is consistently handled as an array throughout your code, we may need to adjust a few key areas. Here’s a step-by-step guide to making those adjustments:
Initialization of selectedChoices: Where you initially set selectedChoices, ensure that it begins as an array (even if it's empty) whenever it is created or used.
Modification of Objects: Wherever you handle selectedChoices, take care to ensure you're using it as an array (e.g., utilizing Array.isArray to check its type).
Updating JSON Structures: When constructing the new key-value pairs in ticketworkflowconditions or similar objects based on selectedChoices, make sure to always treat it as an array, inserting it as an array rather than an object.
Here is a portion of the modified code to reflect those changes:
Inside showingitschoices Method:
Ensure that selectedChoices is treated as an array.
Inside fieldschoosingvalues where you construct combination
When constructing new entry for combination, confirm it handles selectedChoices as an array.
const newKeyValue = { [selectedFieldName]: selectedChoices ? Array.isArray(selectedChoices) ? selectedChoices : [selectedChoices] : [] };
// This ensures selectedChoices is treated as an array, even if it is a single value.
Explanation of Changes:
The changes made have ensured that selectedChoices is consistently treated as an array across your code. By initiating it as an array when it defaults and by checking its type before performing operations on it, you minimize the risk of mistakenly treating it as an object or a single value.
By applying these adjustments, your application will be more robust and consistent in handling selectedChoices as an array, which aligns with your requirements.