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

fix: add params to getDescendants #796

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions src/api/item.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ItemType } from '@graasp/sdk';

import { AxiosInstance } from 'axios';
import { v4 } from 'uuid';
import { describe, expect, it, vi } from 'vitest';

import { getDescendants } from './item.js';

describe('getDescendants', () => {
it('without parameters', () => {
const mockGet = vi.fn().mockResolvedValue({ data: { value: 'hello' } });
const axios = {
get: mockGet,
} as unknown as AxiosInstance;
getDescendants({ id: v4() }, { API_HOST: 'https://localhost:3000', axios });
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
expect(mockGet).toHaveBeenCalledWith(expect.not.stringContaining('types'));
});

it.each([
{ inputs: { showHidden: true }, query: 'showHidden=true' },
{ inputs: { showHidden: false }, query: 'showHidden=false' },
{
inputs: { types: [ItemType.FOLDER], showHidden: false },
query: 'types=folder&showHidden=false',
},
{
inputs: { types: [ItemType.FOLDER], showHidden: true },
query: 'types=folder&showHidden=true',
},
{
inputs: { types: [ItemType.FOLDER, ItemType.APP], showHidden: false },
query: 'types=folder&types=app&showHidden=false',
},
{
inputs: { types: [ItemType.FOLDER, ItemType.APP], showHidden: true },
query: 'types=folder&types=app&showHidden=true',
},
])('With parameters: $inputs', ({ inputs, query }) => {
const mockGet = vi.fn().mockResolvedValue({ data: { value: 'hello' } });
const axios = {
get: mockGet,
} as unknown as AxiosInstance;
getDescendants(
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
{ id: v4(), ...inputs },
{ API_HOST: 'https://localhost:3000', axios },
);
expect(mockGet).toHaveBeenCalledWith(expect.stringContaining(query));
});
});
23 changes: 18 additions & 5 deletions src/api/item.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DiscriminatedItem,
ItemGeolocation,
ItemTypeUnion,
PackedItem,
PackedRecycledItemData,
ResultOf,
Expand Down Expand Up @@ -227,12 +228,24 @@ export const getParents = async (
};

export const getDescendants = async (
{ id }: { id: UUID },
{
id,
types,
showHidden,
}: { id: UUID; types?: ItemTypeUnion[]; showHidden?: boolean },
{ API_HOST, axios }: PartialQueryConfigForApi,
) =>
axios
.get<PackedItem[]>(`${API_HOST}/${buildGetItemDescendants(id)}`)
.then(({ data }) => data);
) => {
const url = new URL(`${API_HOST}/${buildGetItemDescendants(id)}`);
if (types?.length) {
for (const itemType of types) {
url.searchParams.append('types', itemType);
}
}
spaenleh marked this conversation as resolved.
Show resolved Hide resolved
if (showHidden !== undefined) {
url.searchParams.set('showHidden', showHidden.toString());
}
return axios.get<PackedItem[]>(url.toString()).then(({ data }) => data);
};

export const moveItems = async (
{
Expand Down
9 changes: 5 additions & 4 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DiscriminatedItem,
ItemGeolocation,
ItemType,
ItemTypeUnion,
UUID,
UnionOfConst,
} from '@graasp/sdk';
Expand Down Expand Up @@ -60,7 +61,10 @@ export const itemKeys = {
paginatedChildren: [...allChildren, 'paginated'] as const,

// descendants
descendants: [...singleBaseKey, 'descendants'] as const,
descendants: (options?: {
types?: ItemTypeUnion[];
showHidden?: boolean;
}) => [...singleBaseKey, 'descendants', options].filter(Boolean),

// parents
parents: [...singleBaseKey, 'parents'] as const,
Expand Down Expand Up @@ -137,9 +141,6 @@ export const itemKeys = {
accessiblePage: (params: ItemSearchParams, pagination: PaginationParams) =>
[...itemKeys.allAccessible(), params, pagination] as const,

// shared items
shared: () => [...itemKeys.all, 'shared'] as const,

search: (args: {
query?: string;
categories?: Category['id'][][];
Expand Down
Loading