Skip to content

Commit 06427f8

Browse files
committed
Don't make two API calls for activity log data
1 parent 986c375 commit 06427f8

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

resources/scripts/api/account/activity.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { ActivityLog, Transformers } from '@definitions/user';
44
import { AxiosError } from 'axios';
55
import http, { PaginatedResult, QueryBuilderParams, withQueryBuilderParams } from '@/api/http';
66
import { toPaginatedSet } from '@definitions/helpers';
7+
import useFilteredObject from '@/plugins/useFilteredObject';
78

89
export type ActivityLogFilters = QueryBuilderParams<'ip' | 'event', 'timestamp'>;
910

1011
const useActivityLogs = (filters?: ActivityLogFilters, config?: ConfigInterface<PaginatedResult<ActivityLog>, AxiosError>): responseInterface<PaginatedResult<ActivityLog>, AxiosError> => {
11-
const key = useUserSWRContentKey([ 'account', 'activity', JSON.stringify(filters) ]);
12+
const key = useUserSWRContentKey([ 'account', 'activity', JSON.stringify(useFilteredObject(filters || {})) ]);
1213

1314
return useSWR<PaginatedResult<ActivityLog>>(key, async () => {
1415
const { data } = await http.get('/api/client/account/activity', {

resources/scripts/api/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export interface FractalPaginatedResponse extends FractalResponseList {
8888
total_pages: number;
8989
/* eslint-enable camelcase */
9090
};
91-
}
91+
};
9292
}
9393

9494
export interface PaginatedResult<T> {

resources/scripts/helpers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ export function hashToPath (hash: string): string {
6565
export function formatIp (ip: string): string {
6666
return /([a-f0-9:]+:+)+[a-f0-9]+/.test(ip) ? `[${ip}]` : ip;
6767
}
68+
69+
// eslint-disable-next-line @typescript-eslint/ban-types
70+
export const isObject = (o: unknown): o is {} => typeof o === 'object' && o !== null;
71+
72+
// eslint-disable-next-line @typescript-eslint/ban-types
73+
export const isEmptyObject = (o: {}): boolean =>
74+
Object.keys(o).length === 0 && Object.getPrototypeOf(o) === Object.prototype;
75+
76+
// eslint-disable-next-line @typescript-eslint/ban-types
77+
export const getObjectKeys = <T extends {}> (o: T): Array<keyof T> => Object.keys(o) as Array<keyof T>;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Similar to "withQueryBuilderParams" except this function filters out any null,
3+
* undefined, or empty string key values. This allows the parameters to be used for
4+
* caching without having to account for all of the different data combinations.
5+
*/
6+
import { isEmptyObject, isObject } from '@/helpers';
7+
8+
// eslint-disable-next-line @typescript-eslint/ban-types
9+
export default <T extends {}>(data: T): T => {
10+
const empty = [ undefined, null, '' ] as unknown[];
11+
12+
const removeEmptyValues = (input: T): T =>
13+
Object.entries(input)
14+
.filter(([ _, value ]) => !empty.includes(value))
15+
.reduce((obj, [ k, v ]) => {
16+
const parsed = isObject(v) ? removeEmptyValues(v as any) : v;
17+
18+
return isObject(parsed) && isEmptyObject(parsed) ? obj : { ...obj, [k]: parsed };
19+
}, {} as T);
20+
21+
return removeEmptyValues(data);
22+
};

0 commit comments

Comments
 (0)