// Use 'let' to allow reassignment of selectedEvent
let selectedEvent = typeof eventName === 'string' ? eventName : ''; // Declaring selectedEvent here with 'let'
console.log("Previous Selected Event:", selectedEvent); // Log the previous selected event
console.log("New Event Name:", newEvent);
const previousEventIndex = workflowConditions.findIndex(cond => cond.eventName === selectedEvent);
if (previousEventIndex !== -1) {
workflowConditions.splice(previousEventIndex, 1);
console.log(`Removed existing condition for event: "${selectedEvent}"`);
}
// Clear existing field-related containers
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
// Create a new condition for the newly selected event
const newCondition = { eventName: newEvent, isEnabled: true, fields: {} };
workflowConditions.push(newCondition);
console.log(`Added new condition for: "${newEvent}"`);
// Create new containers for fields and choices
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 fields based on the new event's context
await fieldschoosingvalues(newEvent, fieldNameContainer, choicesContainer, buttonContainer, newDropdownSet, null, null);
// Log the updated state of workflowConditions
console.log(`Updated workflowConditions: ${JSON.stringify(workflowConditions, null, 2)}`);
// Update the selected event after the change
selectedEvent = newEvent; // Reassign selectedEvent here
});
// Append the newDropdownSet to the eventContainer
eventContainer.appendChild(newDropdownSet);
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;
}
try {
let statusResp = await makeApiCall(apiKeyInput1.value, domainInput1.value);
} 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;
}
code
const fieldDropdown = document.createElement('fw-select');
fieldDropdown.classList.add('field-name-dropdown');
fieldDropdown.setAttribute('label', 'Ticket Fields');
fieldDropdown.setAttribute('required', 'true');
fieldDropdown.setAttribute('placeholder', 'Choose a field');
fieldDropdown.setAttribute('hint-text', 'Select singular option');
fields.forEach(field => {
const option = document.createElement('fw-select-option');
option.setAttribute('value', field.name);
option.textContent = field.label;
fieldDropdown.appendChild(option);
// Select the current selected field if available
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
}
});
fieldNameContainer.appendChild(fieldDropdown);
if (selectedFieldName) {
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
// Send the selected field and its choices to showingitschoices for mapping
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices);
} else {
console.error('Field not found in Dropdown');
}
}
// Handle field change event and pass selected field to showingitschoices
fieldDropdown.addEventListener('fwChange', async (event) => {
const newSelectedFieldName = event.target.value;
// Find the event condition for the selected event name
const eventCondition = ticketworkflowconditions.find(cond => cond.eventName === selectedEventValue);
if (eventCondition) {
const previousFieldName = selectedFieldName; // Store the previous selected field name
// Log the previous field name for debugging
console.log(`Previous field name: ${previousFieldName}`);
if (previousFieldName && previousFieldName !== newSelectedFieldName) {
// Don't replace, just add new values
if (!eventCondition.fields) {
eventCondition.fields = {};
}
eventCondition.fields[newSelectedFieldName] = null; // Add the new field with a placeholder value (null)
console.log(`Added new field "${newSelectedFieldName}" to event condition.`);
}
// Clear the choices container before adding new choices
choicesContainer.innerHTML = '';
const newSelectedField = fields.find(field => field.name === newSelectedFieldName);
if (newSelectedField) {
// Send the newly selected field and its choices to showingitschoices for mapping
await showingitschoices(choicesContainer, newSelectedField, selectedEventValue, newSelectedFieldName, buttonContainer, container, fieldNameContainer);
} else {
console.error('Field not found in Dropdown');
}
}
});
}
for each mapping of event, it should send respective fields object to run getfieldsfrom1stapi and mapp existing value
Sure! It looks like you want the addNewRule function to dynamically manage and map fields based on the selected event while preserving the existing mappings. To ensure that existing values for mapped fields persist when changing the event, modifications will have to be made in several parts of your existing code. Below are steps and the updated code to achieve this.
Key Changes
Retain Existing Field Mappings: When changing events, save the current field states, so that they can be remapped when the new selections are made.
Send Existing Fields for Each Mapping: When a new field is selected, make sure to check if any existing field mappings need to be updated or preserved.
Updated Code
Here is the modified version of your code, focused on the addNewRule and getFieldnamesfrom1stapi functions:
javascript
async function addNewRule(eventName, workflowConditions) {
// ... existing code ...
const eventSelect = newDropdownSet.querySelector('.event-name-dropdown');
eventSelect.addEventListener('fwChange', async () => {
const newEvent = eventSelect.value;
// Retrieve the event condition if it exists
const existingCondition = workflowConditions.find(cond => cond.eventName === selectedEvent);
if (existingCondition) {
// Store current field mappings to maintain state
const existingFields = existingCondition.fields || {};
console.log(`Existing fields for event "${selectedEvent}":`, existingFields);
}
const previousEventIndex = workflowConditions.findIndex(cond => cond.eventName === selectedEvent);
if (previousEventIndex !== -1) {
workflowConditions.splice(previousEventIndex, 1);
console.log(`Removed existing condition for event: "${selectedEvent}"`);
}
// Clear existing field-related containers
newDropdownSet.querySelectorAll('.field-name-container, .choices-container, .button-container').forEach(container => container.remove());
// Create a new condition for the newly selected event
const newCondition = { eventName: newEvent, isEnabled: true, fields: existingFields || {} };
workflowConditions.push(newCondition);
console.log(`Added new condition for: "${newEvent}"`);
// Create new containers for fields and choices
// ... existing code for creating containers ...
// Populate fields based on the new event's context, passing existing fields as well
await fieldschoosingvalues(newEvent, fieldNameContainer, choicesContainer, buttonContainer, newDropdownSet, null, existingFields);
// Log the updated state of workflowConditions
console.log(`Updated workflowConditions: ${JSON.stringify(workflowConditions, null, 2)}`);
// Update the selected event after the change
selectedEvent = newEvent; // Reassign selectedEvent here
});
// ... existing code ...
}
async function fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName = null, existingFields = {}) {
// ... existing code ...
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
);
await getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName, existingFields);
} 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, existingFields = {}) {
// ... existing code ...
fields.forEach(field => {
const option = document.createElement('fw-select-option');
option.setAttribute('value', field.name);
option.textContent = field.label;
fieldDropdown.appendChild(option);
// Select the current selected field if available and exist in existingFields.
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
}
});
fieldNameContainer.appendChild(fieldDropdown);
if (selectedFieldName || (existingFields[selectedFieldName] && fields.some(field => field.name === selectedFieldName))) {
// If there are existing fields, send them for mapping
const selectedFieldNameToCheck = selectedFieldName || Object.keys(existingFields)[0]; // Check existing fields
const selectedField = fields.find(field => field.name === selectedFieldNameToCheck);
if (selectedField) {
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldNameToCheck, buttonContainer, container, fieldNameContainer, existingFields[selectedFieldNameToCheck]);
} else {
console.error('Field not found in Dropdown');
}
}
// Handle field change event
// ... existing event handling code ...
}
Explanation of Changes
Retaining Existing Fields: When switching events, we keep the existing fields and send that as context to the fieldschoosingvalues function.
Mapping Existing Values: When mapping fields, we check if there's already a mapping in the existingFields and supply appropriate values into the dropdown.
This setup ensures that when an event is changed, previously selected fields and their values can be carried over and intelligently managed without losing previously defined rules.