Extract file-uploader, resotore file upload to collection creator
[arvados-workbench2.git] / src / store / collections / collection-upload-actions.ts
similarity index 57%
rename from src/store/collections/uploader/collection-uploader-actions.ts
rename to src/store/collections/collection-upload-actions.ts
index 58dcdc4cd1b31ffb05ccfcdecaedb5557d1893a3..c04ef1a24b7ead162f2df8fb1046982aef0b45c2 100644 (file)
@@ -1,63 +1,45 @@
-// Copyright (C) The Arvados Authors. All rights reserved.\r
-//\r
-// SPDX-License-Identifier: AGPL-3.0\r
-\r
-import { unionize, ofType, UnionOf } from "~/common/unionize";\r
-import { Dispatch } from 'redux';\r
-import { RootState } from '~/store/store';\r
-import { ServiceRepository } from '~/services/services';\r
-import { dialogActions } from '~/store/dialog/dialog-actions';\r
-import { loadCollectionFiles } from '../../collection-panel/collection-panel-files/collection-panel-files-actions';\r
-import { snackbarActions } from "~/store/snackbar/snackbar-actions";\r
-\r
-export interface UploadFile {\r
-    id: number;\r
-    file: File;\r
-    prevLoaded: number;\r
-    loaded: number;\r
-    total: number;\r
-    startTime: number;\r
-    prevTime: number;\r
-    currentTime: number;\r
-}\r
-\r
-export const collectionUploaderActions = unionize({\r
-    SET_UPLOAD_FILES: ofType<File[]>(),\r
-    START_UPLOAD: ofType(),\r
-    SET_UPLOAD_PROGRESS: ofType<{ fileId: number, loaded: number, total: number, currentTime: number }>(),\r
-    CLEAR_UPLOAD: ofType()\r
-});\r
-\r
-export type CollectionUploaderAction = UnionOf<typeof collectionUploaderActions>;\r
-\r
-export const uploadCollectionFiles = (collectionUuid: string) =>\r
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {\r
-        dispatch(collectionUploaderActions.START_UPLOAD());\r
-        const files = getState().collections.uploader.map(file => file.file);\r
-        await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch));\r
-        dispatch(collectionUploaderActions.CLEAR_UPLOAD());\r
-    };\r
-\r
-\r
-export const uploadCurrentCollectionFiles = () =>\r
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {\r
-        const currentCollection = getState().collectionPanel.item;\r
-        if (currentCollection) {\r
-            await dispatch<any>(uploadCollectionFiles(currentCollection.uuid));\r
-            dispatch<any>(loadCollectionFiles(currentCollection.uuid));\r
-            dispatch(closeUploadCollectionFilesDialog());\r
-            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Data has been uploaded.', hideDuration: 2000 }));\r
-        }\r
-    };\r
-\r
-export const UPLOAD_COLLECTION_FILES_DIALOG = 'uploadCollectionFilesDialog';\r
-export const openUploadCollectionFilesDialog = () => (dispatch: Dispatch) => {\r
-    dispatch(collectionUploaderActions.CLEAR_UPLOAD());\r
-    dispatch<any>(dialogActions.OPEN_DIALOG({ id: UPLOAD_COLLECTION_FILES_DIALOG, data: {} }));\r
-};\r
-\r
-export const closeUploadCollectionFilesDialog = () => dialogActions.CLOSE_DIALOG({ id: UPLOAD_COLLECTION_FILES_DIALOG });\r
-\r
-const handleUploadProgress = (dispatch: Dispatch) => (fileId: number, loaded: number, total: number, currentTime: number) => {\r
-    dispatch(collectionUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));\r
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { Dispatch } from 'redux';
+import { RootState } from '~/store/store';
+import { ServiceRepository } from '~/services/services';
+import { dialogActions } from '~/store/dialog/dialog-actions';
+import { loadCollectionFiles } from '../collection-panel/collection-panel-files/collection-panel-files-actions';
+import { snackbarActions } from '~/store/snackbar/snackbar-actions';
+import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
+import { reset } from 'redux-form';
+
+export const uploadCollectionFiles = (collectionUuid: string) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(fileUploaderActions.START_UPLOAD());
+        const files = getState().fileUploader.map(file => file.file);
+        await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch));
+        dispatch(fileUploaderActions.CLEAR_UPLOAD());
+    };
+
+export const UPLOAD_COLLECTION_FILES_DIALOG = 'uploadCollectionFilesDialog';
+
+export const openUploadCollectionFilesDialog = () => (dispatch: Dispatch) => {
+    dispatch(reset(UPLOAD_COLLECTION_FILES_DIALOG));
+    dispatch(fileUploaderActions.CLEAR_UPLOAD());
+    dispatch<any>(dialogActions.OPEN_DIALOG({ id: UPLOAD_COLLECTION_FILES_DIALOG, data: {} }));
+};
+
+export const uploadCurrentCollectionFiles = () =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const currentCollection = getState().collectionPanel.item;
+        if (currentCollection) {
+            await dispatch<any>(uploadCollectionFiles(currentCollection.uuid));
+            dispatch<any>(loadCollectionFiles(currentCollection.uuid));
+            dispatch(closeUploadCollectionFilesDialog());
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Data has been uploaded.', hideDuration: 2000 }));
+        }
+    };
+
+export const closeUploadCollectionFilesDialog = () => dialogActions.CLOSE_DIALOG({ id: UPLOAD_COLLECTION_FILES_DIALOG });
+
+const handleUploadProgress = (dispatch: Dispatch) => (fileId: number, loaded: number, total: number, currentTime: number) => {
+    dispatch(fileUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
 };
\ No newline at end of file