Skip to content

Commit

Permalink
fix: filters not applied are cleared when changing the sections
Browse files Browse the repository at this point in the history
i.e. when opening or closing a group

+ some optimization to some useEffect dependencies
  • Loading branch information
Loxeris committed Aug 27, 2024
1 parent e9c1599 commit dda6abc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
42 changes: 13 additions & 29 deletions packages/diracx-web-components/components/shared/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -374,35 +374,19 @@ export function DataTable(props: DataTableProps) {
updateSectionFilters(filters);
};

const SectionItem = React.useMemo(
() =>
sections
.find((section) => section.items.some((item) => item.id === appId))
?.items.find((item) => item.id === appId),
[appId, sections],
);

React.useEffect(() => {
// Function to parse the filters from the URL search params
const parseFiltersFromUrl = () => {
const filterStrings = getAllParam("filter");
return filterStrings.map((filterString: string) => {
const [id, column, operator, value] = filterString.split("_");
return { id: Number(id), column, operator, value };
});
};

const item = sections
.find((section) => section.items.some((item) => item.id === appId))
?.items.find((item) => item.id === appId);

if (getParam("filter")) {
// Parse the filters when the component mounts or when the searchParams change
const initialFilters = parseFiltersFromUrl();
// Set the filters (they will be displayed in the UI)
setFilters(initialFilters);
// Apply the filters to get the filtered data
const jsonFilters = initialFilters.map((filter) => ({
parameter: filter.column,
operator: filter.operator,
value: filter.value,
}));
setSearchBody({ search: jsonFilters });
} else if (item?.data?.filters) {
setFilters(item.data.filters);
const jsonFilters = item.data.filters.map(
if (SectionItem?.data?.filters) {
setFilters(SectionItem.data.filters);
setAppliedFilters(SectionItem.data.filters);
const jsonFilters = SectionItem.data.filters.map(
(filter: {
id: number;
column: string;
Expand All @@ -419,7 +403,7 @@ export function DataTable(props: DataTableProps) {
setFilters([]);
setSearchBody({ search: [] });
}
}, [appId, getAllParam, getParam, sections, setFilters, setSearchBody]);
}, [SectionItem?.data?.filters, setFilters, setSearchBody]);

// Manage sorting
const handleRequestSort = (
Expand Down
14 changes: 10 additions & 4 deletions packages/diracx-web-components/contexts/ApplicationsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { createContext, useCallback, useEffect, useState } from "react";
import React, {
createContext,
useCallback,
useMemo,
useEffect,
useState,
} from "react";
import { Dashboard, FolderCopy, Monitor } from "@mui/icons-material";
import JSONCrush from "jsoncrush";
import { useSearchParamsUtils } from "@/hooks/searchParamsUtils";
Expand Down Expand Up @@ -36,9 +42,6 @@ export const ApplicationsProvider = ({

const { getParam, setParam } = useSearchParamsUtils();

// get user sections from searchParams
const sectionsParams = getParam("sections");

// save user sections to searchParams (but not icons)
const setSectionsParams = useCallback(
(sections: UserSection[] | ((prev: UserSection[]) => UserSection[])) => {
Expand All @@ -61,6 +64,9 @@ export const ApplicationsProvider = ({
[setParam, userSections],
);

// get user sections from searchParams
const sectionsParams = useMemo(() => getParam("sections"), [getParam]);

useEffect(() => {
if (userSections.length !== 0) return;
if (sectionsParams) {
Expand Down

0 comments on commit dda6abc

Please sign in to comment.