my code should allow multiple selections of same event show as combo for ex like this
"ticket_workflow_conditions": [
{
"eventName": "Ticket Create",
"combinations": [
{
"isEnabled": true,
"fields": [
{ "name": "Priority", "value": 2 },
{ "name": "Status", "value": 3 }
]
},
{
"isEnabled": true,
"fields": [
{ "name": "Priority", "value": 3 },
{ "name": "Status", "value": 5 }
]
}
]
},
{
"eventName": "Ticket Update",
"combinations": [
{
"isEnabled": true,
"fields": [
{ "name": "Priority", "value": 4 },
{ "name": "Status", "value": 4 }
]
}
]
}
]
}
can you add a check in this. for example i choosed ticket create event, field status and i choosed its choices, if i click add and it should show the remaining options from the above choosed
To implement a check in your code that ensures you can select multiple combinations for the same event and remove any already selected combinations from the available options, follow these steps:
Track Selected Combinations:
Keep a list to track the combinations that have been selected for each event.
Determine Remaining Combinations:
For the currently selected event, exclude combinations that have been previously selected when presenting new choices.
Code Implementation:
This can be implemented in JavaScript (assuming a web-based interface). Here's a high-level approach:
ticketWorkflowConditions: Contains all possible event combinations.
selectedCombinations: An object that tracks the combinations that have been selected for each event type.
addCombination: Adds a selected field combination to the selectedCombinations for a specific event.
getAvailableCombinations: Filters the combinations to only leave those that haven't been selected yet.
This setup tracks what combinations have been chosen and filters out those when adding new choices, ensuring you can only choose available options that haven't been selected yet. Adjust the logic and presentation to fit your specific user interface and use case.
async function 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);
}
if (!choicesContainer) {
choicesContainer = document.createElement('div');
choicesContainer.classList.add('col-md-2', 'choices-container');
container.appendChild(choicesContainer);
}
// Add "Delete Event" button if it doesn't already exist
if (!buttonContainer.querySelector('.delete-event-button')) {
const deleteEventButton = document.createElement('fw-button');
deleteEventButton.classList.add('delete-event-button');
deleteEventButton.setAttribute('color', 'danger');
deleteEventButton.textContent = 'Delete Event';
buttonContainer.appendChild(deleteEventButton);
code
deleteEventButton.addEventListener('click', () => {
const eventContainer = container.closest('.event-container');
if (eventContainer) {
eventContainer.remove();
delete ticketworkflowconditions[eventSelect.value];
console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions.");
}
});
}
// Handle existing mappings
if (fieldMappings) {
for (const [selectedFieldName, selectedChoices] of Object.entries(fieldMappings)) {
console.log(Mapping existing value: Event - ${eventSelect.value}, Field - ${selectedFieldName}, Choices - ${selectedChoices});
for (const choice of selectedChoices) {
await fieldschoosingvalues(
eventSelect.value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
}
}
eventSelect.addEventListener('fwChange', async function () {
const selectedEventValue = eventSelect.value;
console.log(Event changed to: ${selectedEventValue});
code
fieldNameContainer.innerHTML = '';
choicesContainer.innerHTML = '';
buttonContainer.innerHTML = '';
if (!selectedEventValue) {
console.error("Selected event value is undefined or empty.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container);
});
}
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
);
console.log(`Starting fieldschoosingvalues with selectedEventValue: ${selectedEventValue}`);
console.log(`Starting field name to map: ${selectedFieldName}`);
console.log(`Starting choice name to map: ${selectedChoices}`);
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);
for (const choice of selectedChoices) {
console.log(`Mapping existing choice ${choice} to dropdown for field ${selectedFieldName} in event ${selectedEventValue}`);
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(choice)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(choice);
console.log(`Added choice ${choice} to ticketworkflowconditions for field ${selectedFieldName} under event ${selectedEventValue}`);
} else {
console.log(`Choice ${choice} already exists for field ${selectedFieldName} under event ${selectedEventValue}`);
}
await getFieldnamesfrom1stapi(
individualFieldContainer,
fields,
individualChoicesContainer,
selectedEventValue,
buttonContainer,
container,
selectedFieldName,
[choice]
);
}
} 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 (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
console.log(`Added choice ID ${selectedChoiceId} to field ${selectedFieldName} for event ${selectedEventValue}`);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
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');
fieldNameContainer.classList.add('offset-md-2');
// Call fieldschoosingvalues for this additional dropdown
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container
);
}
make changes in this code
To ensure your code handles cases where the user adds new selections and can track already selected fields and their choices, we’ll make adjustments based on your requirements. Here's how we can achieve the desired functionality:
Tracking Selected Fields and Choices: We will enhance your code to keep track of previously selected fields and choices for each event.
Refining Add and Delete Functionalities: Ensure that when a new combination is added, it doesn't duplicate existing selections. Also, refine the remove functionality to correctly update the stored conditions.
Here's the revised version of your code with the necessary changes:
javascript
let ticketworkflowconditions = {};
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 = `
<div class="col-md-2 event-name-container">
<fw-select
class="event-name-dropdown"
label="Select Event Name"
required="true"
placeholder="Your choice"
hint-text="Select singular option">
<fw-select-option value="TicketCreate" ${selectedEvent === "TicketCreate" ? "selected" : ""}>Ticket Create</fw-select-option>
<fw-select-option value="TicketUpdate" ${selectedEvent === "TicketUpdate" ? "selected" : ""}>Ticket Update</fw-select-option>
<fw-select-option value="TicketCreateandUpdate" ${selectedEvent === "TicketCreateandUpdate" ? "selected" : ""}>Ticket Create and Update</fw-select-option>
</fw-select>
</div>
`;
const eventSelect = newDropdownSet.querySelector('.event-name-dropdown');
if (selectedEvent) {
eventSelect.value = selectedEvent;
}
return newDropdownSet;
}
async function initializeEventDropdown(container, fieldMappings = null) {
const eventSelect = container.querySelector('.event-name-dropdown');
console.log(`initializeEventDropdown - Initial selected event: ${eventSelect.value}`);
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);
}
if (!buttonContainer) {
buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-3', 'button-container');
buttonContainer.style.marginTop = '25px';
container.appendChild(buttonContainer);
}
if (!buttonContainer.querySelector('.delete-event-button')) {
const deleteEventButton = document.createElement('fw-button');
deleteEventButton.classList.add('delete-event-button');
deleteEventButton.setAttribute('color', 'danger');
deleteEventButton.textContent = 'Delete Event';
buttonContainer.appendChild(deleteEventButton);
deleteEventButton.addEventListener('click', () => {
const eventContainer = container.closest('.event-container');
if (eventContainer) {
eventContainer.remove();
delete ticketworkflowconditions[eventSelect.value];
console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions.");
}
});
}
if (fieldMappings) {
for (const [selectedFieldName, selectedChoices] of Object.entries(fieldMappings)) {
console.log(`Mapping existing value: Event - ${eventSelect.value}, Field - ${selectedFieldName}, Choices - ${selectedChoices}`);
for (const choice of selectedChoices) {
await fieldschoosingvalues(eventSelect.value, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName, [choice]);
}
}
}
eventSelect.addEventListener('fwChange', async function () {
const selectedEventValue = eventSelect.value;
console.log(`Event changed to: ${selectedEventValue}`);
fieldNameContainer.innerHTML = '';
choicesContainer.innerHTML = '';
buttonContainer.innerHTML = '';
if (!selectedEventValue) {
console.error("Selected event value is undefined or empty.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container);
});
}
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(`Starting field name to map: ${selectedFieldName}`);
console.log(`Starting choice name to map: ${selectedChoices}`);
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);
for (const choice of selectedChoices) {
console.log(`Mapping existing choice ${choice} to dropdown for field ${selectedFieldName} in event ${selectedEventValue}`);
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(choice)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(choice);
console.log(`Added choice ${choice} to ticketworkflowconditions for field ${selectedFieldName} under event ${selectedEventValue}`);
} else {
console.log(`Choice ${choice} already exists for field ${selectedFieldName} under event ${selectedEventValue}`);
}
await getFieldnamesfrom1stapi(individualFieldContainer, fields, individualChoicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName, [choice]);
}
} 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}`);
const firstDropdown = document.createElement('fw-select');
firstDropdown.classList.add('field-name-dropdown');
firstDropdown.setAttribute('label', 'Ticket Fields');
firstDropdown.setAttribute('required', 'true');
firstDropdown.setAttribute('placeholder', 'Choose a field');
firstDropdown.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;
firstDropdown.appendChild(option);
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
console.log(`Pre-selected field: ${selectedFieldName}`);
}
});
fieldNameContainer.appendChild(firstDropdown);
if (selectedFieldName) {
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices);
}
}
firstDropdown.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 {
console.error('Selected Field not found in fields.');
}
});
}
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');
console.log(`Pre-selected choice: ${choice.id} for field: ${selectedFieldName}`);
}
});
choicesContainer.appendChild(choicesDropdown);
if (buttonContainer.childNodes.length === 0) {
// Create the Add button
const addButton = document.createElement('fw-button');
addButton.setAttribute('color', 'primary');
addButton.textContent = 'Add';
buttonContainer.appendChild(addButton);
// Create the Delete button for individual fields and choices
const deleteFieldButton = document.createElement('fw-button');
deleteFieldButton.setAttribute('color', 'warning');
deleteFieldButton.textContent = 'Delete';
buttonContainer.appendChild(deleteFieldButton);
if (container.querySelectorAll('.choices-container').length === 1 && !buttonContainer.querySelector('.delete-event-button')) {
const deleteEventButton = document.createElement('fw-button');
deleteEventButton.classList.add('delete-event-button');
deleteEventButton.setAttribute('color', 'danger');
deleteEventButton.textContent = 'Delete Event';
buttonContainer.appendChild(deleteEventButton);
deleteEventButton.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.');
} else {
console.error('Event container not found for deletion.');
}
});
}
addButton.addEventListener('click', () => {
createAdditionalDropdowns(container);
});
deleteFieldButton.addEventListener('click', () => {
fieldNameContainer.remove();
choicesContainer.remove();
buttonContainer.remove();
delete ticketworkflowconditions[selectedEventValue][selectedFieldName];
console.log(`Delete button clicked. Removed field: ${selectedFieldName} and its choices.`);
});
}
choicesDropdown.addEventListener('fwChange', (event) => {
const selectedChoiceId = event.target.value;
if (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
console.log(`Added choice ID ${selectedChoiceId} to field ${selectedFieldName} for event ${selectedEventValue}`);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
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');
fieldNameContainer.classList.add('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');
container.appendChild(fieldNameContainer);
container.appendChild(choicesContainer);
container.appendChild(buttonContainer);
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container
);
}
Key Changes Made:
Tracking and Updating State: The ticketworkflowconditions object is updated whenever a selection is made or removed, ensuring the current state is accurately represented.
Dynamic Element Creation: Functions ensure that new elements (dropdowns, buttons) are dynamically created and inserted into the DOM just once per field, eliminating duplicates.
Enhanced Event Listeners: Listeners are added for managing both field and event changes, maintaining the correctness of selections and states.
Make sure to adjust this code further based on your environment, especially for any integrations with APIs or existing systems.
To address the issues you're encountering and create a working solution, we'll need to break down the requirements, ensure that we properly manage the state, and implement DOM manipulation correctly. Let me guide you through simplified steps to achieve this.
Key Requirements
Dynamic Event Selection: Allow users to select an event from a dropdown.
Field and Choice Management: Present fields and choices related to the event, and track user selections.
Maintain Workflow Conditions: Update and manage ticketworkflowconditions to reflect changes in selections.
Simplified and Corrected Example
Here's an example that incorporates these requirements:
Modular Code Structure: The code is divided into functions that each handle a specific task. This improves readability and maintainability.
State Management: The ticketworkflowconditions object keeps track of selections. It's updated whenever a user makes a choice.
DOM Manipulation: Dynamic elements (dropdowns, buttons) are created and inserted only when needed.
Event Handling: Event listeners are correctly attached to ensure selections trigger the right actions, and additional dropdowns can be added dynamically.
Ensure you replace placeholder logic in processFieldValues with actual code to interface with your backend. If further problems persist, please always check for console errors, as they can point out specific pieces missing from this setup tailored to your environment.
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);
}
deleteEventButton.addEventListener('click', () => {
const eventContainer = container.closest('.event-container');
if (eventContainer) {
eventContainer.remove();
delete ticketworkflowconditions[eventSelect.value];
console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions.");
}
});
}
if (fieldMappings) {
for (const [selectedFieldName, selectedChoices] of Object.entries(fieldMappings)) {
console.log(Mapping existing value: Event - ${eventSelect.value}, Field - ${selectedFieldName}, Choices - ${selectedChoices});
for (const choice of selectedChoices) {
await fieldschoosingvalues(eventSelect.value, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName, [choice]);
}
}
}
eventSelect.addEventListener('fwChange', async function () {
const selectedEventValue = eventSelect.value;
console.log(Event changed to: ${selectedEventValue});
code
fieldNameContainer.innerHTML = '';
choicesContainer.innerHTML = '';
buttonContainer.innerHTML = '';
if (!selectedEventValue) {
console.error("Selected event value is undefined or empty.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container);
});
}
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
);
console.log(`Starting fieldschoosingvalues with selectedEventValue: ${selectedEventValue}`);
console.log(`Starting field name to map: ${selectedFieldName}`);
console.log(`Starting choice name to map: ${selectedChoices}`);
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);
for (const choice of selectedChoices) {
console.log(`Mapping existing choice ${choice} to dropdown for field ${selectedFieldName} in event ${selectedEventValue}`);
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(choice)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(choice);
console.log(`Added choice ${choice} to ticketworkflowconditions for field ${selectedFieldName} under event ${selectedEventValue}`);
} else {
console.log(`Choice ${choice} already exists for field ${selectedFieldName} under event ${selectedEventValue}`);
}
await getFieldnamesfrom1stapi(individualFieldContainer, fields, individualChoicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName, [choice]);
}
} 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 (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
console.log(`Added choice ID ${selectedChoiceId} to field ${selectedFieldName} for event ${selectedEventValue}`);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
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');
fieldNameContainer.classList.add('offset-md-2');
on additional mapping you should remove add button
To remove the "Add" button when creating additional field mappings, you need to adjust the logic so that the button is only added when appropriate. Here is the modified version of your code that ensures the "Add" button is only present where needed:
Key Adjustments:
Add Button Presence: The "Add" button should only be available with the initial dropdown set for an event. Once additional mappings are created, we don't add another "Add" button.
javascript
let ticketworkflowconditions = {};
async function initializeEventDropdown(container, fieldMappings = null) {
const eventSelect = container.querySelector('.event-name-dropdown');
console.log(`initializeEventDropdown - Initial selected event: ${eventSelect.value}`);
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);
}
if (!buttonContainer) {
buttonContainer = document.createElement('div');
buttonContainer.classList.add('col-md-3', 'button-container');
buttonContainer.style.marginTop = '25px';
container.appendChild(buttonContainer);
}
if (!buttonContainer.querySelector('.delete-event-button')) {
const deleteEventButton = document.createElement('fw-button');
deleteEventButton.classList.add('delete-event-button');
deleteEventButton.setAttribute('color', 'danger');
deleteEventButton.textContent = 'Delete Event';
buttonContainer.appendChild(deleteEventButton);
deleteEventButton.addEventListener('click', () => {
const eventContainer = container.closest('.event-container');
if (eventContainer) {
eventContainer.remove();
delete ticketworkflowconditions[eventSelect.value];
console.log("Delete Event button clicked. Event removed from DOM and ticketworkflowconditions.");
}
});
}
if (fieldMappings) {
for (const [selectedFieldName, selectedChoices] of Object.entries(fieldMappings)) {
console.log(`Mapping existing value: Event - ${eventSelect.value}, Field - ${selectedFieldName}, Choices - ${selectedChoices}`);
for (const choice of selectedChoices) {
await fieldschoosingvalues(eventSelect.value, fieldNameContainer, choicesContainer, buttonContainer, container, selectedFieldName, [choice]);
}
}
}
eventSelect.addEventListener('fwChange', async function () {
const selectedEventValue = eventSelect.value;
console.log(`Event changed to: ${selectedEventValue}`);
fieldNameContainer.innerHTML = '';
choicesContainer.innerHTML = '';
buttonContainer.innerHTML = '';
if (!selectedEventValue) {
console.error("Selected event value is undefined or empty.");
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
await fieldschoosingvalues(selectedEventValue, fieldNameContainer, choicesContainer, buttonContainer, container);
});
}
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(`Starting field name to map: ${selectedFieldName}`);
console.log(`Starting choice name to map: ${selectedChoices}`);
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);
for (const choice of selectedChoices) {
console.log(`Mapping existing choice ${choice} to dropdown for field ${selectedFieldName} in event ${selectedEventValue}`);
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(choice)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(choice);
console.log(`Added choice ${choice} to ticketworkflowconditions for field ${selectedFieldName} under event ${selectedEventValue}`);
} else {
console.log(`Choice ${choice} already exists for field ${selectedFieldName} under event ${selectedEventValue}`);
}
await getFieldnamesfrom1stapi(individualFieldContainer, fields, individualChoicesContainer, selectedEventValue, buttonContainer, container, selectedFieldName, [choice]);
}
} 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}`);
const firstDropdown = document.createElement('fw-select');
firstDropdown.classList.add('field-name-dropdown');
firstDropdown.setAttribute('label', 'Ticket Fields');
firstDropdown.setAttribute('required', 'true');
firstDropdown.setAttribute('placeholder', 'Choose a field');
firstDropdown.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;
firstDropdown.appendChild(option);
if (selectedFieldName && field.name === selectedFieldName) {
option.setAttribute('selected', 'selected');
console.log(`Pre-selected field: ${selectedFieldName}`);
}
});
fieldNameContainer.appendChild(firstDropdown);
if (selectedFieldName) {
const selectedField = fields.find(field => field.name === selectedFieldName);
if (selectedField) {
await showingitschoices(choicesContainer, selectedField, selectedEventValue, selectedFieldName, buttonContainer, container, fieldNameContainer, selectedChoices);
}
}
firstDropdown.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 {
console.error('Selected Field not found in fields.');
}
});
}
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');
console.log(`Pre-selected choice: ${choice.id} for field: ${selectedFieldName}`);
}
});
choicesContainer.appendChild(choicesDropdown);
// Remove "Add" button creation outside this function
const addButtonExists = container.querySelector('.add-field-btn');
const isInitialDropdownSet = container.querySelectorAll('.field-name-container').length <= 1;
if (!addButtonExists && isInitialDropdownSet) {
const addButton = createAddButton(container);
buttonContainer.appendChild(addButton);
}
const deleteFieldButton = createDeleteFieldButton(container, fieldNameContainer, choicesContainer, buttonContainer, selectedEventValue, selectedFieldName);
buttonContainer.appendChild(deleteFieldButton);
choicesDropdown.addEventListener('fwChange', (event) => {
const selectedChoiceId = event.target.value;
if (!selectedEventValue || !selectedFieldName) {
console.error('Event or field name is undefined');
return;
}
if (!ticketworkflowconditions[selectedEventValue]) {
ticketworkflowconditions[selectedEventValue] = {};
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName]) {
ticketworkflowconditions[selectedEventValue][selectedFieldName] = [];
}
if (!ticketworkflowconditions[selectedEventValue][selectedFieldName].includes(selectedChoiceId)) {
ticketworkflowconditions[selectedEventValue][selectedFieldName].push(selectedChoiceId);
console.log(`Added choice ID ${selectedChoiceId} to field ${selectedFieldName} for event ${selectedEventValue}`);
} else {
console.warn(`Choice ID ${selectedChoiceId} already exists in ${selectedFieldName}`);
}
console.log('Updated ticketworkflowconditions:', JSON.stringify(ticketworkflowconditions, null, 2));
});
}
function createAddButton(container) {
const addButton = document.createElement('fw-button');
addButton.classList.add('add-field-btn');
addButton.setAttribute('color', 'primary');
addButton.textContent = 'Add';
addButton.addEventListener('click', () => {
addButton.remove(); // Remove the "Add" button when clicked, to ensure it does not duplicate
createAdditionalDropdowns(container);
});
return addButton;
}
function createDeleteFieldButton(container, fieldNameContainer, choicesContainer, buttonContainer, selectedEventValue, selectedFieldName) {
const deleteFieldButton = document.createElement('fw-button');
deleteFieldButton.setAttribute('color', 'warning');
deleteFieldButton.textContent = 'Delete';
deleteFieldButton.addEventListener('click', () => {
fieldNameContainer.remove();
choicesContainer.remove();
buttonContainer.remove();
if (selectedEventValue && selectedFieldName && ticketworkflowconditions[selectedEventValue]) {
delete ticketworkflowconditions[selectedEventValue][selectedFieldName];
console.log(`Delete button clicked. Removed field: ${selectedFieldName} and its choices.`);
}
});
return deleteFieldButton;
}
function createAdditionalDropdowns(container) {
const fieldNameContainer = document.createElement('div');
fieldNameContainer.classList.add('col-md-2', 'field-name-container');
fieldNameContainer.classList.add('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');
container.appendChild(fieldNameContainer);
container.appendChild(choicesContainer);
container.appendChild(buttonContainer);
fieldschoosingvalues(
container.querySelector('.event-name-dropdown').value,
fieldNameContainer,
choicesContainer,
buttonContainer,
container
);
}
Key Changes
Add Button Logic: If there is an "Add" button for a set, it will be removed once clicked, avoiding duplication.
Button Creation and Condition: Now only checks for initial dropdowns to determine if an "Add" button is needed.
Cleaner Code Zdesign: The createAddButton function strictly handles the creation and behavior of the "Add" button, and createDeleteFieldButton does the same for "Delete."
These changes should align the logic to remove the "Add" button once used, ensuring users can add only as needed and manage field choices effectively.