in this on grouping, add this filter the rule should be active-true
Assistant
To include only active rules (active === true) in the grouping, you can add a filter condition inside the loop or before processing the rules. Here's your modified function with the filter applied:
If you want me to help integrating it with your exact code, just share the UI part with search and tabs or your framework, I can help customize the solution!
import React, { useState, useEffect, useRef } from "react";
import {
FwTabs,
FwTab,
FwTabPanel,
FwSpinner,
ToastController,
FwToast,
FwPagination,
FwButton,
FwIcon,
FwListOptions,
FwPopover,
FwModal,
} from "@freshworks/crayons/react";
import AutomationCard from "./AutomationCard";
import SummaryContent from "./SummaryContent";
import {
getTicketCreationRules,
getTicketUpdationRules,
getHourlyRules,
getAgents,
pagination,
} from "../common";
// ===== UPDATE: fetchData now just toggles fetchFromApi and triggers data fetch on that state change =====
const fetchData = async () => {
// Toggle mode and data fetch will happen automatically in useEffect above
setFetchFromApi((prev) => !prev);
code
// Show toast for user info
toast.current.trigger({
type: "info",
content: `Fetching ${
fetchFromApi ? "Entity Data" : "Recent API Data"
}...`,
});
when search is empty, automatically you should show all rules, and if i m searching the key and changed the tab in that tab the search should be empty
Thanks for sharing your full component! You already have searchTerm and searchQuery states with some logic. To implement what you want:
When search is empty, show all rules. (You have this basically handled in getFilteredRules).
When you change tabs, reset the search input and searchTerm & searchQuery to empty string ("") so the search input clears and all rules are shown for new tab.
How to do this:
Reset search states (searchTerm and searchQuery) whenever activeTab changes.
Make sure the search input (your <fw-input>) is cleared accordingly.
What you need to add/change:
Add this useEffect that runs when activeTab changes to clear search:
Also fix search input value binding so it clears in UI:
You’re using a ref for your <fw-input> and manually setting .value in a useEffect. This is a little fragile; better to make searchTerm control the input value directly.
Change your <fw-input> to:
jsx
<fw-input
ref={inputRef}
icon-left="search"
hint-text="Type Keyword to Search"
warning-text="Please use numbers for user ID"
placeholder="Enter a Keyword to Search..."
error-text="Invalid user ID"
clear-input
style={{
width: "400px",
height: "60px",
fontSize: "18px",
}}
value={searchTerm} // controlled value bound here
onInput={(e) => {
setSearchTerm(e.target.value);
if (showSearchHint) setShowSearchHint(false);
}}
onKeyDown={(e) => {
if (e.key === "Enter") {
const trimmed = e.target.value.trim();
if (!trimmed) {
setSearchQuery("");
setShowSearchHint(false);
} else {
setSearchQuery(trimmed);
setShowSearchHint(false);
}
}
}}
/>
Remove the useEffect that was setting input value manually:
js
// REMOVE this effect as no longer needed
// useEffect(() => {
// if (inputRef.current) {
// inputRef.current.value = searchTerm;
// }
// }, [searchTerm]);
Why?
Controlled input's value is React's source of truth, so whenever you clear searchTerm on tab change, the input visually clears.
Your filtering logic is already doing: if empty searchQuery then show all.
Since you already filter on searchQuery (which only updates on Enter key), it means typing alone won't filter until Enter is hit. You could also add filtering on typing (searchTerm) for instant search results, if you want.
Let me know if you want that extension or help with anything else!
Not yet included in a published release.
if (activeTab === "ticketcreate") {
groups = ticketCreationRuleGroups;
flatRules = ticketCreationRules;
} else if (activeTab === "ticketupdate") {
groups = ticketUpdateRuleGroups;
flatRules = ticketUpdateRules;
} else if (activeTab === "hourly") {
groups = hourlyRuleGroups;
flatRules = hourlyTriggerRules;
}
if (loading) {
return (
<div style={{ display: "flex", justifyContent: "center", alignItems: "center", height: "200px" }}>
<FwSpinner size="medium" color="green" />
</div>
);
}
if (ruleViewFilter.value === "duplicate_rules" && groups.length > 0) {
// Create paginated group chunks according to rulesPerPage and group sizes
const paginatedGroups = [];
let currentPageGroups = [];
let currentGroupCount = 0;
for (const group of groups) {
const groupSize = group.length;
if (groupSize > rulesPerPage) {
// Large group alone on page
if (currentGroupCount > 0) {
paginatedGroups.push(currentPageGroups);
currentPageGroups = [];
currentGroupCount = 0;
}
paginatedGroups.push([group]);
} else {
// Fit group in current page or start new page
if (currentGroupCount + groupSize <= rulesPerPage) {
currentPageGroups.push(group);
currentGroupCount += groupSize;
} else {
if (currentGroupCount > 0) {
paginatedGroups.push(currentPageGroups);
}
currentPageGroups = [group];
currentGroupCount = groupSize;
}
}
}
if (currentGroupCount > 0) {
paginatedGroups.push(currentPageGroups);
}
const pageIndex = currentPage[activeTab] - 1;
const groupsToShow = paginatedGroups[pageIndex] || [];
// Flatten rules shown on this page for filtering and delete operations
const flattenedRulesOnPage = flattenGroups(groupsToShow);
const filteredRulesOnPage = getFilteredRules(flattenedRulesOnPage);
return (
<div style={{ marginLeft: 10, marginRight: "auto" }}>
{groupsToShow.map((group, idx) => {
const rulesToShow = group.filter(rule =>
filteredRulesOnPage.some(filteredRule => filteredRule.id === rule.id)
);
if (rulesToShow.length === 0) return null;
return (
<div key={`group-${pageIndex}-${idx}`} style={{ marginBottom: "12px", boxShadow: "0 4px 8px rgba(0, 0, 0, 0.2)", }}>
{rulesToShow.map((rule, i) => (
<div
key={rule.id}
className="fw-card-1"
style={{
// marginBottom: "10px",
marginTop: i === 0 ? "10px" : "2px",
padding: "5px 8px",
// marginLeft: "20px",
borderRadius: "4px",
}}
>
<AutomationCard
rule={rule}
index={i + 1}
isFirstInGroup={i === 0}
onDelete={() => deleteRule(rule, flattenedRulesOnPage)}
activeTab={activeTab}
onSelect={openRuleDetails}
agent={agent}
/>
</div>
))}
</div>
);
})}
</div>
);
} else {
// All rules view paginated by rules as before
const filteredFlatRules = getFilteredRules(flatRules);
const paginatedFlatRules = pagination(filteredFlatRules, currentPage[activeTab]);
if (paginatedFlatRules.length === 0) {
return (
<p style={{ textAlign: "center", marginTop: "20px" }}>
No Rules Found.
</p>
);
}
return paginatedFlatRules.map((rule, idx) => (
<div
key={rule.id || idx}
className="fw-card-1"
style={{
marginBottom: "5px",
marginTop: "10px",
padding: "5px 8px",
marginLeft: 10,
marginRight: "auto",
}}
>
<AutomationCard
rule={rule}
index={idx + 1}
isFirstInGroup={true}
onDelete={() => deleteRule(rule, filteredFlatRules)}
activeTab={activeTab}
onSelect={openRuleDetails}
agent={agent}
/>
</div>
));
}