in this default by default use state ticketcreate is there. when i change to ticketupdate/hourlytriggers it should make that tab as usestate to show its details
To ensure that the active tab reflects the selection made by the user and updates the displayed rules accordingly, you can take the following steps:
Set the initial active tab: You're already initializing the state to ticketcreate.
Update the rules fetching based on the active tab: To ensure that the rules reflect the selected tab without duplicating the rendering logic, use a single FwTabPanel and conditionally render the correct data based on the activeTab.
Utilize the useEffect to fetch rules again whenever the activeTab changes: This allows for dynamic fetching or filtering of the specific rules associated with the active tab.
Here's how you can modify your AutomationTabs component to achieve this:
Single FwTabPanel: There's now only one active FwTabPanel, which is conditionally updated according to the current tab.
Reset Search When Switching Tabs: The searchTriggered state is reset when changing tabs to ensure that the search state does not carry over between different tabs.
Dynamic Rendering: The displayed cards will now always reflect the rules that belong to the currently active tab, simplifying the rendering logic.
This way, your component should properly reflect the user’s actions when they switch between tabs.
The issue you're facing where the ticketupdate or hourly tabs show as empty when selected can occur due to a couple of reasons:
Data Not Being Updated: When switching tabs, the current data context should switch to the appropriate rules set (e.g., ticketUpdateRules or hourlyTriggerRules).
Incorrect Filtering Logic: If the filtering logic does not match any rules after switching tabs, it may return an empty array.
Fixing the Logic
Let's ensure that the data is correctly fetched and displayed based on the selected tab. Here’s how you can adjust your component:
Ensure rules are properly loaded: Make sure that the states for ticketUpdateRules and hourlyTriggerRules are being set when initially loading the rules.
Filtering: Ensure that the filtering works correctly based on the current tab and the search term.
Rerendering: Ensure that the logic to retrieve the rules is appropriately reflecting the current tab.
Ensure Data Retrieval: Make sure that you’re correctly setting the rules when fetching them in fetchRules. I modified the function to directly retrieve the rules and set them in state.
Conditional Rendering: Updated the FwTabPanel to render cards only for the activeTab to ensure that you are using the right data for the selected tab.
Simplified Rendering Logic: The rendering now just uses the current activeTab to choose which data to display, removing redundancy and ensuring clarity.
By applying these changes, your tabs should correctly display rules according to the selected active tab, and the search should work effectively across all categories. If you still experience issues, please ensure the rules are being returned correctly from the backend or API calls.
import React, { useState, useEffect } from "react";
import { FwTabs, FwTab, FwTabPanel, FwSpinner, FwPagination } from "@freshworks/crayons/react";
import AutomationCard from "./AutomationCard";
import { getTicketCreationRules, getTicketUpdationRules, getHourlyRules } from '../common';
use state should be change based on the user input by default it should be ticketcreate tha's it
To ensure that your component behaves as you intend, with the active tab defaulted to ticketcreate, and allows switching between tabs while correctly updating the displayed data, you must ensure the search and tab-switching logic is correctly implemented.
From your provided code, it appears you already have a foundation in place. However, let's refine it a bit to ensure that the active tab is managed properly, and the data displayed reflects the current selection of the tab while maintaining the search functionality.
Data Fetching: Ensure you're setting the rules correctly from your API functions (getTicketCreationRules, getTicketUpdationRules, getHourlyRules) to the respective state variables upon loading.
Single FwTabPanel Handling: Each tab panel checks for the active tab before rendering the cards. You can eliminate the need for redundant FwTabPanel instances and simply render the content based on the active tab. In this way, only one panel will show content based on the current tab.
Tying State To User Input: The state for activeTab defaults to "ticketcreate", which will show the corresponding rules upon the initial render. The tab change updates this state, and the rules will change accordingly.
This structure ensures that your application behaves as expected, allowing seamless switching between tabs while maintaining a search functionality that applies to the currently viewed rules.