Skip to content

Commit

Permalink
fix: change input of hook
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh committed Oct 11, 2023
1 parent 2b71b75 commit 6287ed9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
7 changes: 4 additions & 3 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -202,8 +202,9 @@ export const buildActionsKey = (args: {
];

export const buildAggregateActionsKey = <K extends AggregateBy[]>(
args: Optional<AggregateActionsArgs<K>, 'itemId'>,
) => ['aggregateActions', args];
itemId: string | undefined,
args: Omit<AggregateActionsArgs<K>, 'itemId'>,
) => ['aggregateActions', itemId, args];

export const buildInvitationKey = (id?: UUID) => ['invitations', id];
export const buildItemInvitationsKey = (id?: UUID) => [
Expand Down
11 changes: 5 additions & 6 deletions src/hooks/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ describe('Action Hooks', () => {

describe('useAggregateActions', () => {
const args = {
itemId,
view: Context.Builder,
requestedSampleSize: 5,
type: ['update'],
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -150,7 +149,7 @@ describe('Action Hooks', () => {
});

it(`Unauthorized`, async () => {
const hook = () => hooks.useAggregateActions(args);
const hook = () => hooks.useAggregateActions(itemId, args);
const endpoints = [
{
route,
Expand Down
15 changes: 8 additions & 7 deletions src/hooks/action.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -46,15 +46,16 @@ export default (queryConfig: QueryClientConfig) => {
};

const useAggregateActions = <T extends (keyof MappedAggregateBy)[]>(
args: Optional<AggregateActionsArgs<T>, 'itemId'>,
itemId: string | undefined,
args: Omit<AggregateActionsArgs<T>, '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 } & {
Expand All @@ -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,
Expand Down

0 comments on commit 6287ed9

Please sign in to comment.