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);
}
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;
}
if i choose multiple fields and choices in same events make add that into one field and make tht as a combo and if i choose another dropdown with same event show as another combo
To handle multiple fields and choices for the same event, grouping them as one "combination," and to reflect additional selections as different "combinations," we can modify the existing functionality. The idea is to create a structure where each event can have multiple combinations, each consisting of multiple fields.
Here's a modified approach to achieve this:
Maintain Combinations Effectively: Adjust how combinations are stored such that fields are grouped together under a single combination for an event. When a new combination is needed, create a new entry in the combinations list.
Event Listeners and Logic Adjustments: Carefully update event listeners and logic to support these new structures, ensuring that selections are correctly reflected within the combinations list.
Implementation Adjustments
Step 1: Update Combination Management
Modify how combinations are added and updated in the showingitschoices function. We want to check existing combinations and update them if necessary, otherwise, create new ones.
Code Updates:
Modify showingitschoices:
javascript
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 => {
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');
}
});
choicesContainer.appendChild(choicesDropdown);
if (buttonContainer.childNodes.length === 0) {
const addButton = document.createElement('fw-button');
addButton.setAttribute('color', 'primary');
addButton.textContent = 'Add';
buttonContainer.appendChild(addButton);
const deleteFieldButton = document.createElement('fw-button');
deleteFieldButton.setAttribute('color', 'warning');
deleteFieldButton.textContent = 'Delete';
buttonContainer.appendChild(deleteFieldButton);
addButton.addEventListener('click', () => {
createAdditionalDropdowns(container);
});
deleteFieldButton.addEventListener('click', () => {
fieldNameContainer.remove();
choicesContainer.remove();
buttonContainer.remove();
// Remove the related fields and associated combinations
let eventObj = ticketworkflowconditions.find(event => event.eventName === selectedEventValue);
if (eventObj) {
eventObj.combinations = eventObj.combinations.filter(combo => !combo.fields.some(field => field.name === selectedFieldName));
console.log(`Deleted field: ${selectedFieldName} from event: ${selectedEventValue}`);
}
});
}
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) ||
{ eventName: selectedEventValue, isEnabled: true, combinations: [] };
// Finding an existing combination to add field to, or create a new combination
let combination = eventObj.combinations.find(combo => combo.fields.some(field => field.name === selectedFieldName));
if (!combination) {
combination = { fields: [] };
eventObj.combinations.push(combination);
}
// Ensure the field is updated or added properly
let field = combination.fields.find(f => f.name === selectedFieldName);
if (field) {
field.value = selectedChoiceId;
} else {
field = { name: selectedFieldName, value: selectedChoiceId };
combination.fields.push(field);
}
if (!ticketworkflowconditions.includes(eventObj)) {
ticketworkflowconditions.push(eventObj);
}
console.log('Updated ticketworkflowconditions:', JSON.stringify(ticketworkflowconditions, null, 2));
});
}
Step 2: Create Additional Dropdowns
When creating additional dropdowns, ensure they result in a new combination being created only if needed or when explicitly desired by user action (e.g., clicking "Add" button).
This setup should ensure that each event is able to maintain multiple distinct combinations, while every set of multiple fields selected together forms a single combination, facilitating clear and accurate workflow behavior modeling.
To ensure that a new combination is created every time you add fields and choices under the same event, we'll need to modify the logic to explicitly handle how combinations are stored. Instead of attempting to match or update existing combinations, we should always add a new entry for a new set of selections.
Let's refine the logic to ensure that each time you choose new fields and choices with the same event, it creates a unique "combination."
Here's a more refined approach:
Store Combinations Independently: Every time you create fields and choose values, even if it's under the same event name, ensure it's stored independently as a separate combination.
Indicate New Combinations: Use user actions, like clicking an "Add New Combination" button, to distinctly segregate combinations.
Let's modify a few parts of the original implementation to reflect these:
Revised Implementation
Step 1: Update the fieldschoosingvalues to Create New Combinations Always
Ensure that every call creates a new combination for fields, regardless of existing ones.
Code Updates:
Keep the creation of combinations straightforward, always starting fresh for each new event configuration set.
Ensure new combinations are pushed to the eventObj.combinations every time.
javascript
const ticketworkflowconditions = [];
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("fieldschoosingvalues called with an undefined selectedEventValue.");
return;
}
try {
const statusResp = await makeApiCall(apiKeyInput1.value, domainInput1.value);
if (statusResp && statusResp.ticket_fields) {
const fields = statusResp.ticket_fields.filter(field =>
field.field_type.startsWith('default_') &&
field.choices &&
field.choices.length > 0 &&
field.required_for_agents
);
// Creating a new field container and choices container for each field set
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);
// Initialize new combination entry
if (selectedFieldName && selectedChoices) {
const combination = { fields: [] };
for (const choice of selectedChoices) {
combination.fields.push({ name: selectedFieldName, value: choice });
// Render the dropdowns with pre-selected values
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
// Add this new combination to the event
const eventObj = ticketworkflowconditions.find(event => event.eventName === selectedEventValue);
if(eventObj) {
eventObj.combinations.push(combination);
} else {
ticketworkflowconditions.push({
eventName: selectedEventValue,
isEnabled: true,
combinations: [combination]
});
}
} else {
// Render dropdowns with defaults
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);
}
}
Step 2: Managing Multiple Combinations
To manage the creation of multiple combinations under the same event, ensure that any new addition through the UI creates an independent entry.
Step 3: UI Handling with Add Button
Provide a clear way to start a new combination via UI action, such as clicking an "Add New Combination" button.
javascript
function createAdditionalDropdowns(container) {
// Prepare new containers for the additional dropdown
const fieldCombinationContainer = document.createElement('div');
fieldCombinationContainer.classList.add('field-combination-container');
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
fieldCombinationContainer.appendChild(fieldNameContainer);
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
fieldCombinationContainer.appendChild(choicesContainer);
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-3', 'button-container', 'mt-3');
fieldCombinationContainer.appendChild(buttonContainer);
container.appendChild(fieldCombinationContainer);
// Call fieldschoosingvalues to initialize new dropdown set and create a new event combination each time
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
fieldCombinationContainer
);
}
This setup ensures that whenever you add new fields and choices under the same event name, it creates a separate logical grouping (combination), and maintains the previous choices as distinct combinations. The UI should clearly reflect this through user actions such as explicit addition of combinations.