Skip to content

Commit

Permalink
add blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
AshrafMd-1 committed Jan 6, 2024
1 parent 22f36fd commit 3fda4c4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
29 changes: 23 additions & 6 deletions src/Common/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { classNames } from "../../Utils/utils";

export type FilterState = Record<string, unknown>;
export type FilterParamKeys = string | string[];

interface FilterBadgeProps {
name: string;
value?: string;
Expand All @@ -18,15 +19,25 @@ interface FilterBadgeProps {
* A custom hook wrapped around raviger's `useQueryParams` hook to ease handling
* of pagination and filters.
*/
export default function useFilters({ limit = 14 }: { limit?: number }) {
export default function useFilters({
limit = 14,
cacheBlacklist = [],
}: {
limit?: number;
cacheBlacklist?: string[];
}) {
const { t } = useTranslation();
const { kasp_string } = useConfig();
const hasPagination = limit > 0;
const [showFilters, setShowFilters] = useState(false);
const [qParams, setQueryParams] = useQueryParams();
const [primaryCacheBlacklist, setPrimaryCacheBlacklist] =
useState(cacheBlacklist);

const updateQuery = (filter: FilterState) => {
const updateQuery = (filter: FilterState, cacheBlacklist: string[] = []) => {
filter = hasPagination ? { page: 1, limit, ...filter } : filter;
if (cacheBlacklist.length)
setPrimaryCacheBlacklist(primaryCacheBlacklist.concat(cacheBlacklist));
setQueryParams(Object.assign({}, qParams, filter), { replace: true });
};
const updatePage = (page: number) => {
Expand All @@ -38,7 +49,10 @@ export default function useFilters({ limit = 14 }: { limit?: number }) {
};
const removeFilter = (param: string) => removeFilters([param]);

useEffect(() => updateFiltersCache(qParams), [qParams]);
useEffect(
() => updateFiltersCache(qParams, primaryCacheBlacklist),
[qParams, primaryCacheBlacklist]
);

useEffect(() => {
const cache = getFiltersCache();
Expand Down Expand Up @@ -170,7 +184,7 @@ 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={() => {
updateFiltersCache({});
updateFiltersCache({}, primaryCacheBlacklist);
removeFilters(Object.keys(qParams));
}}
>
Expand Down Expand Up @@ -266,9 +280,12 @@ const getFiltersCacheKey = () => `filters--${window.location.pathname}`;
const getFiltersCache = () => {
return JSON.parse(localStorage.getItem(getFiltersCacheKey()) || "{}");
};
const updateFiltersCache = (cache: Record<string, unknown>) => {
const updateFiltersCache = (
cache: Record<string, unknown>,
cacheBlacklist: string[]
) => {
const result = { ...cache };
for (const param of FILTERS_CACHE_BLACKLIST) {
for (const param of FILTERS_CACHE_BLACKLIST.concat(cacheBlacklist)) {
delete result[param];
}
localStorage.setItem(getFiltersCacheKey(), JSON.stringify(result));
Expand Down
1 change: 1 addition & 0 deletions src/Components/Assets/AssetsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const AssetsList = () => {
resultsPerPage,
} = useFilters({
limit: 18,
cacheBlacklist: ["search"],
});
const [assets, setAssets] = useState([{} as AssetData]);
const [isLoading, setIsLoading] = useState(false);
Expand Down
6 changes: 5 additions & 1 deletion src/Components/ExternalResult/ResultList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Page from "../Common/components/Page";
import routes from "../../Redux/api";
import useQuery from "../../Utils/request/useQuery";
import { parsePhoneNumber } from "../../Utils/utils";

const Loading = lazy(() => import("../Common/Loading"));

export default function ResultList() {
Expand All @@ -26,7 +27,10 @@ export default function ResultList() {
FilterBadges,
advancedFilter,
resultsPerPage,
} = useFilters({ limit: 14 });
} = useFilters({
limit: 14,
cacheBlacklist: ["mobile_number", "name"],
});
const [showDialog, setShowDialog] = useState(false);
const [selectedFacility, setSelectedFacility] = useState<FacilityModel>({
name: "",
Expand Down
1 change: 1 addition & 0 deletions src/Components/Facility/HospitalList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const HospitalList = () => {
resultsPerPage,
} = useFilters({
limit: 14,
cacheBlacklist: ["search"],
});
let manageFacilities: any = null;
const { user_type } = useAuthUser();
Expand Down
7 changes: 6 additions & 1 deletion src/Components/Patient/ManagePatients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const PatientManager = () => {
resultsPerPage,
} = useFilters({
limit: 12,
cacheBlacklist: ["name", "patient_no"],
});
const [selectedFacility, setSelectedFacility] = useState<FacilityModel>({
name: "",
Expand Down Expand Up @@ -775,7 +776,11 @@ export const PatientManager = () => {
return {
name,
value: qParams[name] || defaultValue,
onChange: (e: FieldChangeEvent<T>) => updateQuery({ [e.name]: e.value }),
onChange: (e: FieldChangeEvent<T>) =>
updateQuery({ [e.name]: e.value }, [
"phone_number",
"emergency_phone_number",
]),
className: "grow w-full mb-2",
};
};
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Patient/SampleViewAdmin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import CountBlock from "../../CAREUI/display/Count";
import CareIcon from "../../CAREUI/icons/CareIcon";
import { AdvancedFilterButton } from "../../CAREUI/interactive/FiltersSlideover";
import Page from "../Common/components/Page";

const Loading = lazy(() => import("../Common/Loading"));

export default function SampleViewAdmin() {
Expand All @@ -38,6 +39,7 @@ export default function SampleViewAdmin() {
resultsPerPage,
} = useFilters({
limit: 10,
cacheBlacklist: ["patient_name", "district_name"],
});
const dispatch: any = useDispatch();
const initialData: any[] = [];
Expand All @@ -58,6 +60,7 @@ export default function SampleViewAdmin() {
const res = await dispatch(getAnyFacility(qParams.facility));
setFacilityName(res?.data?.name);
}

fetchData();
}, [dispatch, qParams.facility]);

Expand Down
1 change: 1 addition & 0 deletions src/Components/Shifting/BoardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const ScrollingComponent = withScrolling("div");
export default function BoardView() {
const { qParams, updateQuery, FilterBadges, advancedFilter } = useFilters({
limit: -1,
cacheBlacklist: ["patient_name"],
});
const { wartime_shifting } = useConfig();

Expand Down
2 changes: 1 addition & 1 deletion src/Components/Shifting/ListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function ListView() {
FilterBadges,
advancedFilter,
resultsPerPage,
} = useFilters({});
} = useFilters({ cacheBlacklist: ["patient_name"] });

const [modalFor, setModalFor] = useState({
externalId: undefined,
Expand Down
5 changes: 4 additions & 1 deletion src/Components/Users/ManageUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export default function ManageUsers() {
FilterBadges,
advancedFilter,
resultsPerPage,
} = useFilters({ limit: 18 });
} = useFilters({
limit: 18,
cacheBlacklist: ["username"],
});
let manageUsers: any = null;
const [expandSkillList, setExpandSkillList] = useState(false);
const [expandFacilityList, setExpandFacilityList] = useState(false);
Expand Down

0 comments on commit 3fda4c4

Please sign in to comment.