To adjust the code to initialize only the entity data for the active tab on initialization, and then fetch from the API only when the user clicks the "Recent Data" (fetch button), you mainly need to modify:
The initial useEffect on component mount: Load entity data for only the active tab (done already).
The fetch on tab change: Do not fetch data automatically from API; only load entity data.
The fetch button handler (fetchData) to fetch recent data from the API for the currently active tab.
Currently, the code fetches either entity or API data on every tab change based on the fetchFromApi flag, but you want it so that:
Initialization: only load entity data for the active tab.
Tab change: only load entity data for the newly active tab.
When the user clicks "Recent Data" button: fetch from API for current tab.
In other words, on init and tab change: always load entity data; only fetch API data when user explicitly clicks "Recent Data".
Proposed changes summary:
Remove the fetchFromApi dependency in the activeTab effect. On tab change, always fetch from entity DB, not from API.
fetchFromApi state no longer controls data fetching on tab change. Instead, it's used to toggle the fetch button.
Update the fetchData function to fetch from API only for the active tab, not all tabs.
Update the "Recent Data" button label and behavior so user click triggers API fetch for current tab only.
Possibly, rename fetchFromApi to something like showingApiData or similar.
Code modifications in detail
Find useEffect that fetches data on tab change:
js
useEffect(() => {
if (!tabsInitialized) return;
const fetchTabData = async () => {
setLoading(true);
if (!newRulePloadDB5.current) {
console.error("❌ Entity DB reference not initialized.");
setLoading(false);
return;
}
if (fetchFromApi) {
if (activeTab === "ticketcreate") {
await getTicketCreationRules(client, setTicketCreationRules);
} else if (activeTab === "ticketupdate") {
await getTicketUpdationRules(client, setTicketUpdateRules);
} else if (activeTab === "hourly") {
await getHourlyRules(client, setHourlyTriggerRules);
}
} else {
// Load entity DB data fallback per active tab
if (activeTab === "ticketcreate") {
const data = await Filterrecords(ticketCreate, newRulePloadDB5.current);
setTicketCreationRules(data);
} else if (activeTab === "ticketupdate") {
const data = await Filterrecords(ticketUpdate, newRulePloadDB5.current);
setTicketUpdateRules(data);
} else if (activeTab === "hourly") {
const data = await Filterrecords(hourlyTriggers, newRulePloadDB5.current);
setHourlyTriggerRules(data);
}
}
setLoading(false);
};
fetchTabData();
setCurrentPage((prev) => ({ ...prev, [activeTab]: 1 }));
}, [activeTab, fetchFromApi, tabsInitialized]);
Change to:
Drop dependency on fetchFromApi (no longer fetch API data on tab change).
On tab change, load entity data only.
js
useEffect(() => {
if (!tabsInitialized) return;
const fetchTabData = async () => {
setLoading(true);
if (!newRulePloadDB5.current) {
console.error("❌ Entity DB reference not initialized.");
setLoading(false);
return;
}
// Always load entity DB data on tab change
if (activeTab === "ticketcreate") {
const data = await Filterrecords(ticketCreate, newRulePloadDB5.current);
setTicketCreationRules(data);
} else if (activeTab === "ticketupdate") {
const data = await Filterrecords(ticketUpdate, newRulePloadDB5.current);
setTicketUpdateRules(data);
} else if (activeTab === "hourly") {
const data = await Filterrecords(hourlyTriggers, newRulePloadDB5.current);
setHourlyTriggerRules(data);
}
setLoading(false);
};
fetchTabData();
setCurrentPage((prev) => ({ ...prev, [activeTab]: 1 }));
}, [activeTab, tabsInitialized]);
Then, update the fetchData function that fires when "Recent Data" (button) is clicked, so that it fetches from API only for the current active tab, not all tabs:
if (fetchFromApi) {
setLoading(true);
if (activeTab === "ticketcreate") {
await getTicketCreationRules(client, setTicketCreationRules);
} else if (activeTab === "ticketupdate") {
await getTicketUpdationRules(client, setTicketUpdateRules);
} else if (activeTab === "hourly") {
await getHourlyRules(client, setHourlyTriggerRules);
}
const agentData = await getAgents(client);
setAgent(agentData);
setLoading(false);
} else {
// entity data already loaded on tab change, you can omit or keep this as safety
}
Finally, the toggle fetchFromApi flag usage is confusing: if it’s true on init, and fetches only entity data on tab change ignoring it, then the toggle seems odd. Instead, maybe renamefetchFromApi => showingApiData, and when user clicks "Recent Data", we fetch API for the current tab, and set showingApiData to true.
On tab change, you can do:
Load only entity data.
Set showingApiData to false to reflect that entity data is shown.
On initialization and tab change: only entity data is fetched to populate state.
The "Recent Data" button triggers API fetching for only the current tab.
The button toggles between viewing entity data and API data for the active tab.
This avoids fetching API data automatically on tab changes.
If you want no toggle on the button (i.e., clicking "Recent Data" always fetches from API; no reverting back), you can simplify fetchData to only fetch API data:
js
const fetchData = async () => {
if (!newRulePloadDB5.current) {
console.error("❌ Entity DB reference not initialized.");
return;
}
setLoading(true);
if (activeTab === "ticketcreate") {
await getTicketCreationRules(client, setTicketCreationRules);
} else if (activeTab === "ticketupdate") {
await getTicketUpdationRules(client, setTicketUpdateRules);
} else if (activeTab === "hourly") {
await getHourlyRules(client, setHourlyTriggerRules);
}
const agentData = await getAgents(client);
setAgent(agentData);
setLoading(false);
};
And button label stays static: Recent Data.
Let me know if you want me to provide the fully modified component code with these changes applied!
Certainly! Below is the full updated component with the requested behavior:
On initialization, only entity data for the last active tab is loaded.
On tab change, only entity data is loaded for that tab.
When the user clicks "Recent Data" button, it fetches data only from the API for the currently active tab.
The button label remains "Recent Data" (no toggle back to entity data). You can easily toggle that if you want.
The fetchFromApi flag was replaced by showingApiData for clarity, but in this version I've kept the button simple and it only fetches Recent Data (API data) for the current tab.
On component mount and tab changes, only entity data loads.
Clicking the "Recent Data" button fetches fresh data from API for the active tab.
The agent list is also refreshed on this fetch.
The initial entity load happens only once for the last active tab.
The UI and pagination logic remain unchanged.
Let me know if you'd also want the button to toggle between entity and API data, or any other behavior!