From f1ced88074567736866ccc8734f42de32fa3c057 Mon Sep 17 00:00:00 2001 From: spaenleh Date: Wed, 11 Oct 2023 10:44:55 +0200 Subject: [PATCH] fix: use a mapped type to return dynamic object based on keys supplied in the input --- src/hooks/action.ts | 25 ++++++++++++++----------- src/utils/action.ts | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/hooks/action.ts b/src/hooks/action.ts index f8b7f207d..ecbacc132 100644 --- a/src/hooks/action.ts +++ b/src/hooks/action.ts @@ -7,7 +7,7 @@ import * as Api from '../api'; import { UndefinedArgument } from '../config/errors'; import { buildActionsKey, buildAggregateActionsKey } from '../config/keys'; import { QueryClientConfig } from '../types'; -import { AggregateActionsArgs } from '../utils/action'; +import { AggregateActionsArgs, MappedAggregateBy } from '../utils/action'; export default (queryConfig: QueryClientConfig) => { const { defaultQueryOptions } = queryConfig; @@ -45,9 +45,9 @@ export default (queryConfig: QueryClientConfig) => { }); }; - const useAggregateActions = ( - args: Omit & - Partial>, + const useAggregateActions = ( + args: AggregateActionsArgs & + Partial, 'itemId'>>, options?: { enabled?: boolean }, ) => { const enabledValue = @@ -57,16 +57,19 @@ export default (queryConfig: QueryClientConfig) => { return useQuery({ queryKey: buildAggregateActionsKey(args), queryFn: (): Promise< - ImmutableCast<{ aggregateResult: number; createdDay: Date }[]> + ImmutableCast< + ({ aggregateResult: number } & { + // this adds a key for each element in the aggregation array and sets it to the relevant type + [Key in T[number]]: MappedAggregateBy[Key]; + })[] + > > => { - const { itemId } = args; - if (!itemId) { + if (!args.itemId) { throw new UndefinedArgument(); } - return Api.getAggregateActions( - args as AggregateActionsArgs, - queryConfig, - ).then((data) => convertJs(data)); + return Api.getAggregateActions(args, queryConfig).then((data) => + convertJs(data), + ); }, ...defaultQueryOptions, enabled: enabledValue, diff --git a/src/utils/action.ts b/src/utils/action.ts index 5e1e704d9..c99ebb2c1 100644 --- a/src/utils/action.ts +++ b/src/utils/action.ts @@ -7,7 +7,7 @@ import { Item, } from '@graasp/sdk'; -export type AggregateActionsArgs = { +export type AggregateActionsArgs = { itemId: Item['id']; view: Context; requestedSampleSize: number; @@ -15,5 +15,17 @@ export type AggregateActionsArgs = { countGroupBy: CountGroupBy[]; aggregateFunction: AggregateFunction; aggregateMetric: AggregateMetric; - aggregateBy: AggregateBy[]; + aggregateBy: K; +}; + +export type MappedAggregateBy = { + [AggregateBy.CreatedDay]: Date; + // todo: this should maybe be returned by the backend as a number directly ? + [AggregateBy.CreatedDayOfWeek]: string; + // todo: same as above, this should be returned as a number by the backend + [AggregateBy.CreatedTimeOfDay]: string; + [AggregateBy.ItemId]: string; + [AggregateBy.ActionType]: string; + [AggregateBy.ActionCount]: number; + [AggregateBy.ActionLocation]: string; };