From a12fb3ee9c40c3e6d76736ee26caebac37f5a2f2 Mon Sep 17 00:00:00 2001 From: Roshan Jagadish Date: Sun, 12 Nov 2023 20:07:17 +0530 Subject: [PATCH] fix: Subtable Filter Refresh Bug Fixed --- src/components/TableToolbar/Filters/Filters.tsx | 6 ++---- src/components/TableToolbar/Filters/useFilterUrl.tsx | 10 ++++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components/TableToolbar/Filters/Filters.tsx b/src/components/TableToolbar/Filters/Filters.tsx index 48ce54ef0..2dbb450cd 100644 --- a/src/components/TableToolbar/Filters/Filters.tsx +++ b/src/components/TableToolbar/Filters/Filters.tsx @@ -42,6 +42,7 @@ import { analytics, logEvent } from "@src/analytics"; import type { TableFilter } from "@src/types/table"; import { generateId } from "@src/utils/table"; import { useFilterUrl } from "./useFilterUrl"; +import { isEqual } from "lodash-es"; const shouldDisableApplyButton = (queries: any) => { for (let query of queries) { @@ -255,10 +256,7 @@ export default function Filters() { // If the filter in URL is not the same as currently applied local filter // then update the user filter. useEffect(() => { - if ( - filtersUrl && - JSON.stringify(filtersUrl) !== JSON.stringify(appliedFilters) - ) { + if (filtersUrl && !isEqual(filtersUrl, appliedFilters)) { setUserFilters(filtersUrl); setOverrideTableFilters(true); } diff --git a/src/components/TableToolbar/Filters/useFilterUrl.tsx b/src/components/TableToolbar/Filters/useFilterUrl.tsx index 6f008b611..e5b4b7fba 100644 --- a/src/components/TableToolbar/Filters/useFilterUrl.tsx +++ b/src/components/TableToolbar/Filters/useFilterUrl.tsx @@ -1,6 +1,9 @@ +import { tableFiltersAtom, tableScope } from "@src/atoms/tableScope"; import { TableFilter } from "@src/types/table"; +import { useAtom } from "jotai"; +import { isEqual } from "lodash-es"; import { useSnackbar } from "notistack"; -import { useEffect, useState } from "react"; +import { useEffect } from "react"; import { useSearchParams } from "react-router-dom"; function isTableFilter(filter: any): filter is TableFilter { @@ -16,7 +19,7 @@ function isTableFilter(filter: any): filter is TableFilter { export function useFilterUrl() { const [searchParams, setSearchParams] = useSearchParams(); const { enqueueSnackbar } = useSnackbar(); - const [filters, setFilters] = useState(null); + const [filters, setFilters] = useAtom(tableFiltersAtom, tableScope); // Fetch filter from URL and update user filter useEffect(() => { @@ -32,8 +35,7 @@ export function useFilterUrl() { for (const _filter of _filters) { if (!isTableFilter(_filter)) throw new Error("Invalid Filter"); } - - setFilters(_filters); + if (!isEqual(_filters, filters)) setFilters(_filters); } catch (err) { enqueueSnackbar("Oops, filter in URL is incorrect!!!", { variant: "error",