diff --git a/src/api/itemTag.ts b/src/api/itemTag.ts index 879c5ac7..c525b2e5 100644 --- a/src/api/itemTag.ts +++ b/src/api/itemTag.ts @@ -1,34 +1,9 @@ -import { ItemTag, ItemTagType, ResultOf, UUID } from '@graasp/sdk'; +import { ItemTag, ItemTagType, UUID } from '@graasp/sdk'; -import { - GET_TAGS_ROUTE, - buildDeleteItemTagRoute, - buildGetItemTagsRoute, - buildGetItemsTagsRoute, - buildPostItemTagRoute, -} from '../routes.js'; +import { buildDeleteItemTagRoute, buildPostItemTagRoute } from '../routes.js'; import { PartialQueryConfigForApi } from '../types.js'; import { verifyAuthentication } from './axios.js'; -export const getTags = async ({ API_HOST, axios }: PartialQueryConfigForApi) => - axios.get(`${API_HOST}/${GET_TAGS_ROUTE}`).then(({ data }) => data); - -export const getItemTags = async ( - id: UUID, - { API_HOST, axios }: PartialQueryConfigForApi, -) => - axios - .get(`${API_HOST}/${buildGetItemTagsRoute(id)}`) - .then(({ data }) => data); - -export const getItemsTags = async ( - ids: UUID[], - { API_HOST, axios }: PartialQueryConfigForApi, -) => - axios - .get>(`${API_HOST}/${buildGetItemsTagsRoute(ids)}`) - .then(({ data }) => data); - // payload: tagId, itemPath, creator export const postItemTag = async ( { itemId, type }: { itemId: UUID; type: ItemTagType }, diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 105f2f66..58bbb0df 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -21,7 +21,6 @@ import configureItemGeolocationHooks from './itemGeolocation.js'; import configureItemLikeHooks from './itemLike.js'; import configureItemLoginHooks from './itemLogin.js'; import configureItemPublishedHooks from './itemPublish.js'; -import configureItemTagHooks from './itemTag.js'; import configureItemValidationHooks from './itemValidation.js'; import configureMembershipHooks from './membership.js'; import configureMentionsHooks from './mention.js'; @@ -45,7 +44,6 @@ export default ( ...configureMembershipHooks(queryConfig, websocketClient), ...configureItemHooks(queryConfig, websocketClient), ...configureEtherpadHooks(queryConfig), - ...configureItemTagHooks(queryConfig), ...configureCategoryHooks(queryConfig), ...configureKeywordSearchHooks(queryConfig), ...configureItemLikeHooks(queryConfig), diff --git a/src/hooks/itemTag.test.ts b/src/hooks/itemTag.test.ts deleted file mode 100644 index 15c7335f..00000000 --- a/src/hooks/itemTag.test.ts +++ /dev/null @@ -1,173 +0,0 @@ -import { - DiscriminatedItem, - FolderItemFactory, - ItemTag, - ItemTagType, - MAX_TARGETS_FOR_READ_REQUEST, - Member, -} from '@graasp/sdk'; - -import { StatusCodes } from 'http-status-codes'; -import nock from 'nock'; -import { afterEach, describe, expect, it } from 'vitest'; - -import { - ITEM_TAGS, - UNAUTHORIZED_RESPONSE, - buildResultOfData, - generateFolders, -} from '../../test/constants.js'; -import { - mockHook, - setUpTest, - splitEndpointByIds, - splitEndpointByIdsForErrors, -} from '../../test/utils.js'; -import { itemKeys } from '../keys.js'; -import { buildGetItemTagsRoute, buildGetItemsTagsRoute } from '../routes.js'; - -const { hooks, wrapper, queryClient } = setUpTest(); - -describe('Item Tags Hooks', () => { - afterEach(() => { - nock.cleanAll(); - queryClient.clear(); - }); - - describe('useItemTags', () => { - const itemId = FolderItemFactory().id; - const route = `/${buildGetItemTagsRoute(itemId)}`; - const key = itemKeys.single(itemId).tags; - - const hook = () => hooks.useItemTags(itemId); - - it(`Receive tags of given item`, async () => { - const response = ITEM_TAGS; - const endpoints = [{ route, response }]; - const { data, isSuccess } = await mockHook({ endpoints, hook, wrapper }); - - expect(data).toMatchObject(response); - - // verify cache keys - expect(queryClient.getQueryData(key)).toMatchObject(response); - expect(isSuccess).toBeTruthy(); - }); - - it(`Unauthorized`, async () => { - const endpoints = [ - { - route, - response: UNAUTHORIZED_RESPONSE, - statusCode: StatusCodes.UNAUTHORIZED, - }, - ]; - const { data, isError } = await mockHook({ - hook, - wrapper, - endpoints, - }); - - expect(data).toBeFalsy(); - expect(isError).toBeTruthy(); - // verify cache keys - expect(queryClient.getQueryData(key)).toBeFalsy(); - }); - }); - - describe('useItemsTags', () => { - const itemsIds = generateFolders(MAX_TARGETS_FOR_READ_REQUEST + 1).map( - ({ id }) => id, - ); - - const keys = itemsIds.map((itemId) => itemKeys.single(itemId).tags); - const tags = itemsIds.map((id) => [ - { - id: 'some id', - createdAt: '2023-09-06T11:50:32.894Z', - creator: {} as Member, - item: { id } as DiscriminatedItem, - type: ItemTagType.Hidden, - }, - ]); - - const hook = () => hooks.useItemsTags(itemsIds); - - it(`Receive tags of given items`, async () => { - const response = buildResultOfData(tags, (t: ItemTag[]) => t[0].item.id); - - const endpoints = splitEndpointByIds( - itemsIds, - MAX_TARGETS_FOR_READ_REQUEST, - (chunk) => `/${buildGetItemsTagsRoute(chunk)}`, - tags, - (t: ItemTag[]) => t[0].item.id, - ); - const { data, isSuccess } = await mockHook({ endpoints, hook, wrapper }); - expect(data).toEqual(response); - - expect(isSuccess).toBeTruthy(); - }); - - it(`Receive tags and save only for non-error tags`, async () => { - const errors = [ - { - name: 'error', - message: 'error', - stack: '', - }, - ]; - const id = itemsIds[0]; - const tagsForItem = [ - { - id: 'some id', - createdAt: '2023-09-06T11:50:32.894Z', - creator: {} as Member, - item: { id } as DiscriminatedItem, - type: ItemTagType.Hidden, - }, - ]; - const response = buildResultOfData( - [tagsForItem], - (t: ItemTag[]) => t[0].item.id, - errors, - ); - const idWithError = 'some-id'; - const ids = [id, idWithError]; - const endpoints = splitEndpointByIdsForErrors( - ids, - MAX_TARGETS_FOR_READ_REQUEST, - (chunk) => `/${buildGetItemsTagsRoute(chunk)}`, - { response, statusCode: StatusCodes.OK }, - ); - - const { data, isSuccess } = await mockHook({ - endpoints, - hook: () => hooks.useItemsTags(ids), - wrapper, - }); - - expect(data).toEqual(response); - expect(isSuccess).toBeTruthy(); - }); - - it(`Unauthorized`, async () => { - const endpoints = [ - { - route: `/${buildGetItemsTagsRoute(['some-id'])}`, - response: UNAUTHORIZED_RESPONSE, - statusCode: StatusCodes.UNAUTHORIZED, - }, - ]; - const { data, isError } = await mockHook({ - hook, - wrapper, - endpoints, - }); - - expect(data).toBeFalsy(); - expect(isError).toBeTruthy(); - // verify cache keys - keys.forEach((key) => expect(queryClient.getQueryData(key)).toBeFalsy()); - }); - }); -}); diff --git a/src/hooks/itemTag.ts b/src/hooks/itemTag.ts deleted file mode 100644 index bb9b7581..00000000 --- a/src/hooks/itemTag.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { MAX_TARGETS_FOR_READ_REQUEST, UUID } from '@graasp/sdk'; - -import { useQuery } from '@tanstack/react-query'; - -import { splitRequestByIdsAndReturn } from '../api/axios.js'; -import * as Api from '../api/itemTag.js'; -import { UndefinedArgument } from '../config/errors.js'; -import { itemKeys } from '../keys.js'; -import { QueryClientConfig } from '../types.js'; - -export default (queryConfig: QueryClientConfig) => { - const { defaultQueryOptions } = queryConfig; - - const useItemTags = (id?: UUID) => - useQuery({ - queryKey: itemKeys.single(id).tags, - queryFn: () => { - if (!id) { - throw new UndefinedArgument(); - } - return Api.getItemTags(id, queryConfig); - }, - enabled: Boolean(id), - ...defaultQueryOptions, - }); - - const useItemsTags = (ids?: UUID[]) => { - return useQuery({ - queryKey: itemKeys.many(ids).tags, - queryFn: () => { - if (!ids || ids?.length === 0) { - throw new UndefinedArgument(); - } - return splitRequestByIdsAndReturn( - ids, - MAX_TARGETS_FOR_READ_REQUEST, - (chunk) => Api.getItemsTags(chunk, queryConfig), - true, - ); - }, - enabled: Boolean(ids?.length), - ...defaultQueryOptions, - }); - }; - - return { useItemTags, useItemsTags }; -}; diff --git a/src/routes.ts b/src/routes.ts index 7e9e810c..fb4e4c98 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -91,9 +91,6 @@ export const SIGN_IN_ROUTE = '/login'; export const SIGN_IN_WITH_PASSWORD_ROUTE = '/login-password'; export const SIGN_UP_ROUTE = '/register'; export const SIGN_OUT_ROUTE = '/logout'; -export const buildGetItemTagsRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/tags`; -export const buildGetItemsTagsRoute = (ids: UUID[]) => - `${ITEMS_ROUTE}/tags?${new URLSearchParams(ids.map((id) => ['id', id]))}`; export const buildPostItemTagRoute = ({ itemId, type, @@ -395,7 +392,6 @@ export const API_ROUTES = { buildGetItemMembershipsForItemsRoute, buildGetItemPublishedInformationRoute, buildGetItemsInCategoryRoute, - buildGetItemTagsRoute, buildGetLastItemValidationGroupRoute, buildGetLikesForMemberRoute, buildGetMostLikedPublishedItemsRoute,