// // Delete Combo Logics (working Version)
// deleteButton.addEventListener('click', () => {
// console.log(Delete button clicked for mapping at index ${fieldIndex});
// // Check if the field is the first dropdown (special case)
// if (fieldIndex === 0) {
// console.log("Resetting the first field and clearing its choices.");
// fieldDropdown.value = ""; // Clear the value
// fieldDropdown.setAttribute("placeholder", "Choose a field");
// fieldDropdown.disabled = false;
// console.log(
// Backend JSON updated after resetting first field: ${JSON.stringify(ticketworkflowconditions, null, 2)}
// );
// }
// console.log("First field and choices cleared, but position retained.");
// return; // Exit cleanup logic
// }
// // For all other fields, do normal deletion
// const findRule = ticketworkflowconditions[ruleIndex];
// if (findRule) {
// const combination = findRule.event.combination;
// // Remove the field from the backend combination array
// combination.splice(fieldIndex, 1);
// console.log(
// Backend JSON updated after resetting first field: ${JSON.stringify(ticketworkflowconditions, null, 2)}
// );
// }
// console.log("First field and choices cleared, but position retained.");
// return; // Exit cleanup logic
// }
// // Normal deletion for other fields
// const findRule = ticketworkflowconditions[ruleIndex];
// if (findRule) {
// // Find the current index of the dropdown in the backend combination
// const currentDropdowns = container.querySelectorAll('.field-name-dropdown');
// const currentDropdownIndex = Array.from(currentDropdowns).indexOf(fieldDropdown); // Re-evaluate fieldIndex dynamically
// if (currentDropdownIndex !== -1) {
// const combination = findRule.event.combination;
// combination.splice(currentDropdownIndex, 1); // Remove the correct index from the backend array
// if (remainingDropdowns.length === 0) {
// // No remaining dropdowns—ensure Add button is in the buttonContainer
// console.log("No remaining dropdowns, placing Add button in existing container.");
// if (!buttonContainer.querySelector('.add-button')) {
// addButton.disabled = false; // Enable Add button
// buttonContainer.appendChild(addButton);
// }
// } else {
// // Append the Add button to the last dropdown's button container
// console.log("Dropdowns still exist—placing Add button next to the last dropdown.");
// const lastDropdownWrapper = remainingDropdowns[remainingDropdowns.length - 1].closest('.button-container');
// if (lastDropdownWrapper && !lastDropdownWrapper.contains(addButton)) {
// lastDropdownWrapper.appendChild(addButton);
// }
// }
// console.log(Field at index ${fieldIndex} deleted successfully.);
// });
deleteButton.addEventListener('click', () => {
console.log(Delete button clicked for mapping at index ${fieldIndex});
// Special case: Reset the first field and retain the choices container
if (fieldIndex === 0) {
console.log("Resetting the first field and clearing its choices.");
// Reset the first dropdown
fieldDropdown.value = ""; // Clear value
fieldDropdown.setAttribute("placeholder", "Choose a field");
fieldDropdown.disabled = false; // Allow user to reselect
choicesContainer.innerHTML = ""; // Clear inner choices, but retain the container
choicesContainer.style.display = "block"; // Ensure it's still visible
// Update backend JSON
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
const combination = findRule.event.combination;
combination[fieldIndex] = { field: null }; // Reset only the field itself
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
console.log(
`Backend JSON updated after resetting the first field: ${JSON.stringify(ticketworkflowconditions, null, 2)}`
);
}
console.log("First field reset and choices cleared, but position retained.");
return; // Exit here as we don't want to fully delete/remove the first dropdown or choices container
}
// Normal deletion for other fields
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
// Find the dynamic index of the dropdown in the backend combination
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 backend combination at correct index
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination }
};
console.log(`Backend updated after deletion: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
} else {
console.error(`Dropdown not found in the container. Backend update skipped.`);
}
}
// Remove field dropdown, choices, and delete button (UI cleanup for NON-first fields)
fieldNameContainer.removeChild(fieldDropdown);
choicesContainer.innerHTML = ''; // Clear the choices
buttonContainer.removeChild(deleteButtonWrapper);
// Handle remaining dropdowns in the UI
const remainingDropdowns = container.querySelectorAll('.field-name-dropdown');
if (remainingDropdowns.length === 0) {
// Ensure Add button is in the buttonContainer when no dropdowns are left
console.log("No remaining dropdowns, placing Add button in existing container.");
if (!buttonContainer.querySelector('.add-button')) {
addButton.disabled = false; // Enable the Add button
buttonContainer.appendChild(addButton);
}
} else {
// Add button to the last dropdown's button container
console.log("Dropdowns still exist—placing Add button next to the last dropdown.");
const lastDropdownWrapper = remainingDropdowns[remainingDropdowns.length - 1].closest('.button-container');
if (lastDropdownWrapper && !lastDropdownWrapper.contains(addButton)) {
lastDropdownWrapper.appendChild(addButton);
}
}
console.log(`Field at index ${fieldIndex} deleted successfully.`);
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 the mapping for <strong>${rowData.merchantStatus}</strong> and <strong>${rowData.vendorStatus}</strong>?</span> ;
// Add listener to delete mapping
modal.addEventListener('fwSubmit', () => {
delete mappings[rowData.id];
renderStatusMappingTable();
showSuccessToast("Selected Status Mapping has been Deleted!");
modal.removeAttribute('is-open');
}, { once: true });
}
},
like this in this if i click yes, then execute
Adding a modal for confirming deletion is a good user experience practice to ensure the user does not unintentionally delete the mapping. Here’s how you can integrate your modal into the provided deleteButton click handler. If the user clicks "Yes," we'll execute the deletion logic. The updated function is provided below:
Updated Code
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 it's the first field in the list, reset instead of deleting
if (fieldIndex === 0) {
console.log("Resetting the first field and clearing its choices.");
fieldDropdown.value = ""; // Clear value
fieldDropdown.setAttribute("placeholder", "Choose a field");
fieldDropdown.disabled = false; // Allow user to reselect
choicesContainer.innerHTML = ""; // Clear inner choices, but retain the container
choicesContainer.style.display = "block"; // Ensure it's visible
// Update backend JSON
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
const combination = findRule.event.combination;
combination[fieldIndex] = { field: null }; // Reset only the field itself
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
console.log(
`Backend JSON updated after resetting the first field: ${JSON.stringify(
ticketworkflowconditions,
null,
2
)}`
);
}
console.log("First field reset and choices cleared, but position retained.");
modal.removeAttribute('is-open'); // Close the modal
return; // Exit early for the first field
}
// For other fields, perform DELETE logic
const findRule = ticketworkflowconditions[ruleIndex];
if (findRule) {
// Find the dynamic index of the dropdown in the backend combination
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 backend combination at the correct index
ticketworkflowconditions[ruleIndex] = {
...findRule,
event: { ...findRule.event, combination },
};
console.log(`Backend updated after deletion: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
} else {
console.error("Dropdown not found in the container. Backend update skipped.");
}
}
// UI Cleanup: Remove field dropdown, choices, and delete button
fieldNameContainer.removeChild(fieldDropdown);
choicesContainer.innerHTML = ""; // Clear the choices
buttonContainer.removeChild(deleteButtonWrapper);
// Handle remaining dropdowns in the UI
const remainingDropdowns = container.querySelectorAll(".field-name-dropdown");
if (remainingDropdowns.length === 0) {
// Ensure Add button is in the buttonContainer when no dropdowns are left
console.log("No remaining dropdowns, placing Add button in existing container.");
if (!buttonContainer.querySelector(".add-button")) {
addButton.disabled = false; // Enable the Add button
buttonContainer.appendChild(addButton);
}
} else {
// Add button to the last dropdown's button container
console.log("Dropdowns still exist—placing Add button next to the last dropdown.");
const lastDropdownWrapper = remainingDropdowns[remainingDropdowns.length - 1].closest(".button-container");
if (lastDropdownWrapper && !lastDropdownWrapper.contains(addButton)) {
lastDropdownWrapper.appendChild(addButton);
}
}
console.log(`Field at index ${fieldIndex} deleted successfully.`);
modal.removeAttribute('is-open'); // Close the modal
},
{ once: true } // Ensure the listener is executed only once
);
// Add event listener to close the modal if "No" is clicked
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.");
}
});
Explanation of Changes
Modal Integration:
The modal is obtained using document.getElementById('composition'), ensuring its existence.
The title (fw-modal-title) and content (fw-modal-content) are dynamically updated with relevant information.
Added event listeners for:
fwSubmit: Executes the deletion/reset logic if the "Yes" button is clicked.
fwClose: Cancels the delete operation if the "No" button is clicked, which closes the modal.
Special Case Handling (First Dropdown):
If the field being deleted is the first one, it is reset instead of being removed to retain the UI structure (as per your existing logic). The modal is still used for confirmation.
Normal Field Deletion:
If it's not the first field, the dropdown, choices, and delete button are removed from the DOM, and the backend JSON is updated accordingly.
Once Event Listeners:
Used { once: true } to ensure that the listeners are executed only once and automatically removed after the first interaction.
Workflow:
When the delete button is clicked, the modal is displayed.
If the user clicks "Yes" in the modal:
The deletion/reset logic is executed.
The UI is updated accordingly.
Backend JSON (ticketworkflowconditions) is updated.
If the user clicks "No," the modal simply closes without making any changes.
Additional Note:
Make sure that you include the modal's HTML fw-modal markup in your DOM before invoking this logic or dynamically attach it to the DOM. Let me know if you'd need code for creating the modal dynamically!