From 02ee68e5a38d57d069529ecd83fd5263d130f55d Mon Sep 17 00:00:00 2001 From: Rushikesh Tote Date: Sun, 20 Mar 2022 15:59:29 +0530 Subject: [PATCH] refactor --- .../pages/gallery/FileTypeChoiceModal.tsx | 7 ++- src/components/pages/gallery/Upload.tsx | 26 +++++--- src/services/importService.ts | 62 +++++++++++-------- src/utils/strings/englishConstants.tsx | 3 + 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/components/pages/gallery/FileTypeChoiceModal.tsx b/src/components/pages/gallery/FileTypeChoiceModal.tsx index 4f45fff9d..ce7fbc0dc 100644 --- a/src/components/pages/gallery/FileTypeChoiceModal.tsx +++ b/src/components/pages/gallery/FileTypeChoiceModal.tsx @@ -2,6 +2,7 @@ import { Modal, Button } from 'react-bootstrap'; import React from 'react'; import ImportService from 'services/importService'; import { getElectronFiles } from 'utils/upload'; +import constants from 'utils/strings/constants'; export default function FileTypeChoiceModal({ setElectronFiles, @@ -33,7 +34,7 @@ export default function FileTypeChoiceModal({ centered> - Upload + {constants.CHOOSE_UPLOAD_TYPE} diff --git a/src/components/pages/gallery/Upload.tsx b/src/components/pages/gallery/Upload.tsx index d752e41c2..8924bf1f9 100644 --- a/src/components/pages/gallery/Upload.tsx +++ b/src/components/pages/gallery/Upload.tsx @@ -149,13 +149,23 @@ export default function Upload(props: Props) { const uploadFailedFiles = async () => { try { - uploadInit(); - const { files, collections } = + const { files, collectionName, collectionIDs } = await ImportService.getToUploadFiles(); - await uploadFiles( - files as FileWithCollection[], - collections as Collection[] - ); + + toUploadFiles = files; + + if (collectionName) { + uploadToSingleNewCollection(collectionName); + } else { + uploadInit(); + const filesWithCollectionToUpload: FileWithCollection[] = + toUploadFiles.map((file, index) => ({ + file, + localID: index, + collectionID: collectionIDs[index], + })); + await uploadFiles(filesWithCollectionToUpload); + } } catch (e) { logError(e, 'Failed to upload previously failed files'); } finally { @@ -218,7 +228,6 @@ export default function Upload(props: Props) { const uploadFilesToExistingCollection = async (collection) => { try { - console.log('uploadFilesToExistingCollection'); uploadInit(); const filesWithCollectionToUpload: FileWithCollection[] = toUploadFiles.map((file, index) => ({ @@ -237,7 +246,6 @@ export default function Upload(props: Props) { collectionName?: string ) => { try { - console.log('uploadFilesToNewCollections'); uploadInit(); const filesWithCollectionToUpload: FileWithCollection[] = []; @@ -290,7 +298,6 @@ export default function Upload(props: Props) { filesWithCollectionToUpload: FileWithCollection[], collections?: Collection[] ) => { - console.log(collections); try { props.setUploadInProgress(true); props.closeCollectionSelector(); @@ -341,7 +348,6 @@ export default function Upload(props: Props) { }; const uploadToSingleNewCollection = (collectionName: string) => { - console.log('uploadToSingleNewCollection'); if (collectionName) { uploadFilesToNewCollections( UPLOAD_STRATEGY.SINGLE_COLLECTION, diff --git a/src/services/importService.ts b/src/services/importService.ts index 2ebe6ee54..f487a8567 100644 --- a/src/services/importService.ts +++ b/src/services/importService.ts @@ -1,11 +1,7 @@ import { Collection } from 'types/collection'; import { ElectronFile, FileWithCollection } from 'types/upload'; import { runningInBrowser } from 'utils/common'; - -interface FilesAndCollections { - files: FileWithCollection[]; - collections: Collection[]; -} +import { getElectronFiles } from 'utils/upload'; class ImportService { ElectronAPIs: any; @@ -16,28 +12,9 @@ class ImportService { this.allElectronAPIsExist = !!this.ElectronAPIs?.exists; } - async setToUploadFiles( - filesWithCollectionToUpload: FileWithCollection[], - collections?: Collection[] - ) { - if (this.allElectronAPIsExist) { - this.ElectronAPIs.setToUploadFiles( - filesWithCollectionToUpload, - collections, - false - ); - } - } - async setDoneUploadingFiles() { if (this.allElectronAPIsExist) { - this.ElectronAPIs.setToUploadFiles([], [], true); - } - } - - async getToUploadFiles(): Promise { - if (this.allElectronAPIsExist) { - return this.ElectronAPIs.getToUploadFiles(); + this.ElectronAPIs.setToUploadFiles(null, null, null, true); } } @@ -64,5 +41,40 @@ class ImportService { return this.ElectronAPIs.showUploadDirsDialog(); } } + + async getToUploadFiles() { + if (this.allElectronAPIsExist) { + const { filesPaths, collectionName, collectionIDs } = + this.ElectronAPIs.getToUploadFiles(); + const files = await getElectronFiles(filesPaths); + return { + files, + collectionName, + collectionIDs, + }; + } + } + + async setToUploadFiles( + files: FileWithCollection[], + collections?: Collection[] + ) { + if (this.allElectronAPIsExist) { + let collectionName; + if (collections?.length > 0) { + collectionName = collections[0].name; + } + const filePaths = files.map( + (file) => (file.file as ElectronFile).path + ); + const collectionIDs = files.map((file) => file.collectionID); + this.ElectronAPIs.setToUploadFiles( + filePaths, + collectionName, + collectionIDs, + false + ); + } + } } export default new ImportService(); diff --git a/src/utils/strings/englishConstants.tsx b/src/utils/strings/englishConstants.tsx index 8252be11c..eb0b6fedf 100644 --- a/src/utils/strings/englishConstants.tsx +++ b/src/utils/strings/englishConstants.tsx @@ -675,6 +675,9 @@ const englishConstants = { LIVE_PHOTO: 'this is a live photo', LIVE: 'LIVE', DOWNLOAD_UPLOAD_LOGS: 'debug logs', + CHOOSE_UPLOAD_TYPE: 'Choose Upload Type', + UPLOAD_FILES: 'Upload Files', + UPLOAD_DIRS: 'Upload Folders', }; export default englishConstants;