Skip to content

Commit

Permalink
feat: Add possibility to copy multiple files at one
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-Torrent committed Aug 12, 2021
1 parent b7aa5e2 commit 8a10e63
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/api/item.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StatusCodes } from 'http-status-codes';
import {
buildCopyItemRoute,
buildCopyItemsRoute,
buildDeleteItemRoute,
buildDeleteItemsRoute,
buildDownloadFilesRoute,
Expand Down Expand Up @@ -189,6 +190,22 @@ export const copyItem = async (
return newItem;
};

export const copyItems = async (
{ id, to }: { id: UUID[]; to: UUID },
{ API_HOST }: QueryClientConfig,
) => {
// send parentId if defined
const body = { ...(to && { parentId: to }) };
const res = await fetch(`${API_HOST}/${buildCopyItemsRoute(id)}`, {
...DEFAULT_POST,
body: JSON.stringify(body),
}).then(failOnError);

const newItem = await res.json();

return newItem;
};

export const getSharedItems = async ({ API_HOST }: QueryClientConfig) => {
const res = await fetch(`${API_HOST}/${SHARE_ITEM_WITH_ROUTE}`, {
...DEFAULT_GET,
Expand Down
3 changes: 3 additions & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export const buildGetItemsRoute = (ids: UUID[]) =>
`${ITEMS_ROUTE}?${qs.stringify({ id: ids }, { arrayFormat: 'repeat' })}`;
export const buildMoveItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/move`;
export const buildCopyItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/copy`;
export const buildCopyItemsRoute = (ids: UUID[]) =>
`${ITEMS_ROUTE}/copy?${qs.stringify({ id: ids }, { arrayFormat: 'repeat' })}`;
export const buildEditItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}`;
export const buildShareItemWithRoute = (id: UUID) =>
`item-memberships?itemId=${id}`;
Expand Down Expand Up @@ -122,6 +124,7 @@ export const API_ROUTES = {
buildDeleteItemRoute,
buildDeleteItemsRoute,
buildCopyItemRoute,
buildCopyItemsRoute,
buildPatchMember,
buildPostItemFlagRoute,
buildEditItemMembershipRoute,
Expand Down
1 change: 1 addition & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const MUTATION_KEYS = {
DELETE_ITEM: 'deleteItem',
DELETE_ITEMS: 'deleteItems',
COPY_ITEM: 'copyItem',
COPY_ITEMS: 'copyItems',
MOVE_ITEM: 'moveItem',
SHARE_ITEM: 'shareItem',
FILE_UPLOAD: 'fileUpload',
Expand Down
21 changes: 21 additions & 0 deletions src/mutations/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { QueryClient } from 'react-query';
import * as Api from '../api';
import {
copyItemRoutine,
copyItemsRoutine,
createItemRoutine,
deleteItemsRoutine,
deleteItemRoutine,
Expand Down Expand Up @@ -32,6 +33,7 @@ const {
SHARE_ITEM,
MOVE_ITEM,
COPY_ITEM,
COPY_ITEMS,
DELETE_ITEMS,
POST_ITEM_LOGIN,
PUT_ITEM_LOGIN,
Expand Down Expand Up @@ -302,6 +304,25 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
},
});

queryClient.setMutationDefaults(COPY_ITEMS, {
mutationFn: (payload) =>
Api.copyItems(payload, queryConfig).then((newItem) => ({
to: payload.to,
...newItem,
})),
// cannot mutate because it needs the id
onSuccess: () => {
notifier?.({ type: copyItemsRoutine.SUCCESS });
},
onError: (error) => {
notifier?.({ type: copyItemsRoutine.FAILURE, payload: { error } });
},
onSettled: (newItem) => {
const parentKey = getKeyForParentId(newItem?.to);
queryClient.invalidateQueries(parentKey);
},
});

queryClient.setMutationDefaults(MOVE_ITEM, {
mutationFn: (payload) =>
Api.moveItem(payload, queryConfig).then(() => payload),
Expand Down
1 change: 1 addition & 0 deletions src/routines/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getOwnItemsRoutine = createRoutine('GET_OWN_ITEMS');
export const setItemRoutine = createRoutine('GET_OWN_ITEMS');
export const moveItemRoutine = createRoutine('MOVE_ITEM');
export const copyItemRoutine = createRoutine('COPY_ITEM');
export const copyItemsRoutine = createRoutine('COPY_ITEMS');
export const editItemRoutine = createRoutine('EDIT_ITEM');
export const deleteItemsRoutine = createRoutine('DELETE_ITEMS');
export const uploadFileRoutine = createRoutine('UPLOAD_FILE');

0 comments on commit 8a10e63

Please sign in to comment.