Skip to content

Commit

Permalink
fix: use a mapped type to return dynamic object based on keys supplie…
Browse files Browse the repository at this point in the history
…d in the input
  • Loading branch information
spaenleh committed Oct 11, 2023
1 parent 9163af4 commit f1ced88
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
25 changes: 14 additions & 11 deletions src/hooks/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,9 +45,9 @@ export default (queryConfig: QueryClientConfig) => {
});
};

const useAggregateActions = (
args: Omit<AggregateActionsArgs, 'itemId'> &
Partial<Pick<AggregateActionsArgs, 'itemId'>>,
const useAggregateActions = <T extends (keyof MappedAggregateBy)[]>(
args: AggregateActionsArgs<T> &
Partial<Pick<AggregateActionsArgs<T>, 'itemId'>>,
options?: { enabled?: boolean },
) => {
const enabledValue =
Expand All @@ -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,
Expand Down
16 changes: 14 additions & 2 deletions src/utils/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,25 @@ import {
Item,
} from '@graasp/sdk';

export type AggregateActionsArgs = {
export type AggregateActionsArgs<K extends AggregateBy[]> = {
itemId: Item['id'];
view: Context;
requestedSampleSize: number;
type?: string[];
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;
};

0 comments on commit f1ced88

Please sign in to comment.