feat: enhance category filter functionality to include child categories in selection and deselection; update transaction page to expand selected categories based on parent-child relationships
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
useDuplicateIds,
|
||||
} from "@/lib/hooks";
|
||||
import type { TransactionsPaginatedParams } from "@/services/banking.service";
|
||||
import type { Category } from "@/lib/types";
|
||||
|
||||
type SortField = "date" | "amount" | "description";
|
||||
type SortOrder = "asc" | "desc";
|
||||
@@ -74,15 +75,35 @@ export function useTransactionsPage() {
|
||||
const categoryIdsParam = searchParams.get("categoryIds");
|
||||
const includeUncategorizedParam = searchParams.get("includeUncategorized");
|
||||
|
||||
if (categoryIdsParam) {
|
||||
if (categoryIdsParam && metadata) {
|
||||
const categoryIds = categoryIdsParam.split(",");
|
||||
setSelectedCategories(categoryIds);
|
||||
|
||||
// Expand parent categories to include their children
|
||||
const expandedCategoryIds = new Set<string>(categoryIds);
|
||||
|
||||
categoryIds.forEach((categoryId) => {
|
||||
// Check if this is a parent category
|
||||
const category = metadata.categories.find(
|
||||
(c: Category) => c.id === categoryId
|
||||
);
|
||||
if (category && category.parentId === null) {
|
||||
// Find all children of this parent
|
||||
const children = metadata.categories.filter(
|
||||
(c: Category) => c.parentId === categoryId
|
||||
);
|
||||
children.forEach((child: Category) => {
|
||||
expandedCategoryIds.add(child.id);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
setSelectedCategories(Array.from(expandedCategoryIds));
|
||||
setPage(0);
|
||||
} else if (includeUncategorizedParam === "true") {
|
||||
setSelectedCategories(["uncategorized"]);
|
||||
setPage(0);
|
||||
}
|
||||
}, [searchParams]);
|
||||
}, [searchParams, metadata]);
|
||||
|
||||
// Calculate start date based on period
|
||||
const startDate = useMemo(() => {
|
||||
|
||||
Reference in New Issue
Block a user