Skip to content

Commit

Permalink
refactor: remove use parents opti (#950)
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia authored Oct 7, 2024
1 parent 16126c0 commit ce39340
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 34 deletions.
10 changes: 1 addition & 9 deletions src/item/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
PackedRecycledItemData,
ResultOf,
UUID,
getParentFromPath,
} from '@graasp/sdk';

import { verifyAuthentication } from '../api/axios.js';
Expand Down Expand Up @@ -97,16 +96,9 @@ export const getChildren = async (
.then(({ data }) => data);

export const getParents = async (
{ id, path }: { id: UUID; path?: string },
{ id }: { id: UUID },
{ API_HOST, axios }: PartialQueryConfigForApi,
) => {
// shortcut to prevent fetching parents if path shows that item is a root
if (path) {
const parentId = getParentFromPath(path);
if (!parentId) {
return [];
}
}
return axios
.get<PackedItem[]>(`${API_HOST}/${buildGetItemParents(id)}`)
.then(({ data }) => data);
Expand Down
14 changes: 0 additions & 14 deletions src/item/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,20 +236,6 @@ describe('useParents', () => {
}
});

it(`providing path can deduce empty array`, async () => {
const { data, isFetched } = await mockHook({
hook: () => hooks.useParents({ id: childItem.id, path: 'some-id' }),
endpoints: [],
wrapper,
});

expect(data).toHaveLength(0);
expect(isFetched).toBeTruthy();
expect(
queryClient.getQueryData(itemKeys.single(childItem.id).parents),
).toHaveLength(0);
});

it(`Unauthorized`, async () => {
// build endpoint for each item
const endpoints = [
Expand Down
13 changes: 2 additions & 11 deletions src/item/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,17 @@ const config = (
/**
* return parents for given item id
* @param id {string} item id
* @param path {string} item path, used to prevent fetching if no parent is defined
* @returns immutable list of parent items
*/
useParents: ({
id,
path,
enabled,
}: {
id?: UUID;
path?: string;
enabled?: boolean;
}) => {
useParents: ({ id, enabled }: { id?: UUID; enabled?: boolean }) => {
return useQuery({
queryKey: itemKeys.single(id).parents,
queryFn: async () => {
if (!id) {
throw new UndefinedArgument();
}

return Api.getParents({ id, path }, queryConfig);
return Api.getParents({ id }, queryConfig);
},
...defaultQueryOptions,
enabled: enabled && Boolean(id),
Expand Down

0 comments on commit ce39340

Please sign in to comment.