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);
}
}
let fieldNameContainer = container.querySelector('.field-name-container');
let choicesContainer = container.querySelector('.choices-container');
let buttonContainer = container.querySelector('.button-container');
if (!fieldNameContainer) {
fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
container.appendChild(fieldNameContainer);
}
Assistant
User
please refix and give me final code
Assistant
if (!choicesContainer) {
choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
container.appendChild(choicesContainer);
}
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);
code
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
);
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);
const eventObj = ticketworkflowconditions.find(event => event.eventName === selectedEventValue) ||
{ eventName: selectedEventValue, isEnabled: true, combinations: [] };
// Always create a new combination each time
let combination = { fields: [] };
eventObj.combinations.push(combination);
for (const choice of selectedChoices) {
combination.fields.push({ name: selectedFieldName, value: choice });
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
if (!ticketworkflowconditions.includes(eventObj)) {
ticketworkflowconditions.push(eventObj);
}
} 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 i choose a set of fields and choices in same event, if i choose another dropdown wih same event, field and choices are choosed keep every set as combo
To address the issue where choosing a new field and choice for the same event is overwriting or not grouping selections correctly, you need to ensure each set of field and choice selections is maintained as a distinct combination. Let’s modify the relevant sections of your code to handle this scenario.
Store combinations uniquely: Ensure that each combination of fields and choices is maintained separately.
Update event handling: Enhance the event listeners to handle these changes without overwriting previous selections.
Here's a refined approach:
javascript
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
);
if (selectedFieldName) {
// Initialize containers for field and choices
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);
// Find or create event object
let eventObj = ticketworkflowconditions.find(event => event.eventName === selectedEventValue);
if (!eventObj) {
eventObj = { eventName: selectedEventValue, isEnabled: true, combinations: [] };
ticketworkflowconditions.push(eventObj);
}
// Create new combination for this selection
let combination = { fields: [] };
eventObj.combinations.push(combination);
for (const choice of selectedChoices) {
combination.fields.push({ name: selectedFieldName, value: choice });
// Initialize choices for the field
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
} else {
// Handle field selection without initial data
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);
}
}
// Create new combination when the choice changes
choicesDropdown.addEventListener('fwChange', (event) => {
const selectedChoiceId = parseInt(event.target.value);
if (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
let eventObj = ticketworkflowconditions.find(event => event.eventName === selectedEventValue);
if (!eventObj) {
eventObj = { eventName: selectedEventValue, isEnabled: true, combinations: [] };
ticketworkflowconditions.push(eventObj);
}
// Create or find an existing combination
let combination = eventObj.combinations.find(combo => combo.fields.some(field => field.name === selectedFieldName));
if (!combination) {
combination = { fields: [] };
eventObj.combinations.push(combination);
}
// Update the combination with the new choice
let fieldEntry = combination.fields.find(field => field.name === selectedFieldName);
if (!fieldEntry) {
fieldEntry = { name: selectedFieldName, value: selectedChoiceId };
combination.fields.push(fieldEntry);
} else {
fieldEntry.value = selectedChoiceId;
}
console.log('Updated ticketworkflowconditions:', JSON.stringify(ticketworkflowconditions, null, 2));
});
Key Changes:
Distinct Event Object Initialization: The code guarantees that each selectedEventValue has its own event object in ticketworkflowconditions.
Separate Combinations for Each Field and Choice Pair: Each pair of field and choice is stored in a separate combination, preventing them from overwriting each other.
Listeners & Combinations: Listeners now accommodate multi-combination scenarios by either updating existing combinations or creating new ones where necessary.
This approach should help maintain the integrity of each selection as a distinct combination within your workflow.
Certainly! Here's a refined version of your code that fixes the issue with managing combinations of fields and choices for events:
Separate Combinations: Each selection of field and choice is handled as a separate combination within the event object.
Correct Handling of Events and Combinations: The addition and deletion logic correctly updates the ticketworkflowconditions to maintain data integrity.
Listener Adjustments: Added checks to ensure each dropdown interaction properly updates or creates a new combination, without overwriting existing ones.
This final version should maintain each field and choice pair as unique combinations for each event, keeping your workflow stable and consistent.