async function addNewRule(eventName, fieldMappings) { const ticketworkflowContainer =…
- User
async function addNewRule(eventName, fieldMappings) { const ticketworkflowContainer = document.getElementById('ticketworkflow-container'); const eventContainer = document.createElement('div'); eventContainer.classList.add('event-container'); eventContainer.style.boxShadow = '0 5px 10px 0 rgba(0, 0, 0, 0.2)'; eventContainer.style.padding = '30px'; eventContainer.style.marginBottom = '15px'; eventContainer.style.borderRadius = '5px'; eventContainer.style.backgroundColor = '#fff';
const newDropdownSet = await createEventDropdownSet(eventName); eventContainer.appendChild(newDropdownSet);
ticketworkflowContainer.appendChild(eventContainer); await initializeEventDropdown(newDropdownSet, fieldMappings); }
async function createEventDropdownSet(eventName) { console.log("Creating Event Dropdown");
const newDropdownSet = document.createElement('div'); newDropdownSet.classList.add('row', 'ticketworkflow-dropdown');
const selectedEvent = typeof eventName === 'string' ? eventName : '';
newDropdownSet.innerHTML = <fw-select-option value="TicketCreate" {selectedEvent === "TicketUpdate" ? "selected" : ""}>Ticket Update <fw-select-option value="TicketCreateandUpdate" ${selectedEvent === "TicketCreateandUpdate" ? "selected" : ""}>Ticket Create and Update ;
const eventSelect = newDropdownSet.querySelector('.event-name-dropdown'); if (selectedEvent) { eventSelect.value = selectedEvent; }
eventSelect.addEventListener('fwChange', async () => { const newEvent = eventSelect.value;
if (!ticketworkflowconditions[newEvent]) { ticketworkflowconditions[newEvent] = { combinations: [] }; }
if (!ticketworkflowconditions[newEvent].combinations.length) { ticketworkflowconditions[newEvent].combinations.push({ isEnabled: true, fields: {} }); }
const fieldNameContainer = newDropdownSet.querySelector('.field-name-container'); const choicesContainer = newDropdownSet.querySelector('.choices-container'); const buttonContainer = newDropdownSet.querySelector('.button-container');
await fieldschoosingvalues(newEvent, fieldNameContainer, choicesContainer, buttonContainer, newDropdownSet);
console.log(
Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}); });return newDropdownSet; }
async function initializeEventDropdown(container, fieldMappings = null) { const eventSelect = container.querySelector('.event-name-dropdown'); const selectedEventValue = eventSelect.value;
console.log(initializeEventDropdown - Initial selected event: ${selectedEventValue});
let fieldNameContainer = container.querySelector('.field-name-container'); let choicesContainer = container.querySelector('.choices-container'); let buttonContainer = container.querySelector('.button-container'); let toggleContainer = container.querySelector('.toggle-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); }
if (!buttonContainer) { buttonContainer = document.createElement('div'); buttonContainer.classList.add('col-md-3', 'button-container'); buttonContainer.style.marginTop = '25px'; container.appendChild(buttonContainer); }
if (!toggleContainer) { toggleContainer = document.createElement('div'); toggleContainer.classList.add('col-md-2', 'toggle-container'); toggleContainer.style.marginLeft = 'auto'; toggleContainer.style.marginRight = '5px'; toggleContainer.style.marginTop = '30px'; container.appendChild(toggleContainer); }
toggleContainer.style.display = 'flex'; toggleContainer.style.flexDirection = 'column'; toggleContainer.style.alignItems = 'center';
const toggleSwitch = document.createElement('fw-toggle'); toggleSwitch.setAttribute('size', 'medium'); toggleSwitch.checked = fieldMappings?.combinations[0]?.isEnabled || false; toggleContainer.appendChild(toggleSwitch);
const deleteButton = document.createElement('fw-button'); deleteButton.textContent = 'Delete Event'; deleteButton.setAttribute('color', 'danger'); deleteButton.style.marginTop = '15px'; toggleContainer.appendChild(deleteButton);
if (fieldMappings && fieldMappings.combinations.length > 0) { const fields = fieldMappings.combinations[0].fields; for (const [fieldName, fieldValue] of Object.entries(fields)) { await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container, fieldName, [fieldValue]); } }
toggleSwitch.addEventListener('fwChange', (event) => { const isChecked = event.target.checked; updateDropdownAccessibility(isChecked); updateIsEnabledState(selectedEventValue, isChecked); });
deleteButton.addEventListener('click', () => { const eventContainer = container.closest('.event-container'); if (eventContainer) { eventContainer.remove(); delete ticketworkflowconditions[selectedEventValue]; console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions."); } });
updateIsEnabledState(selectedEventValue, toggleSwitch.checked); // Initial state capture
function updateDropdownAccessibility(isEnabled) { const dropdowns = container.querySelectorAll('.choices-dropdown, .field-name-dropdown, .event-name-dropdown'); dropdowns.forEach(dropdown => { dropdown.disabled = !isEnabled; }); }
function updateIsEnabledState(selectedEventValue, isChecked) { if (!selectedEventValue) { console.error("Event is not selected."); return; }
if (!ticketworkflowconditions[selectedEventValue]) { ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: isChecked, fields: {} }] }; }
let combination = ticketworkflowconditions[selectedEventValue].combinations[0]; combination.isEnabled = isChecked;
console.log(
Toggle for ${selectedEventValue} set to ${isChecked ? 'enabled' : 'disabled'}.); console.log(Updated ticketworkflowconditions after toggle change: ${JSON.stringify(ticketworkflowconditions, null, 2)}); } }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(Mapping field name: ${selectedFieldName}); console.log(Mapping choice value: ${selectedChoices});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) { showErrorToast("Select an event before proceeding."); return; }
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);
if (selectedFieldName && field.name === selectedFieldName) { option.setAttribute('selected', 'selected'); } });
fieldNameContainer.appendChild(fieldDropdown);
if (selectedFieldName) { const selectedField = fields.find(field => field.name === selectedFieldName); if (selectedField) { await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices); } else { showErrorToast('Field not found in Dropdown'); } }
fieldDropdown.addEventListener('fwChange', async (event) => { const selectedFieldName = event.target.value; choicesContainer.innerHTML = '';
const selectedField = fields.find(field => field.name === selectedFieldName); if (selectedField) { await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer); } else { showErrorToast('Field not found in Dropdown'); } }); }
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('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);
// 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);
addButton.addEventListener('click', () => { createAdditionalDropdowns(container); }); }
choicesDropdown.addEventListener('fwChange', (event) => { const selectedChoiceId = event.target.value;
if (!ticketworkflowconditions[selectedEventValue]) { ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: true, fields: {} }] }; }
let combination = ticketworkflowconditions[selectedEventValue].combinations[0]; combination.fields[selectedFieldName] = selectedChoiceId;
console.log(
Updated JSON with field: ${selectedFieldName}, choice: ${selectedChoiceId}); 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', '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');
const deleteButton = document.createElement('button'); deleteButton.textContent = 'Delete'; deleteButton.classList.add('btn', 'btn-danger', 'mt-2'); buttonContainer.appendChild(deleteButton);
container.appendChild(fieldNameContainer); container.appendChild(choicesContainer); container.appendChild(buttonContainer);
fieldschoosingvalues( container.querySelector('.event-name-dropdown').value, fieldNameContainer, choicesContainer, buttonContainer, container );
deleteButton.addEventListener('click', () => { fieldNameContainer.remove(); choicesContainer.remove(); buttonContainer.remove(); }); }
after mapping existing values to dropdown, add tha value to json dont add extra function add logic within this function
- Assistant
Your request involves integrating functionality to map existing values to dropdowns and updating a JSON object with the same logic directly within your existing function. Here's how you can accomplish that within the context of the provided code:
-
Integrate Mapping Directly in the Dropdown Setup: Avoid creating extra functions unless necessary, and ensure logic for updating the JSON (
ticketworkflowconditions) is neatly integrated. -
Update the
ticketworkflowconditionsJSON directly after assigning dropdown values: Ensure that whenever a value is mapped from dropdowns, it is immediately reflected in the JSON.
Below, adjustments are incorporated directly into your existing code:
// ... Existing code ... async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) { if (!selectedEventValue) { showErrorToast("Select an event before proceeding."); return; } 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); if (selectedFieldName && field.name === selectedFieldName) { option.setAttribute('selected', 'selected'); } }); fieldNameContainer.appendChild(fieldDropdown); if (selectedFieldName) { const selectedField = fields.find(field => field.name === selectedFieldName); if (selectedField) { await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices); // Directly update the JSON after mapping ticketworkflowconditions[selectedEventValue].combinations[0].fields[selectedFieldName] = selectedChoices ? selectedChoices[0] : null; console.log(`Updated ticketworkflowconditions with mapped fields: ${JSON.stringify(ticketworkflowconditions, null, 2)}`); } else { showErrorToast('Field not found in Dropdown'); } } fieldDropdown.addEventListener('fwChange', async (event) => { const selectedFieldName = event.target.value; choicesContainer.innerHTML = ''; const selectedField = fields.find(field => field.name === selectedFieldName); if (selectedField) { await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer); } else { showErrorToast('Field not found in Dropdown'); } }); } // ... Existing code ...Key Points:
-
Initial Mapping: When determining the initial field selection (
if (selectedFieldName) ...), directly insert the corresponding value intoticketworkflowconditions. -
Immediate JSON Update: Place the logic to update the JSON object immediately after processing dropdown selections within the event listeners.
This approach ensures your data state (
ticketworkflowconditions) remains synchronized with UI selections without needing additional methods or convoluted implementations. All the changes and selections are directly reflected in your main logic, maintaining code simplicity, and improving readability. -
- User
not working at all
- Assistant
To ensure the dropdown mapping and JSON updating work correctly, let's break down the code logic into a more detailed version, verifying that all steps align with the intended functionality. Here’s a revised approach with better structure and debugging information added for clarity:
Step 1: Creating Dropdowns and Mapping Values
Ensure you accurately create dropdowns and initialize them with existing values. You will also need to directly update the
ticketworkflowconditionsobject whenever a dropdown value is selected or changed.Step 2: Direct JSON Updates
Update the JSON (
ticketworkflowconditions) whenever a dropdown value is initialized or changed, making sure to directly integrate this logic into dropdown setup and event listeners.Revised Code Block
async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) { if (!selectedEventValue) { showErrorToast("Select an event before proceeding."); return; } 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); if (selectedFieldName && field.name === selectedFieldName) { option.setAttribute('selected', 'selected'); } }); fieldNameContainer.appendChild(fieldDropdown); if (selectedFieldName) { const selectedField = fields.find(field => field.name === selectedFieldName); if (selectedField) { await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices); // Update JSON directly with selected field and choice ticketworkflowconditions[selectedEventValue] = ticketworkflowconditions[selectedEventValue] || { combinations: [{}]}; ticketworkflowconditions[selectedEventValue].combinations[0].fields[selectedFieldName] = selectedChoices ? selectedChoices[0] : null; console.log(`Updated ticketworkflowconditions with mapped fields: ${JSON.stringify(ticketworkflowconditions, null, 2)}`); } else { showErrorToast('Field not found in Dropdown'); } } 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); } else { showErrorToast('Field not found in Dropdown'); } }); console.log("Field dropdown initialized:", fieldDropdown); }Key Adjustments:
- Dropdown Initialization: The dropdown is initialized and populated with options. Included logging to ensure the dropdown setup was successful.
- Direct JSON Update after Initialization: Directly update the
ticketworkflowconditionsJSON object right after the choices are set, both during initialization and upon changes. - Event Listener Setup: Handles new field selections and clear previous choices, reflecting the updated selection in
ticketworkflowconditions. - Error Handling: Ensure proper error messages and exception handling mechanisms to manage missing fields or selections.
This setup should allow for dynamic updating of your data model (
ticketworkflowconditions) based on the dropdown interactions. Make sure to replaceshowErrorToast,showingitschoices, and any custom methods with your own error handling and UI logic to complete the application integration. If errors persist, check your console logs for insights and confirmations. - User
toggle button mapped but not the fields and choices fix that
- Assistant
To fix the issue where fields and choices are not being mapped correctly even though the toggle button does, we need to ensure a few key aspects are correctly implemented:
-
Initialization of Fields and Choices: Make sure the fields and choices are being initialized correctly when the dropdown is created and when any selection changes occur.
-
Update Logic for JSON Object: Ensure that the JSON object (
ticketworkflowconditions) is updated correctly with the selected field and choice values whenever changes are made.
Here’s how you can adjust your code to achieve that:
Adjusting the Code for Fields and Choices Mapping
async function getFieldnamesfrom1stapi(fieldNameContainer, fields, choicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName = null, selectedChoices = null) { if (!selectedEventValue) { console.error("Select an event before proceeding."); return; } 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); // 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); // Update JSON directly with selected field and choice ticketworkflowconditions[selectedEventValue] = ticketworkflowconditions[selectedEventValue] || { combinations: [{ isEnabled: true, fields: {} }] }; ticketworkflowconditions[selectedEventValue].combinations[0].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'); } } 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); // Update JSON with newly selected field and choice const choiceValue = choicesContainer.querySelector('.choices-dropdown')?.value || null; ticketworkflowconditions[selectedEventValue].combinations[0].fields[newSelectedFieldName] = choiceValue; console.log(`Field changed: ${newSelectedFieldName}, Choice: ${choiceValue}`); console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`); } else { console.error('Field not found in Dropdown'); } }); } async function showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices = null) { choicesContainer.innerHTML = ''; // Clear previous choices 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); // Set the initial selected choice if provided if (selectedChoices && selectedChoices.includes(String(choice.id))) { option.setAttribute('selected', 'selected'); } }); choicesContainer.appendChild(choicesDropdown); // Add or update JSON object with choice selection on change choicesDropdown.addEventListener('fwChange', (event) => { const selectedChoiceId = event.target.value; ticketworkflowconditions[selectedEventValue].combinations[0].fields[selectedFieldName] = selectedChoiceId; console.log(`Choice changed: ${selectedChoiceId} for field: ${selectedFieldName}`); console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`); }); }Key Fixes:
-
Dropdown Attributes: Ensure the field and choices dropdowns have their initial selections set based on
selectedFieldNameandselectedChoices. -
JSON Update: Updates
ticketworkflowconditionsimmediately with field/choice changes through event listeners, directly reflecting any change in UI. -
Console Logging: Additional
console.log()statements added to help trace the flow of data and confirm the program’s execution path, which is crucial for real-time debugging. -
Event Handling: Make sure event listeners reflect logical steps for updating JSON with dropdown values.
Try these changes, and they should ensure the fields and choices are mapped correctly, similar to how your toggle button functionality is.
-
- User
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);
// 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);
addButton.addEventListener('click', () => { createAdditionalDropdowns(container); });}
choicesDropdown.addEventListener('fwChange', (event) => { const selectedChoiceId = event.target.value;
if (!ticketworkflowconditions[selectedEventValue]) { ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: true, fields: {} }] }; } let combination = ticketworkflowconditions[selectedEventValue].combinations[0]; combination.fields[selectedFieldName] = selectedChoiceId; console.log(`Updated JSON with field: ${selectedFieldName}, choice: ${selectedChoiceId}`); console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`);}); }
make changes in this file
- Assistant
Certainly! Let's ensure that the functionality of the
showingitschoicesfunction correctly handles the field choice dropdown and updates theticketworkflowconditionsobject effectively. Below are some refinements to make it robust and ensure that fields and choices are mapped and tracked correctly:async function showingitschoices( choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices = null ) { // Clear the previous contents of the choices container 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'); // Populate the choices dropdown with options selectedField.choices.forEach(choice => { const option = document.createElement('fw-select-option'); option.setAttribute('value', choice.id); option.textContent = choice.value; choicesDropdown.appendChild(option); // Set the option as selected if it matches the pre-selected choices if (selectedChoices && selectedChoices.includes(String(choice.id))) { option.setAttribute('selected', 'selected'); } }); // Append the populated choices dropdown to the choices container choicesContainer.appendChild(choicesDropdown); // Check if the 'Add' button already exists before adding it if (!buttonContainer.querySelector('.btn-primary')) { const addButton = document.createElement('button'); addButton.textContent = 'Add'; addButton.classList.add('btn', 'btn-primary', 'mt-2'); buttonContainer.appendChild(addButton); addButton.addEventListener('click', () => { createAdditionalDropdowns(container); }); } // Add event listener to update the JSON whenever a choice is selected choicesDropdown.addEventListener('fwChange', (event) => { const selectedChoiceId = event.target.value; // Initialize ticketworkflowconditions if it doesn't exist if (!ticketworkflowconditions[selectedEventValue]) { ticketworkflowconditions[selectedEventValue] = { combinations: [{ isEnabled: true, fields: {} }] }; } // Get the first combination object and update the fields with the selected choice let combination = ticketworkflowconditions[selectedEventValue].combinations[0]; combination.fields[selectedFieldName] = selectedChoiceId; // Log the update to the console for verification console.log(`Updated JSON with field: ${selectedFieldName}, choice: ${selectedChoiceId}`); console.log(`Updated ticketworkflowconditions: ${JSON.stringify(ticketworkflowconditions, null, 2)}`); }); }Key Changes and Explanations:
-
Container Clean-Up: Ensure the
choicesContainer.innerHTMLis cleared before re-populating to prevent duplications or persistent old data. -
Options Population and Selection:
- Create an
fw-select-optionfor each choice item. - Assign each option's
valueattribute thechoice.idand text content to thechoice.value. - Mark the option as
selectedif it matches pre-existing selections (selectedChoices).
- Create an
-
Prevent Redundant Button Creation: Verify if the "Add" button already exists (
!buttonContainer.querySelector('.btn-primary')) to prevent unnecessary duplicate buttons. -
Event Listener for Choice Changes:
- Ensure
ticketworkflowconditionsfor theselectedEventValueis initialized if it doesn't yet exist. - Update the relevant fields in the
ticketworkflowconditionswhenever a choice is selected. - Display console logs to verify the updates effectively.
- Ensure
Testing:
After implementing these changes, thoroughly test:
- Selecting different fields and choices updates
ticketworkflowconditionsas expected. - The initialization with
selectedChoicesbehaves as desired. - The "Add" button and any dynamic additions work seamlessly without duplication or interference.
Make sure to replace placeholder APIs and methods like
createAdditionalDropdownsandticketworkflowconditionswith your actual logic, and test rigorously in a real environment to confirm the expected behavior. -