// Log unmapped fields from alldata and show them in a modal
const unmappedFields = Object.keys(alldata.mandatory_fields).filter(
fieldName => !(fieldName in selectedMandatoryFields)
);
code
if (unmappedFields.length > 0) {
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = `<span>Unmapped Fields</span>`;
modal.querySelector("fw-modal-content").innerHTML = `
<ul>
${unmappedFields
.map(fieldName => `<li><strong>${fieldName}</strong>: ${alldata.mandatory_fields[fieldName]}</li>`)
.join('')}
</ul>
`;
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Unmapped fields modal closed.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
}
} else {
// showErrorToast('Data not available for Dropdown');
}
in this, show the unmapped value in a table inside the modal , also add a delete icon when i click delete icon, it should be removed from the json
Assistant
User
Assistant
To modify your code for displaying the unmapped fields in a table format inside the modal and adding a delete icon to each row that allows the deletion of the corresponding field from your JSON object, you can follow the revised code below.
Revised Code
javascript
// Log unmapped fields from alldata and show them in a modal
const unmappedFields = Object.keys(alldata.mandatory_fields).filter(
fieldName => !(fieldName in selectedMandatoryFields)
);
if (unmappedFields.length > 0) {
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = `<span>Unmapped Fields</span>`;
// Create a table for unmapped fields
modal.querySelector("fw-modal-content").innerHTML = `
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Value</th>
<th>Action</th>
</tr>
</thead>
<tbody>
${unmappedFields
.map(fieldName => `
<tr>
<td>${fieldName}</td>
<td>${alldata.mandatory_fields[fieldName]}</td>
<td>
<button class="delete-icon" data-field="${fieldName}">×</button>
</td>
</tr>`)
.join('')}
</tbody>
</table>
`;
// Add event listeners for delete icons
unmappedFields.forEach(fieldName => {
const deleteButton = modal.querySelector(`button[data-field="${fieldName}"]`);
deleteButton.addEventListener('click', () => {
// Remove the field from the alldata object
delete alldata.mandatory_fields[fieldName];
// Optionally update the modal content to reflect the change
console.log(`${fieldName} has been deleted from the mandatory fields.`);
// Refresh the modal content
// You might want to call this modal rendering block again to reflect the change
// Or close the modal if you want to just remove without further checks
});
});
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Unmapped fields modal closed.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
} else {
// showErrorToast('Data not available for Dropdown');
}
Changes Explained:
Table Structure: The content of the modal now includes a table with headers for field name, value, and action.
Delete Button: Each row has a delete button represented by a simple HTML entity (×). This button uses the data-field attribute to identify which field to delete.
Delete Logic: An event listener for each delete button is added, which deletes the corresponding field from the alldata.mandatory_fields object when clicked.
Refreshing the Modal: Whenever a field is deleted, the modal is left unchanged. If you'd like it to reflect the changes immediately, you might have to re-render the unmapped fields. This can be done by calling the same code block again that generates the modal content.
This approach provides an interactive way to handle unmapped fields, enabling users to remove unwanted fields easily.
// let selectedValues = {};
async function getMandatoryfields() {
// Log unmapped fields from alldata and show them in a modal
const unmappedFields = Object.keys(alldata.mandatory_fields).filter(
fieldName => !(fieldName in selectedMandatoryFields)
);
code
if (unmappedFields.length > 0) {
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = `<span>Unmapped Fields</span>`;
modal.querySelector("fw-modal-content").innerHTML = `
<ul>
${unmappedFields
.map(fieldName => `<li><strong>${fieldName}</strong>: ${alldata.mandatory_fields[fieldName]}</li>`)
.join('')}
</ul>
`;
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Unmapped fields modal closed.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
}
} else {
// showErrorToast('Data not available for Dropdown');
}
} catch (error) {
console.error("Error fetching data for Dropdown:", error);
}
}
add your logic in this code
To integrate the logic for displaying unmapped fields in a modal (in table format with delete functionality) within the provided getMandatoryFields function, you'll need to modify it accordingly. Below is the modified version of the code with the necessary changes:
Modified Code
javascript
// let selectedValues = {};
async function getMandatoryfields() {
try {
let statusresp = await makeApiCall(apiKeyInput2.value, domainInput2.value);
if (statusresp && statusresp.ticket_fields) {
const fieldsWithChoicesContainer = document.getElementById('fieldsWithChoicesContainer');
const fieldsWithoutChoicesContainer = document.getElementById('fieldsWithoutChoicesContainer');
if (!fieldsWithChoicesContainer || !fieldsWithoutChoicesContainer) {
return;
}
// Clear existing content
fieldsWithChoicesContainer.innerHTML = '';
fieldsWithoutChoicesContainer.innerHTML = '';
let hasFieldsWithChoices = false;
let hasFieldsWithoutChoices = false;
statusresp.ticket_fields.forEach(field => {
if (field.required_for_agents && field.field_type.startsWith('custom_')) {
const fieldName = field.label.toLowerCase();
if (field.field_type === 'custom_multi_select_dropdown') {
createMultiDropdownField(fieldsWithChoicesContainer, field, fieldName);
hasFieldsWithChoices = true;
} else if (field.field_type === 'custom_dropdown') {
createDropdownField(fieldsWithChoicesContainer, field);
hasFieldsWithChoices = true;
} else if (field.field_type === 'custom_decimal') {
createCustomDecimal(fieldsWithoutChoicesContainer, field, fieldName, alldata);
hasFieldsWithoutChoices = true;
} else if (field.field_type === 'custom_paragraph') {
createCustomParagraph(fieldsWithoutChoicesContainer, field, fieldName, alldata);
hasFieldsWithoutChoices = true;
} else if (field.field_type === 'custom_number') {
createCustomNumber(fieldsWithoutChoicesContainer, field, fieldName, alldata);
hasFieldsWithoutChoices = true;
} else if (field.field_type === 'custom_checkbox') {
createCustomCheckbox(fieldsWithoutChoicesContainer, field, fieldName, alldata);
hasFieldsWithoutChoices = true;
} else if (field.field_type === 'custom_date') {
createDatePicker(fieldsWithoutChoicesContainer, field, fieldName, alldata);
hasFieldsWithoutChoices = true;
} else {
createInputField(fieldsWithoutChoicesContainer, field, alldata);
hasFieldsWithoutChoices = true;
}
}
});
// Process fields with field_type as 'nested_field'
statusresp.ticket_fields.forEach(field => {
if (field.required_for_agents && field.field_type === 'nested_field') {
const fieldName = field.label.toLowerCase();
console.log("All data :", alldata);
createDependentDropdownField(fieldsWithChoicesContainer, field, fieldName, alldata);
hasFieldsWithChoices = true;
}
});
if (!hasFieldsWithChoices) {
document.getElementById('fieldsWithChoicesLabel').textContent = 'No Mandatory Fields with Choices';
}
if (!hasFieldsWithoutChoices) {
document.getElementById('fieldsWithoutChoicesLabel').textContent = 'No Mandatory Fields without Choices';
}
// Log unmapped fields from alldata and show them in a modal
const unmappedFields = Object.keys(alldata.mandatory_fields).filter(
fieldName => !(fieldName in selectedMandatoryFields)
);
if (unmappedFields.length > 0) {
const modal = document.getElementById("composition");
if (modal) {
modal.setAttribute("is-open", "true");
modal.querySelector("fw-modal-title").innerHTML = `<span>Unmapped Fields</span>`;
// Creating a table for unmapped fields
modal.querySelector("fw-modal-content").innerHTML = `
<table>
<thead>
<tr>
<th>Field Name</th>
<th>Value</th>
<th>Action</th>
</tr>
</thead>
<tbody>
${unmappedFields
.map(fieldName => `
<tr>
<td>${fieldName}</td>
<td>${alldata.mandatory_fields[fieldName]}</td>
<td>
<button class="delete-icon" data-field="${fieldName}">×</button>
</td>
</tr>`)
.join('')}
</tbody>
</table>
`;
// Add event listeners for delete icons
unmappedFields.forEach(fieldName => {
const deleteButton = modal.querySelector(`button[data-field="${fieldName}"]`);
deleteButton.addEventListener('click', () => {
// Remove the field from the alldata object
delete alldata.mandatory_fields[fieldName];
console.log(`${fieldName} has been deleted from the mandatory fields.`);
// Refresh the modal content after deletion
getMandatoryfields(); // Call the function again to reflect changes
});
});
modal.querySelector("fw-modal-footer").addEventListener(
"fwClose",
() => {
modal.removeAttribute("is-open");
console.log("Unmapped fields modal closed.");
},
{ once: true }
);
} else {
console.error("Modal element with ID 'composition' not found.");
}
}
} else {
// showErrorToast('Data not available for Dropdown');
}
} catch (error) {
console.error("Error fetching data for Dropdown:", error);
}
}
Key Changes Made:
Table Structure: The unmapped fields are now displayed in a table format with three columns: Field Name, Value, and Action (Delete).
Delete Functionality: Each row has a delete button represented by an "×" icon. An event listener is added to each delete button that removes the corresponding field from alldata.mandatory_fields.
Confirm Deletion and Refresh: After deleting a field, the getMandatoryfields() function is called again to refresh the content and update the modal without needing to manually close and reopen it.
Consistent Styling: The template for the modal content was kept consistent with your original formatting for ease of integration.
This setup allows users to see unmapped fields, delete them if needed, and have the UI immediately reflect changes, enhancing the usability of your modal interface.