From 6287ed9d6d1369c5d2b9e2826ec6d9518cf37ae6 Mon Sep 17 00:00:00 2001 From: spaenleh Date: Wed, 11 Oct 2023 15:25:50 +0200 Subject: [PATCH] fix: change input of hook --- src/config/keys.ts | 7 ++++--- src/hooks/action.test.ts | 11 +++++------ src/hooks/action.ts | 15 ++++++++------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/config/keys.ts b/src/config/keys.ts index 525b54387..b2bfbf0c6 100644 --- a/src/config/keys.ts +++ b/src/config/keys.ts @@ -1,4 +1,4 @@ -import { AggregateBy, Category, Optional, UUID } from '@graasp/sdk'; +import { AggregateBy, Category, UUID } from '@graasp/sdk'; import { AggregateActionsArgs } from '../utils/action'; import { hashItemsIds } from '../utils/item'; @@ -202,8 +202,9 @@ export const buildActionsKey = (args: { ]; export const buildAggregateActionsKey = ( - args: Optional, 'itemId'>, -) => ['aggregateActions', args]; + itemId: string | undefined, + args: Omit, 'itemId'>, +) => ['aggregateActions', itemId, args]; export const buildInvitationKey = (id?: UUID) => ['invitations', id]; export const buildItemInvitationsKey = (id?: UUID) => [ diff --git a/src/hooks/action.test.ts b/src/hooks/action.test.ts index 433525d92..431f5fa78 100644 --- a/src/hooks/action.test.ts +++ b/src/hooks/action.test.ts @@ -113,7 +113,6 @@ describe('Action Hooks', () => { describe('useAggregateActions', () => { const args = { - itemId, view: Context.Builder, requestedSampleSize: 5, type: ['update'], @@ -122,12 +121,12 @@ describe('Action Hooks', () => { aggregateMetric: AggregateMetric.ActionCount, aggregateBy: [AggregateBy.CreatedDay], }; - const route = `/${buildGetAggregateActions(args)}`; - const key = buildAggregateActionsKey(args); + const route = `/${buildGetAggregateActions({ itemId, ...args })}`; + const key = buildAggregateActionsKey(itemId, args); const response = AGGREGATE_ACTIONS_DATA; it(`Receive aggregate actions for item id`, async () => { - const hook = () => hooks.useAggregateActions(args); + const hook = () => hooks.useAggregateActions(itemId, args); const endpoints = [{ route, response }]; const { data } = await mockHook({ endpoints, hook, wrapper }); expect(data?.toJS()).toEqual(response); @@ -138,7 +137,7 @@ describe('Action Hooks', () => { ).toEqual(response); }); it(`Receive aggregate actions for item id`, async () => { - const hook = () => hooks.useAggregateActions(args); + const hook = () => hooks.useAggregateActions(itemId, args); const endpoints = [{ route, response }]; const { data } = await mockHook({ endpoints, hook, wrapper }); expect(data?.toJS()).toEqual(response); @@ -150,7 +149,7 @@ describe('Action Hooks', () => { }); it(`Unauthorized`, async () => { - const hook = () => hooks.useAggregateActions(args); + const hook = () => hooks.useAggregateActions(itemId, args); const endpoints = [ { route, diff --git a/src/hooks/action.ts b/src/hooks/action.ts index 49b83b005..1d21083cf 100644 --- a/src/hooks/action.ts +++ b/src/hooks/action.ts @@ -1,4 +1,4 @@ -import { Optional, UUID, convertJs } from '@graasp/sdk'; +import { UUID, convertJs } from '@graasp/sdk'; import { ActionDataRecord, ImmutableCast } from '@graasp/sdk/frontend'; import { useQuery } from 'react-query'; @@ -46,15 +46,16 @@ export default (queryConfig: QueryClientConfig) => { }; const useAggregateActions = ( - args: Optional, 'itemId'>, + itemId: string | undefined, + args: Omit, 'itemId'>, options?: { enabled?: boolean }, ) => { const enabledValue = (options?.enabled ?? true) && - Boolean(args.itemId) && + Boolean(itemId) && Boolean(args.requestedSampleSize); return useQuery({ - queryKey: buildAggregateActionsKey(args), + queryKey: buildAggregateActionsKey(itemId, args), queryFn: (): Promise< ImmutableCast< ({ aggregateResult: number } & { @@ -63,11 +64,11 @@ export default (queryConfig: QueryClientConfig) => { })[] > > => { - if (!args.itemId) { + if (!itemId) { throw new UndefinedArgument(); } - return Api.getAggregateActions(args, queryConfig).then((data) => - convertJs(data), + return Api.getAggregateActions({ itemId, ...args }, queryConfig).then( + (data) => convertJs(data), ); }, ...defaultQueryOptions,