diff --git a/src/components/pages/gallery/SelectedFileOptions.tsx b/src/components/pages/gallery/SelectedFileOptions.tsx index 7b6295b69..5cbe34a8b 100644 --- a/src/components/pages/gallery/SelectedFileOptions.tsx +++ b/src/components/pages/gallery/SelectedFileOptions.tsx @@ -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, diff --git a/src/pages/gallery/index.tsx b/src/pages/gallery/index.tsx index b4cc05874..f75b87fef 100644 --- a/src/pages/gallery/index.tsx +++ b/src/pages/gallery/index.tsx @@ -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,59 +317,30 @@ export default function Gallery() { if (!files) { return
; } - const addToCollectionHelper = async ( - collectionName: string, - collection: Collection - ) => { - loadingBar.current?.continuousStart(); - try { - await copyOrMoveFromCollection( - COLLECTION_OPS_TYPE.ADD, - setCollectionSelectorView, - selected, - files, - clearSelection, - syncWithRemote, - setActiveCollection, - collectionName, - collection - ); - } catch (e) { - setDialogMessage({ - title: constants.ERROR, - staticBackdrop: true, - close: { variant: 'danger' }, - content: constants.UNKNOWN_ERROR, - }); - } - }; + const collectionOpsHelper = + (ops: COLLECTION_OPS_TYPE) => async (collection: Collection) => { + loadingBar.current?.continuousStart(); + try { + await handleCollectionOps( + ops, + setCollectionSelectorView, + selected, + files, + clearSelection, + syncWithRemote, + setActiveCollection, + collection + ); + } catch (e) { + setDialogMessage({ + title: constants.ERROR, + staticBackdrop: true, + close: { variant: 'danger' }, + content: constants.UNKNOWN_ERROR, + }); + } + }; - 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,40 +376,33 @@ export default function Gallery() { } }; - const showCreateCollectionModal = (opsType: COLLECTION_OPS_TYPE) => { - 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, + const showCreateCollectionModal = (ops: COLLECTION_OPS_TYPE) => { + const callback = async (collectionName: string) => { + try { + 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, + close: { variant: 'danger' }, + content: constants.UNKNOWN_ERROR, }); - } catch (e) { - logError( - e, - 'showCreateCollectionModal called with incorrect attributes' - ); - setDialogMessage({ - title: constants.ERROR, - staticBackdrop: true, - close: { variant: 'danger' }, - content: constants.UNKNOWN_ERROR, + } + }; + return () => + setCollectionNamerAttributes({ + title: constants.CREATE_COLLECTION, + buttonText: constants.CREATE, + autoFilledName: '', + callback, }); - } }; const deleteFileHelper = async () => { @@ -605,7 +571,9 @@ export default function Gallery() { {selected.count > 0 && selected.collectionID === activeCollection && (