Don't make two API calls for activity log data

This commit is contained in:
DaneEveritt 2022-06-11 14:52:33 -04:00
parent 986c375052
commit 06427f8d13
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 35 additions and 2 deletions

View file

@ -0,0 +1,22 @@
/**
* Similar to "withQueryBuilderParams" except this function filters out any null,
* undefined, or empty string key values. This allows the parameters to be used for
* caching without having to account for all of the different data combinations.
*/
import { isEmptyObject, isObject } from '@/helpers';
// eslint-disable-next-line @typescript-eslint/ban-types
export default <T extends {}>(data: T): T => {
const empty = [ undefined, null, '' ] as unknown[];
const removeEmptyValues = (input: T): T =>
Object.entries(input)
.filter(([ _, value ]) => !empty.includes(value))
.reduce((obj, [ k, v ]) => {
const parsed = isObject(v) ? removeEmptyValues(v as any) : v;
return isObject(parsed) && isEmptyObject(parsed) ? obj : { ...obj, [k]: parsed };
}, {} as T);
return removeEmptyValues(data);
};