-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix filters cache restoration logic + cleaner URL query params and fi…
…lters cache + fixes state management of facility, lsg body and district in patient filters (#7157) * Fix when filters cache is applied and ignore unapllied filters in query params and cache * fixes #7166; fix selected state of facility, lsg body and district * remove unused redux actions
- Loading branch information
1 parent
d259c20
commit c08a7f5
Showing
9 changed files
with
140 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
type Filters = Record<string, unknown>; | ||
|
||
/** | ||
* @returns The filters cache key associated to the current window URL | ||
*/ | ||
const getKey = () => { | ||
return `filters--${window.location.pathname}`; | ||
}; | ||
|
||
/** | ||
* Returns a sanitized filter object that ignores filters with no value or | ||
* filters that are part of the blacklist. | ||
* | ||
* @param filters Input filters to be sanitized | ||
* @param blacklist Optional array of filter keys that are to be ignored. | ||
*/ | ||
const clean = (filters: Filters, blacklist?: string[]) => { | ||
const reducer = (cleaned: Filters, key: string) => { | ||
const valueAllowed = (filters[key] ?? "") != ""; | ||
if (valueAllowed && !blacklist?.includes(key)) { | ||
cleaned[key] = filters[key]; | ||
} | ||
return cleaned; | ||
}; | ||
|
||
return Object.keys(filters).reduce(reducer, {}); | ||
}; | ||
|
||
/** | ||
* Retrieves the cached filters | ||
*/ | ||
const get = (key?: string) => { | ||
const content = localStorage.getItem(key ?? getKey()); | ||
return content ? (JSON.parse(content) as Filters) : null; | ||
}; | ||
|
||
/** | ||
* Sets the filters cache with the specified filters. | ||
*/ | ||
const set = (filters: Filters, blacklist?: string[], key?: string) => { | ||
key ??= getKey(); | ||
filters = clean(filters, blacklist); | ||
|
||
if (Object.keys(filters).length) { | ||
localStorage.setItem(key, JSON.stringify(filters)); | ||
} else { | ||
invalidate(key); | ||
} | ||
}; | ||
|
||
/** | ||
* Removes the filters cache for the specified key or current URL. | ||
*/ | ||
const invalidate = (key?: string) => { | ||
localStorage.removeItem(key ?? getKey()); | ||
}; | ||
|
||
/** | ||
* Removes all filters cache in the platform. | ||
*/ | ||
const invaldiateAll = () => { | ||
for (const key in localStorage) { | ||
if (key.startsWith("filters--")) { | ||
invalidate(key); | ||
} | ||
} | ||
}; | ||
|
||
export default { | ||
get, | ||
set, | ||
invalidate, | ||
invaldiateAll, | ||
utils: { | ||
clean, | ||
}, | ||
}; |
Oops, something went wrong.