From 29d02afba848cd65c4d2b6d4e94a8fc8357c18ad Mon Sep 17 00:00:00 2001 From: abhinav-grd Date: Tue, 28 Sep 2021 13:33:32 +0530 Subject: [PATCH] refactors collection ops code --- .../pages/gallery/SelectedFileOptions.tsx | 14 +- src/pages/gallery/index.tsx | 144 +++++++----------- src/utils/collection/index.ts | 19 +-- src/utils/common/errorUtil.ts | 1 + 4 files changed, 67 insertions(+), 111 deletions(-) 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 && ( 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 } diff --git a/src/utils/collection/index.ts b/src/utils/collection/index.ts index 1c13c40c1..a06f47614 100644 --- a/src/utils/collection/index.ts +++ b/src/utils/collection/index.ts @@ -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, 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); } diff --git a/src/utils/common/errorUtil.ts b/src/utils/common/errorUtil.ts index 9bca94c37..65964cefe 100644 --- a/src/utils/common/errorUtil.ts +++ b/src/utils/common/errorUtil.ts @@ -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) {