Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Filters Cache restore logic #6908

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cypress/e2e/facility_spec/facility_creation.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ describe("Facility Creation", () => {
.should("be.visible");
// verify the facility homepage
cy.visit("/facility");
cy.get("#removeicon").click();
manageUserPage.typeFacilitySearch(facilityName);
facilityPage.verifyFacilityBadgeContent(facilityName);
manageUserPage.assertFacilityInCard(facilityName);
Expand Down
59 changes: 39 additions & 20 deletions src/Common/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,24 @@ export default function useFilters({ limit = 14 }: { limit?: number }) {
if (!hasPagination) return;
setQueryParams(Object.assign({}, qParams, { page }), { replace: true });
};
const removeFilter = (param: string) => updateQuery({ [param]: "" });
const removeFilters = (keys: string[]) =>
updateQuery(keys.reduce((acc, key) => ({ ...acc, [key]: "" }), qParams));
const removeFilters = (params: string[]) => {
setQueryParams(removeFromQuery(qParams, params));
};
const removeFilter = (param: string) => removeFilters([param]);

useEffect(() => {
const localFilters = JSON.parse(
localStorage.getItem("filters--" + window.location.pathname) || "{}"
);
const blacklistLocalFilters = ["page", "limit", "offset"];
const newFilters = { ...localFilters, ...qParams };
const filteredNewFilters = blacklistLocalFilters.reduce(
(acc, key) => ({ ...acc, [key]: undefined }),
newFilters
);
useEffect(() => updateFiltersCache(qParams), [qParams]);

localStorage.setItem(
"filters--" + window.location.pathname,
JSON.stringify(filteredNewFilters)
useEffect(() => {
const cache = getFiltersCache();
const qParamKeys = Object.keys(qParams);
const canSkip = Object.keys(cache).every(
(key) => qParamKeys.includes(key) && qParams[key] === cache[key]
);

updateQuery(newFilters);
}, [qParams]);
if (canSkip) return;
if (Object.keys(cache).length) {
setQueryParams(cache);
}
}, []);

const FilterBadge = ({ name, value, paramKey }: FilterBadgeProps) => {
if (Array.isArray(paramKey))
Expand Down Expand Up @@ -174,7 +170,8 @@ export default function useFilters({ limit = 14 }: { limit?: number }) {
id="clear-all-filters"
className="rounded-full border border-gray-300 bg-white px-2 py-1 text-xs text-gray-600 hover:text-gray-800"
onClick={() => {
removeFilters(activeFilters);
updateFiltersCache({});
removeFilters(Object.keys(qParams));
}}
>
{t("clear_all_filters")}
Expand Down Expand Up @@ -254,3 +251,25 @@ export default function useFilters({ limit = 14 }: { limit?: number }) {
},
};
}

const removeFromQuery = (query: Record<string, unknown>, params: string[]) => {
const result = { ...query };
for (const param of params) {
delete result[param];
}
return result;
};

const FILTERS_CACHE_BLACKLIST = ["page", "limit", "offset"];

const getFiltersCacheKey = () => `filters--${window.location.pathname}`;
const getFiltersCache = () => {
return JSON.parse(localStorage.getItem(getFiltersCacheKey()) || "{}");
};
const updateFiltersCache = (cache: Record<string, unknown>) => {
const result = { ...cache };
for (const param of FILTERS_CACHE_BLACKLIST) {
delete result[param];
}
localStorage.setItem(getFiltersCacheKey(), JSON.stringify(result));
};
Loading