Skip to content

Commit

Permalink
feat: add post item action mutation (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia authored Oct 6, 2023
1 parent 3c14997 commit c8d6f53
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/api/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionData, UUID } from '@graasp/sdk';
import { Action, ActionData, UUID } from '@graasp/sdk';

import { QueryClientConfig } from '../types';
import { AggregateActionsArgs } from '../utils/action';
Expand All @@ -7,6 +7,7 @@ import {
buildExportActions,
buildGetActions,
buildGetAggregateActions,
buildPostItemAction,
} from './routes';

const axios = configureAxios();
Expand Down Expand Up @@ -34,3 +35,10 @@ export const exportActions = async (
{ API_HOST }: QueryClientConfig,
): Promise<void> =>
axios.post(`${API_HOST}/${buildExportActions(args.itemId)}`);

export const postItemAction = async (
itemId: UUID,
payload: { type: string; extra?: { [key: string]: unknown } },
{ API_HOST }: QueryClientConfig,
): Promise<Action> =>
axios.post(`${API_HOST}/${buildPostItemAction(itemId)}`, payload);
15 changes: 9 additions & 6 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ export const buildGetAggregateActions = (args: AggregateActionsArgs) =>
)}`;
export const buildExportActions = (itemId: UUID) =>
`${ITEMS_ROUTE}/${itemId}/actions/export`;
export const buildPostItemAction = (itemId: UUID) =>
`${ITEMS_ROUTE}/${itemId}/actions`;
export const buildGetInvitationRoute = (id: UUID) =>
`${ITEMS_ROUTE}/${INVITATIONS_ROUTE}/${id}`;
export const buildPatchInvitationRoute = (args: { itemId: UUID; id: UUID }) =>
Expand Down Expand Up @@ -410,7 +412,9 @@ export const API_ROUTES = {
buildExportActions,
buildExportItemChatRoute,
buildExportItemRoute,
buildFavoriteItemRoute,
buildGetActions,
buildGetAllPublishedItemsRoute,
buildGetApiAccessTokenRoute,
buildGetCategoriesRoute,
buildGetCategoryRoute,
Expand All @@ -426,30 +430,30 @@ export const API_ROUTES = {
buildGetItemMembershipsForItemsRoute,
buildGetItemPublishedInformationRoute,
buildGetItemRoute,
SEARCH_PUBLISHED_ITEMS_ROUTE,
buildGetItemsInCategoryRoute,
buildGetItemTagsRoute,
buildGetLastItemValidationGroupRoute,
buildGetLikesForMemberRoute,
buildGetMember,
buildGetMembersBy,
buildGetMembersRoute,
buildGetPlanRoute,
buildGetAllPublishedItemsRoute,
buildGetMostLikedPublishedItemsRoute,
buildGetMostRecentPublishedItemsRoute,
buildGetPlanRoute,
buildGetPublishedItemsForMemberRoute,
buildImportH5PRoute,
buildImportZipRoute,
buildItemPublishRoute,
buildItemUnpublishRoute,
buildManyGetItemPublishedInformationsRoute,
buildMoveItemRoute,
buildMoveItemsRoute,
buildPatchInvitationRoute,
buildPatchItemChatMessageRoute,
buildPatchMember,
buildPostEtherpadRoute,
buildPostInvitationsRoute,
buildPostItemAction,
buildPostItemCategoryRoute,
buildPostItemChatMessageRoute,
buildPostItemFlagRoute,
Expand All @@ -470,18 +474,17 @@ export const API_ROUTES = {
buildUploadAvatarRoute,
buildUploadFilesRoute,
buildUploadItemThumbnailRoute,
buildManyGetItemPublishedInformationsRoute,
GET_CATEGORY_TYPES_ROUTE,
GET_CURRENT_MEMBER_ROUTE,
GET_FAVORITE_ITEMS_ROUTE,
GET_FLAGS_ROUTE,
GET_ITEM_VALIDATION_REVIEWS_ROUTE,
GET_ITEM_VALIDATION_STATUSES_ROUTE,
GET_OWN_ITEMS_ROUTE,
GET_RECYCLED_ITEMS_DATA_ROUTE,
GET_FAVORITE_ITEMS_ROUTE,
buildFavoriteItemRoute,
GET_TAGS_ROUTE,
ITEMS_ROUTE,
SEARCH_PUBLISHED_ITEMS_ROUTE,
SHARED_ITEM_WITH_ROUTE,
SIGN_IN_ROUTE,
SIGN_IN_WITH_PASSWORD_ROUTE,
Expand Down
61 changes: 59 additions & 2 deletions src/mutations/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
UNAUTHORIZED_RESPONSE,
} from '../../test/constants';
import { mockMutation, setUpTest, waitForMutation } from '../../test/utils';
import { buildExportActions } from '../api/routes';
import { exportActionsRoutine } from '../routines';
import { buildExportActions, buildPostItemAction } from '../api/routes';
import { exportActionsRoutine, postActionRoutine } from '../routines';

jest.spyOn(Cookies, 'get').mockReturnValue({ session: 'somesession' });

Expand Down Expand Up @@ -84,4 +84,61 @@ describe('Action Mutations', () => {
);
});
});

describe('usePostItemAction', () => {
const route = `/${buildPostItemAction(itemId)}`;
const mutation = mutations.usePostItemAction;
const payload = { type: 'hello', extra: { to: 'me' } };

it(`Post Item Action`, async () => {
const endpoints = [
{ route, response: OK_RESPONSE, method: HttpMethod.POST },
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate({ itemId, payload });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: postActionRoutine.SUCCESS,
}),
);
});

it(`Unauthorized`, async () => {
const endpoints = [
{
route,
response: UNAUTHORIZED_RESPONSE,
method: HttpMethod.POST,
statusCode: StatusCodes.UNAUTHORIZED,
},
];

const mockedMutation = await mockMutation({
endpoints,
mutation,
wrapper,
});

await act(async () => {
await mockedMutation.mutate({ itemId, payload });
await waitForMutation();
});

expect(mockedNotifier).toHaveBeenCalledWith(
expect.objectContaining({
type: postActionRoutine.FAILURE,
}),
);
});
});
});
26 changes: 24 additions & 2 deletions src/mutations/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@ import { UUID } from '@graasp/sdk';

import { useMutation } from 'react-query';

import { exportActions } from '../api';
import { exportActionsRoutine } from '../routines';
import { exportActions, postItemAction } from '../api';
import { exportActionsRoutine, postActionRoutine } from '../routines';
import { QueryClientConfig } from '../types';

export default (queryConfig: QueryClientConfig) => {
const usePostItemAction = () =>
useMutation(
(args: {
itemId: UUID;
payload: { type: string; extra?: { [key: string]: unknown } };
}) => postItemAction(args.itemId, args.payload, queryConfig),
{
onSuccess: () => {
queryConfig.notifier?.({
type: postActionRoutine.SUCCESS,
});
},
onError: (error: Error) => {
queryConfig.notifier?.({
type: postActionRoutine.FAILURE,
payload: { error },
});
},
},
);

const useExportActions = () =>
useMutation((itemId: UUID) => exportActions({ itemId }, queryConfig), {
onSuccess: () => {
Expand All @@ -23,6 +44,7 @@ export default (queryConfig: QueryClientConfig) => {
});

return {
usePostItemAction,
useExportActions,
};
};
2 changes: 1 addition & 1 deletion src/routines/action.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createRoutine from './utils';

// eslint-disable-next-line import/prefer-default-export
export const postActionRoutine = createRoutine('POST_ACTION');
export const exportActionsRoutine = createRoutine('EXPORT_ACTIONS');

0 comments on commit c8d6f53

Please sign in to comment.