refactors collection ops code

This commit is contained in:
abhinav-grd 2021-09-28 13:33:32 +05:30
parent b7497ab647
commit 29d02afba8
4 changed files with 67 additions and 111 deletions

View file

@ -18,14 +18,8 @@ import { Collection } from 'services/collectionService';
import RemoveIcon from 'components/icons/RemoveIcon';
interface Props {
addToCollectionHelper: (
collectionName: string,
collection: Collection
) => void;
moveToCollectionHelper: (
collectionName: string,
collection: Collection
) => void;
addToCollectionHelper: (collection: Collection) => void;
moveToCollectionHelper: (collection: Collection) => void;
showCreateCollectionModal: (opsType: COLLECTION_OPS_TYPE) => () => void;
setDialogMessage: SetDialogMessage;
setCollectionSelectorAttributes: SetCollectionSelectorAttributes;
@ -75,7 +69,7 @@ const SelectedFileOptions = ({
}: Props) => {
const addToCollection = () =>
setCollectionSelectorAttributes({
callback: (collection) => addToCollectionHelper(null, collection),
callback: addToCollectionHelper,
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.ADD),
title: constants.ADD_TO_COLLECTION,
fromCollection: activeCollection,
@ -109,7 +103,7 @@ const SelectedFileOptions = ({
const moveToCollection = () => {
setCollectionSelectorAttributes({
callback: (collection) => moveToCollectionHelper(null, collection),
callback: moveToCollectionHelper,
showNextModal: showCreateCollectionModal(COLLECTION_OPS_TYPE.MOVE),
title: constants.MOVE_TO_COLLECTION,
fromCollection: activeCollection,

View file

@ -25,6 +25,8 @@ import {
getFavItemIds,
getLocalCollections,
getNonEmptyCollections,
createCollection,
CollectionType,
} from 'services/collectionService';
import constants from 'utils/strings/constants';
import billingService from 'services/billingService';
@ -68,9 +70,9 @@ import { AppContext } from 'pages/_app';
import { CustomError, ServerErrorCodes } from 'utils/common/errorUtil';
import { PAGES } from 'types';
import {
copyOrMoveFromCollection,
COLLECTION_OPS_TYPE,
isSharedCollection,
handleCollectionOps,
} from 'utils/collection';
import { logError } from 'utils/sentry';
@ -315,21 +317,18 @@ export default function Gallery() {
if (!files) {
return <div />;
}
const addToCollectionHelper = async (
collectionName: string,
collection: Collection
) => {
const collectionOpsHelper =
(ops: COLLECTION_OPS_TYPE) => async (collection: Collection) => {
loadingBar.current?.continuousStart();
try {
await copyOrMoveFromCollection(
COLLECTION_OPS_TYPE.ADD,
await handleCollectionOps(
ops,
setCollectionSelectorView,
selected,
files,
clearSelection,
syncWithRemote,
setActiveCollection,
collectionName,
collection
);
} catch (e) {
@ -342,32 +341,6 @@ export default function Gallery() {
}
};
const moveToCollectionHelper = async (
collectionName: string,
collection: Collection
) => {
loadingBar.current?.continuousStart();
try {
await copyOrMoveFromCollection(
COLLECTION_OPS_TYPE.MOVE,
setCollectionSelectorView,
selected,
files,
clearSelection,
syncWithRemote,
setActiveCollection,
collectionName,
collection
);
} catch (e) {
setDialogMessage({
title: constants.ERROR,
staticBackdrop: true,
close: { variant: 'danger' },
content: constants.UNKNOWN_ERROR,
});
}
};
const changeFilesVisibilityHelper = async (
visibility: VISIBILITY_STATE
) => {
@ -403,33 +376,18 @@ export default function Gallery() {
}
};
const showCreateCollectionModal = (opsType: COLLECTION_OPS_TYPE) => {
const showCreateCollectionModal = (ops: COLLECTION_OPS_TYPE) => {
const callback = async (collectionName: string) => {
try {
let callback = null;
switch (opsType) {
case COLLECTION_OPS_TYPE.ADD:
callback = (collectionName: string) =>
addToCollectionHelper(collectionName, null);
break;
case COLLECTION_OPS_TYPE.MOVE:
callback = (collectionName: string) =>
moveToCollectionHelper(collectionName, null);
break;
default:
throw Error(CustomError.INVALID_COLLECTION_OPERATION);
}
return () =>
setCollectionNamerAttributes({
title: constants.CREATE_COLLECTION,
buttonText: constants.CREATE,
autoFilledName: '',
callback,
});
} catch (e) {
logError(
e,
'showCreateCollectionModal called with incorrect attributes'
const collection = await createCollection(
collectionName,
CollectionType.album,
collections
);
await collectionOpsHelper(ops)(collection);
} catch (e) {
logError(e, 'create and collection ops failed');
setDialogMessage({
title: constants.ERROR,
staticBackdrop: true,
@ -438,6 +396,14 @@ export default function Gallery() {
});
}
};
return () =>
setCollectionNamerAttributes({
title: constants.CREATE_COLLECTION,
buttonText: constants.CREATE,
autoFilledName: '',
callback,
});
};
const deleteFileHelper = async () => {
loadingBar.current?.continuousStart();
@ -605,7 +571,9 @@ export default function Gallery() {
{selected.count > 0 &&
selected.collectionID === activeCollection && (
<SelectedFileOptions
addToCollectionHelper={addToCollectionHelper}
addToCollectionHelper={collectionOpsHelper(
COLLECTION_OPS_TYPE.ADD
)}
archiveFilesHelper={() =>
changeFilesVisibilityHelper(
VISIBILITY_STATE.ARCHIVED
@ -616,7 +584,9 @@ export default function Gallery() {
VISIBILITY_STATE.VISIBLE
)
}
moveToCollectionHelper={moveToCollectionHelper}
moveToCollectionHelper={collectionOpsHelper(
COLLECTION_OPS_TYPE.ADD
)}
showCreateCollectionModal={
showCreateCollectionModal
}

View file

@ -1,8 +1,6 @@
import {
addToCollection,
Collection,
CollectionType,
createCollection,
moveToCollection,
} from 'services/collectionService';
import { getSelectedFiles } from 'utils/file';
@ -15,8 +13,9 @@ import { getData, LS_KEYS } from 'utils/storage/localStorage';
export enum COLLECTION_OPS_TYPE {
ADD,
MOVE,
REMOVE,
}
export async function copyOrMoveFromCollection(
export async function handleCollectionOps(
type: COLLECTION_OPS_TYPE,
setCollectionSelectorView: (value: boolean) => void,
selected: SelectedState,
@ -24,19 +23,9 @@ export async function copyOrMoveFromCollection(
clearSelection: () => void,
syncWithRemote: () => Promise<void>,
setActiveCollection: (id: number) => void,
collectionName: string,
existingCollection: Collection
collection: Collection
) {
setCollectionSelectorView(false);
let collection: Collection;
if (!existingCollection) {
collection = await createCollection(
collectionName,
CollectionType.album
);
} else {
collection = existingCollection;
}
const selectedFiles = getSelectedFiles(selected, files);
switch (type) {
case COLLECTION_OPS_TYPE.ADD:
@ -49,6 +38,8 @@ export async function copyOrMoveFromCollection(
selectedFiles
);
break;
case COLLECTION_OPS_TYPE.REMOVE:
break;
default:
throw Error(CustomError.INVALID_COLLECTION_OPERATION);
}

View file

@ -27,6 +27,7 @@ export enum CustomError {
SIGNUP_FAILED = 'signup failed',
FAV_COLLECTION_MISSING = 'favorite collection missing',
INVALID_COLLECTION_OPERATION = 'invalid collection operation',
INVALID_COLLECTION = 'invalid collection attribute',
}
function parseUploadError(error: AxiosResponse) {