async function addNewRule(eventName, fieldMappings) {
// Get the container where the workflow will be appended
const ticketworkflowContainer = document.getElementById('ticketworkflow-container');
// Create new dropdown set for the event
const newDropdownSet = document.createElement('div');
newDropdownSet.classList.add('row', 'ticketworkflow-dropdown');
// Reset ticketworkflowconditions for new event
if (newEvent !== selectedEvent) {
ticketworkflowconditions = { [newEvent]: { combinations: [{ isEnabled: true, fields: {} }] } };
// Clear existing field-related containers
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
// Create containers for the fields, choices, and buttons
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
newDropdownSet.appendChild(fieldNameContainer);
const choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
newDropdownSet.appendChild(choicesContainer);
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-3', 'button-container');
buttonContainer.style.marginTop = '25px';
newDropdownSet.appendChild(buttonContainer);
// Populate dropdowns with the newly selected event using the existing logic
await fieldschoosingvalues(newEvent, fieldNameContainer, choicesContainer, buttonContainer, newDropdownSet);
console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
}
Assistant
});
// Append the newDropdownSet to the eventContainer
eventContainer.appendChild(newDropdownSet);
// Finally, append the eventContainer to the main ticketworkflow container
ticketworkflowContainer.appendChild(eventContainer);
}
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
if (!selectedEventValue) {
console.error("fieldschoosingvalues called with an undefined selectedEventValue.");
return; // Early exit
}
try {
let 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
);
console.log(`Starting fieldschoosingvalues with selectedEventValue: ${selectedEventValue}`);
if (selectedFieldName) {
const fieldContainer = document.createElement('div');
fieldContainer.classList.add('field-container');
fieldNameContainer.appendChild(fieldContainer);
const choicesDiv = document.createElement('div');
choicesDiv.classList.add('choices-container');
choicesContainer.appendChild(choicesDiv);
await getFieldnamesfrom1stapi(fieldContainer, fields, choicesDiv, selectedEventValue, buttonContainer, container, selectedFieldName, selectedChoices);
} 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("Select an event before proceeding.");
return;
}
// Set the initial selected field if provided
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
}
});
fieldNameContainer.appendChild(fieldDropdown);
// Initial mapping of choices in case a field is already selected
if (selectedFieldName) {
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices);
// Ensure that ticketworkflowconditions structure is initialized for the event.
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = { combinations: [] }; // Initialize if not present
}
// Get the latest combination or create a new one if none exists
const combinationsArray = ticketworkflowconditions[selectedEventValue].combinations;
let latestCombination;
if (combinationsArray.length > 0) {
// Get the latest combination
latestCombination = combinationsArray[combinationsArray.length - 1];
} else {
// If no combination exists, create and push a new one
latestCombination = { isEnabled: true, fields: {} };
combinationsArray.push(latestCombination);
}
// Update the selected field in the latest combination's fields
// Ensure previous values are preserved
latestCombination.fields[selectedFieldName] = selectedChoices ? selectedChoices[0] : null;
console.log(`Updated ticketworkflowconditions with mapped fields: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
} else {
console.error('Field not found in Dropdown');
}
}
// Update logic for field change event
fieldDropdown.addEventListener('fwChange', async (event) => {
const newSelectedFieldName = event.target.value;
choicesContainer.innerHTML = '';
const newSelectedField = fields.find(field => field.name === newSelectedFieldName);
if (newSelectedField) {
await showingitschoices(choicesContainer, newSelectedField, selectedEventValue, newSelectedFieldName, buttonContainer, container, fieldNameContainer);
// Get the latest combination for the selected event
const latestCombination = ticketworkflowconditions[selectedEventValue].combinations[ticketworkflowconditions[selectedEventValue].combinations.length - 1];
// Update JSON with newly selected field and choice in the latest combination
const choiceValue = choicesContainer.querySelector('.choices-dropdown')?.value || null;
if (latestCombination) {
latestCombination.fields[newSelectedFieldName] = choiceValue; // Add to the latest combination fields
} else {
console.error("No latest combination found for updating fields.");
}
console.log(`Field changed: ${newSelectedFieldName}, Choice: ${choiceValue}`);
console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
} else {
console.error('Field not found in Dropdown');
}
if (selectedChoices && selectedChoices.includes(String(choice.id))) {
option.setAttribute('selected', 'selected');
}
});
choicesContainer.appendChild(choicesDropdown);
// Check if the button has already been added
if (!buttonContainer.querySelector('.btn-primary')) {
const addButton = document.createElement('button');
addButton.textContent = 'Add';
addButton.classList.add('btn', 'btn-primary', 'mt-2');
buttonContainer.appendChild(addButton);
// Ensure the event is initialized in ticketworkflowconditions
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: true, fields: {} }] };
}
// Get the latest combination
const latestCombination = ticketworkflowconditions[selectedEventValue].combinations[
ticketworkflowconditions[selectedEventValue].combinations.length - 1
];
// Update the latest combination's fields with the selected choice
latestCombination.fields[selectedFieldName] = selectedChoiceId;
console.log(`Updated JSON with field: ${selectedFieldName}, choice: ${selectedChoiceId}`);
console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);
});
}
remove existing value mapping
and change object to array
To remove existing value mapping and change the data structure from an object to an array for ticketworkflowconditions, you will need to change how you store and manage the conditions in your code. Instead of using an object where each event is a key, you will use an array of objects, where each object represents a condition corresponding to an event.
Here's how you can modify your functions accordingly:
ticketworkflowconditions = []; // This will hold events as an array of objects
Modifications in Functions
Here’s how you can update your main functions:
Change ticketworkflowconditions to an array and modify how you check for existing combinations and add new ones.
Update the logic for managing combinations when adding new events.
Example Code Changes
Below are the key modifications to the original provided functions:
1. Modify addNewRule
async function addNewRule(eventName, fieldMappings) {
// ... existing code ...
const selectedEvent = typeof eventName === 'string' ? eventName : '';
// Define a new rule for the event if it doesn't exist in the array.
let existingRule = ticketworkflowconditions.find(rule => rule.event === selectedEvent);
if (!existingRule) {
existingRule = { event: selectedEvent, combinations: [{ isEnabled: true, fields: {} }] };
ticketworkflowconditions.push(existingRule);
}
// Clear existing field-related containers
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
// ... remaining logic ...
}
2. Update fieldschoosingvalues
Change how you check for existing combinations:
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, selectedChoices = null) {
// ... existing code ...
// Get or initialize the conditions for the selected event
let existingRule = ticketworkflowconditions.find(rule => rule.event === selectedEventValue);
if (!existingRule) {
existingRule = { event: selectedEventValue, combinations: [{ isEnabled: true, fields: {} }] };
ticketworkflowconditions.push(existingRule);
}
// ... remaining logic ...
}
3. Update Inserting Combinations
For instance, within getFieldnamesfrom1stapi, you may want to adjust the logic for pushing to the combinations array:
let combinationsArray = existingRule.combinations;
let latestCombination = combinationsArray[combinationsArray.length - 1];
if (!latestCombination) {
latestCombination = { isEnabled: true, fields: {} };
combinationsArray.push(latestCombination);
}
// Ensure to update the fields as before based on your logic
latestCombination.fields[selectedFieldName] = selectedChoices ? selectedChoices[0] : null;
// ... remaining code ...
4. Update Event Handlers
Ensure the relevant event handlers continue working with the array structure. You may need to change how you access or modify data based on this new structure in multiple functions.
Conclusion
By switching from an object-based structure to an array-based one, you must ensure that every access point to ticketworkflowconditions respects this format. This might involve adjusting how you search for conditions, handle updates, and remove mappings in different sections of your code. Always validate how data is being modified after these changes to ensure everything operates smoothly.