Skip to content

Commit

Permalink
feat: add get items by ids
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallah75 committed Jun 25, 2021
1 parent 05bde46 commit a898f29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/api/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@ import {
buildEditItemRoute,
buildGetChildrenRoute,
buildGetItemRoute,
buildGetItemsRoute,
buildGetS3MetadataRoute,
buildMoveItemRoute,
buildPostItemRoute,
GET_OWN_ITEMS_ROUTE,
SHARE_ITEM_WITH_ROUTE,
buildS3FileUrl,
buildS3UploadFileRoute,
GET_OWN_ITEMS_ROUTE,
SHARE_ITEM_WITH_ROUTE,
} from './routes';
import {
DEFAULT_DELETE,
DEFAULT_GET,
DEFAULT_POST,
DEFAULT_PATCH,
failOnError,
} from './utils';
import { DEFAULT_DELETE, DEFAULT_GET, DEFAULT_PATCH, DEFAULT_POST, failOnError } from './utils';
import { getParentsIdsFromPath } from '../utils/item';
import { Item, QueryClientConfig, UUID, ExtendedItem } from '../types';
import { ExtendedItem, Item, QueryClientConfig, UUID } from '../types';

export const getItem = async (id: UUID, { API_HOST }: QueryClientConfig) => {
const res = await fetch(
Expand All @@ -33,6 +28,15 @@ export const getItem = async (id: UUID, { API_HOST }: QueryClientConfig) => {
return item;
};

export const getItems = async (ids: UUID[], { API_HOST }: QueryClientConfig) => {
const res = await fetch(
`${API_HOST}/${buildGetItemsRoute(ids)}`,
DEFAULT_GET,
).then(failOnError);
const items = await res.json();
return items;
};

export const getOwnItems = async ({ API_HOST }: QueryClientConfig) => {
const res = await fetch(
`${API_HOST}/${GET_OWN_ITEMS_ROUTE}`,
Expand Down
1 change: 1 addition & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const buildDeleteItemsRoute = (ids: UUID[]) =>
export const buildGetChildrenRoute = (id: UUID) =>
`${ITEMS_ROUTE}/${id}/children`;
export const buildGetItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}`;
export const buildGetItemsRoute = (ids: UUID[]) => `${ITEMS_ROUTE}?${ids.map(i => "id="+i).join("&")}`;
export const buildMoveItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/move`;
export const buildCopyItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/copy`;
export const buildEditItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}`;
Expand Down
1 change: 1 addition & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { UUID } from '../types';
export const ITEMS_KEY = 'items';
export const OWN_ITEMS_KEY = [ITEMS_KEY, 'own'];
export const buildItemKey = (id: UUID) => [ITEMS_KEY, id];
export const MULTIPLE_ITEMS_KEY = [ITEMS_KEY, 'multiple']
export const buildItemChildrenKey = (id: UUID) => [ITEMS_KEY, id, 'children'];
export const SHARED_ITEMS_KEY = 'shared';
export const CURRENT_MEMBER_KEY = 'currentMember';
Expand Down
16 changes: 15 additions & 1 deletion src/hooks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
buildFileContentKey,
buildS3FileContentKey,
OWN_ITEMS_KEY,
SHARED_ITEMS_KEY,
SHARED_ITEMS_KEY, MULTIPLE_ITEMS_KEY,
} from '../config/keys';
import * as Api from '../api';
import { Item, QueryClientConfig, UUID } from '../types';
Expand Down Expand Up @@ -105,6 +105,20 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
...defaultOptions,
}),

useItems: (ids: UUID[]) =>
useQuery({
queryKey: MULTIPLE_ITEMS_KEY,
queryFn: () => Api.getItems(ids, queryConfig).then((data) => List(data)),
onSuccess: async (items: List<Item>) => {
// save items in their own key
items?.forEach(async (item) => {
const { id } = item;
queryClient.setQueryData(buildItemKey(id), Map(item));
});
},
...defaultOptions,
}),

useItemMemberships: (id: UUID) =>
useQuery({
queryKey: buildItemMembershipsKey(id),
Expand Down

0 comments on commit a898f29

Please sign in to comment.