Skip to content

Commit

Permalink
feat: add copy public item mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 8, 2021
1 parent 0eb1489 commit 93a109d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/api/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios, { AxiosResponse } from 'axios';
import {
buildCopyItemRoute,
buildCopyItemsRoute,
buildCopyPublicItemRoute,
buildDeleteItemRoute,
buildDeleteItemsRoute,
buildDownloadFilesRoute,
Expand Down Expand Up @@ -222,6 +223,22 @@ export const copyItem = async (
return newItem;
};

export const copyPublicItem = 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}/${buildCopyPublicItemRoute(id)}`, {
...DEFAULT_POST,
body: JSON.stringify(body),
}).then(failOnError);

const newItem = await res.json();

return newItem;
};

export const copyItems = async (
{ id, to }: { id: UUID[]; to: UUID },
{ API_HOST }: QueryClientConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const buildMoveItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/move`;
export const buildMoveItemsRoute = (ids: UUID[]) =>
`${ITEMS_ROUTE}/move?${qs.stringify({ id: ids }, { arrayFormat: 'repeat' })}`;
export const buildCopyItemRoute = (id: UUID) => `${ITEMS_ROUTE}/${id}/copy`;
export const buildCopyPublicItemRoute = (id: UUID) =>
`p/${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}`;
Expand Down Expand Up @@ -174,6 +176,7 @@ export const API_ROUTES = {
buildDeleteItemRoute,
buildDeleteItemsRoute,
buildCopyItemRoute,
buildCopyPublicItemRoute,
buildCopyItemsRoute,
buildPatchMember,
buildPostItemFlagRoute,
Expand Down
1 change: 1 addition & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const MUTATION_KEYS = {
DELETE_ITEM: 'deleteItem',
DELETE_ITEMS: 'deleteItems',
COPY_ITEM: 'copyItem',
COPY_PUBLIC_ITEM: 'copyPublicItem',
COPY_ITEMS: 'copyItems',
MOVE_ITEM: 'moveItem',
MOVE_ITEMS: 'moveItems',
Expand Down
13 changes: 8 additions & 5 deletions src/hooks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ export default (
ids
? ids.length === 1
? Api.getItem(
ids[0],
{ withMemberships: options?.withMemberships ?? false },
queryConfig,
).then((data) => List([data]))
ids[0],
{ withMemberships: options?.withMemberships ?? false },
queryConfig,
).then((data) => List([data]))
: Api.getItems(ids, queryConfig).then((data) => List(data))
: undefined,
onSuccess: async (items: List<Item>) => {
Expand Down Expand Up @@ -241,7 +241,10 @@ export default (
onSuccess: async (memberships) => {
// save memberships in their own key
ids?.forEach(async (id, idx) => {
queryClient.setQueryData(buildItemMembershipsKey(id), List(memberships[idx]));
queryClient.setQueryData(
buildItemMembershipsKey(id),
List(memberships[idx]),
);
});
},
enabled: Boolean(ids?.length) && ids?.every((id) => Boolean(id)),
Expand Down
19 changes: 19 additions & 0 deletions src/mutations/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {
RECYCLE_ITEM,
RECYCLE_ITEMS,
RESTORE_ITEMS,
COPY_PUBLIC_ITEM,
} = MUTATION_KEYS;

interface Value {
Expand Down Expand Up @@ -394,6 +395,24 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
},
});

queryClient.setMutationDefaults(COPY_PUBLIC_ITEM, {
mutationFn: (payload) =>
Api.copyPublicItem(payload, queryConfig).then((newItem) => ({
to: payload.to,
...newItem,
})),
onSuccess: (data) => {
notifier?.({ type: copyItemRoutine.SUCCESS, payload: data });
},
onError: (error) => {
notifier?.({ type: copyItemRoutine.FAILURE, payload: { error } });
},
onSettled: (_newItem, _err, payload) => {
const parentKey = getKeyForParentId(payload.to);
queryClient.invalidateQueries(parentKey);
},
});

queryClient.setMutationDefaults(COPY_ITEMS, {
mutationFn: (payload) =>
Api.copyItems(payload, queryConfig).then((newItems) => ({
Expand Down
8 changes: 6 additions & 2 deletions src/queryClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getReasonPhrase, StatusCodes } from 'http-status-codes';
import {
QueryClient, QueryClientProvider, useMutation, Hydrate, dehydrate
QueryClient,
QueryClientProvider,
useMutation,
Hydrate,
dehydrate,
} from 'react-query';
import { ReactQueryDevtools } from 'react-query/devtools';
import {
Expand Down Expand Up @@ -86,6 +90,6 @@ export default (config: Partial<QueryClientConfig>) => {
useMutation,
ReactQueryDevtools,
dehydrate,
Hydrate
Hydrate,
};
};
8 changes: 3 additions & 5 deletions src/ws/hooks/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export const configureWsItemHooks = (
}, [itemId]);
},


/**
* React hook to subscribe to the updates of the given item ID
* @param itemId The ID of the item of which to observe updates
Expand Down Expand Up @@ -116,16 +115,15 @@ export const configureWsItemHooks = (

websocketClient.subscribe(channel, handler);


return function cleanup() {
websocketClient.unsubscribe(channel, handler);
};
})
});

// eslint-disable-next-line consistent-return
return () => {
unsubscribeFunctions.forEach(f => f())
}
unsubscribeFunctions.forEach((f) => f());
};
}, [itemIds]);
},

Expand Down
2 changes: 1 addition & 1 deletion src/ws/hooks/membership.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
setUpWsTest,
} from '../../../test/wsUtils';
import { ITEMS, ITEM_MEMBERSHIPS_RESPONSE } from '../../../test/constants';
import { buildItemMembershipsKey, } from '../../config/keys';
import { buildItemMembershipsKey } from '../../config/keys';
import { configureWsMembershipHooks } from './membership';
import { KINDS, OPS, TOPICS } from '../constants';
import { Membership, PERMISSION_LEVELS } from '../../types';
Expand Down

0 comments on commit 93a109d

Please sign in to comment.