Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove use parents opti #950

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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