async function loadExistingWorkflowConditions() {
console.log("Mapping Data for Workflows:", alldata.ticket_workflow_conditions);
for (const [eventName, fieldMappings] of Object.entries(alldata.ticket_workflow_conditions)) {
console.log('Loading rule for event:', eventName);
await addNewRule(eventName, fieldMappings);
}
}
async function addNewRule(eventName, fieldMappings) {
let fieldNameContainer = container.querySelector('.field-name-container');
let choicesContainer = container.querySelector('.choices-container');
let buttonContainer = container.querySelector('.button-container');
Assistant
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);
}
// Add "Delete Event" button if it doesn't already exist
if (!buttonContainer.querySelector('.delete-event-button')) {
const deleteEventButton = document.createElement('fw-button');
deleteEventButton.classList.add('delete-event-button');
deleteEventButton.setAttribute('color', 'danger');
deleteEventButton.textContent = 'Delete Event';
buttonContainer.appendChild(deleteEventButton);
deleteEventButton.addEventListener('click', () => {
const eventContainer = container.closest('.event-container');
if (eventContainer) {
eventContainer.remove();
delete ticketworkflowconditions[eventSelect.value];
console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions.");
}
});
}
// Handle existing mappings
if (fieldMappings) {
for (const [selectedFieldName, selectedChoices] of Object.entries(fieldMappings)) {
console.log(Mapping existing value: Event - ${eventSelect.value}, Field - ${selectedFieldName}, Choices - ${selectedChoices});
for (const choice of selectedChoices) {
await fieldschoosingvalues(
eventSelect.value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
}
}
eventSelect.addEventListener('fwChange', async function () {
const selectedEventValue = eventSelect.value;
console.log(Event changed to: ${selectedEventValue});
fieldNameContainer.innerHTML = '';
choicesContainer.innerHTML = '';
buttonContainer.innerHTML = '';
if (!selectedEventValue) {
console.error("Selected event value is undefined or empty.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container);
});
}
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("fieldschoosingvalues called with an undefined selectedEventValue.");
return;
}
try {
let statusResp = await makeApiCall(apiKeyInput1.value, domainInput1.value);
if (statusResp && statusResp.ticket_fields) {
let fields = statusResp.ticket_fields.filter(field =>
field.field_type.startsWith('default_') &&
field.choices &&
field.choices.length > 0 &&
field.required_for_agents
);
console.log(`Starting fieldschoosingvalues with selectedEventValue: ${selectedEventValue}`);
console.log(`Starting field name to map: ${selectedFieldName}`);
console.log(`Starting choice name to map: ${selectedChoices}`);
if (selectedFieldName) {
const individualFieldContainer = document.createElement('div');
individualFieldContainer.classList.add('field-container');
fieldNameContainer.appendChild(individualFieldContainer);
const individualChoicesContainer = document.createElement('div');
individualChoicesContainer.classList.add('choices-container');
choicesContainer.appendChild(individualChoicesContainer);
for (const choice of selectedChoices) {
console.log(`Mapping existing choice ${choice} to dropdown for field ${selectedFieldName} in event ${selectedEventValue}`);
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(choice)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(choice);
console.log(`Added choice ${choice} to ticketworkflowconditions for field ${selectedFieldName} under event ${selectedEventValue}`);
} else {
console.log(`Choice ${choice} already exists for field ${selectedFieldName} under event ${selectedEventValue}`);
}
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
} else {
await getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container);
}
} else {
console.error('No fields found or data not available');
}
} catch (error) {
console.error("Error fetching data for Dropdown:", error);
}
}
async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("getFieldnamesfrom1stapi called with an undefined selectedEventValue.");
return;
}
console.log(Initializing field dropdown for event ${selectedEventValue}, field: ${selectedFieldName});
if (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
console.log(`Added choice ID ${selectedChoiceId} to field ${selectedFieldName} for event ${selectedEventValue}`);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
console.log('Updated ticketworkflowconditions:', JSON.stringify(ticketworkflowconditions, null, 2));
});
}
function createAdditionalDropdowns(container) {
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
fieldNameContainer.classList.add('offset-md-2');
// Call fieldschoosingvalues for this additional dropdown
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container
);
}
in this add this feature :
if i choosed ticket create, field-status, choices-open, and when i click additional dropdown remove status from the dropdown to show remaining dropdowns only
To implement the feature where selecting a field and its choice should remove them from the available options in additional dropdowns, you can modify your code. Here's a step-by-step approach to achieve that behavior:
Maintain Track of Used Fields and Choices: Create a data structure (e.g., Set) to keep track of the fields and choices already selected in the current context.
Filter Options Based on Selection: Update the logic to filter out already selected fields and choices from new dropdowns.
Update Dropdowns Dynamically: Adjust the creation of new dropdowns to factor in previously selected options.
Here's the modified code with those changes:
let usedFields = new Set();
let usedChoices = new Set();
// update your 'fieldschoosingvalues' to take used fields and choices into account
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("fieldschoosingvalues called with an undefined selectedEventValue.");
return;
}
try {
let statusResp = await makeApiCall(apiKeyInput1.value, domainInput1.value);
if (statusResp && statusResp.ticket_fields) {
let fields = statusResp.ticket_fields.filter(field =>
field.field_type.startsWith('default_') &&
field.choices &&
field.choices.length > 0 &&
field.required_for_agents
);
// Filter fields based on used fields
fields = fields.filter(field => !usedFields.has(field.name));
if (selectedFieldName) {
// If a field is pre-selected, ensure it gets utilized and removed from availability
usedFields.add(selectedFieldName);
const individualFieldContainer = document.createElement('div');
individualFieldContainer.classList.add('field-container');
fieldNameContainer.appendChild(individualFieldContainer);
const individualChoicesContainer = document.createElement('div');
individualChoicesContainer.classList.add('choices-container');
choicesContainer.appendChild(individualChoicesContainer);
for (const choice of selectedChoices) {
usedChoices.add(choice); // Add these choices to used choices
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
} else {
await getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container);
}
} else {
console.error('No fields found or data not available');
}
} catch (error) {
console.error("Error fetching data for Dropdown:", error);
}
}
async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("getFieldnamesfrom1stapi called with an undefined selectedEventValue.");
return;
}
// Initialize field dropdown
const firstDropdown = document.createElement('fw-select');
firstDropdown.classList.add('field-name-dropdown');
firstDropdown.setAttribute('label', 'Ticket Fields');
firstDropdown.setAttribute('required', 'true');
firstDropdown.setAttribute('placeholder', 'Choose a field');
firstDropdown.setAttribute('hint-text', 'Select singular option');
fields.forEach(field => {
if (!usedFields.has(field.name)) { // Check if this field is already used
const option = document.createElement('fw-select-option');
option.setAttribute('value', field.name);
option.textContent = field.label;
firstDropdown.appendChild(option);
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
}
}
});
fieldNameContainer.appendChild(firstDropdown);
if (selectedFieldName) {
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices);
}
}
firstDropdown.addEventListener('fwChange', async (event) => {
const selectedFieldName = event.target.value;
choicesContainer.innerHTML = '';
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
// Add to the set of used fields
usedFields.add(selectedFieldName);
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer);
} else {
console.error('Selected Field not found in fields.');
}
});
}
async function showingitschoices(
choicesContainer,
selectedField,
selectedEventValue,
selectedFieldName,
buttonContainer,
container,
fieldNameContainer,
selectedChoices = null
) {
choicesContainer.innerHTML = '';
const choicesDropdown = document.createElement('fw-select');
choicesDropdown.classList.add('choices-dropdown');
choicesDropdown.setAttribute('label', `${selectedFieldName}`);
choicesDropdown.setAttribute('required', 'true');
choicesDropdown.setAttribute('placeholder', `Choose ${selectedFieldName}`);
choicesDropdown.setAttribute('hint-text', 'Select singular option');
selectedField.choices.forEach(choice => {
if (!usedChoices.has(choice.id)) { // Ensure this choice isn't used
const option = document.createElement('fw-select-option');
option.setAttribute('value', choice.id);
option.textContent = choice.value;
choicesDropdown.appendChild(option);
if (selectedChoices && selectedChoices.includes(String(choice.id))) {
option.setAttribute('selected', 'selected');
usedChoices.add(choice.id); // Add pre-selected to used choices
}
}
});
choicesContainer.appendChild(choicesDropdown);
choicesDropdown.addEventListener('fwChange', (event) => {
const selectedChoiceId = event.target.value;
// Ensure this choice isn't re-used
if (!usedChoices.has(selectedChoiceId)) {
usedChoices.add(selectedChoiceId);
}
if (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
console.log('Updated ticketworkflowconditions:', JSON.stringify(ticketworkflowconditions, null, 2));
});
}
function createAdditionalDropdowns(container) {
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
fieldNameContainer.classList.add('offset-md-2');
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-3', 'button-container', 'mt-3');
container.appendChild(fieldNameContainer);
container.appendChild(choicesContainer);
container.appendChild(buttonContainer);
// Call `fieldschoosingvalues` for this additional dropdown
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container
);
}
Explanation:
Used Fields and Choices Tracking: Both usedFields and usedChoices are sets that keep track of fields and choices that are already selected. They get updated whenever a user selects an item.
Filtering Dropdown Options: When creating or updating the dropdowns, the code now filters out options that are already in the usedFields or usedChoices.
Dynamic Updates: Event listeners are used to update the sets whenever a user interacts with the dropdowns, ensuring no duplicates are available in subsequent dropdowns.
With these changes, when you select a field and choice, they aren't shown in any new added dropdown. This way, users won't be able to re-select fields and choices already chosen in the current workflow setup.