18169: Removed cancel disable when uploading
[arvados-workbench2.git] / src / store / collections / collection-upload-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { dialogActions } from 'store/dialog/dialog-actions';
9 import { loadCollectionFiles } from '../collection-panel/collection-panel-files/collection-panel-files-actions';
10 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
11 import { fileUploaderActions } from 'store/file-uploader/file-uploader-actions';
12 import { reset, startSubmit, stopSubmit } from 'redux-form';
13 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
14 import { collectionPanelFilesAction } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
15 import { createTree } from 'models/tree';
16 import { loadCollectionPanel } from '../collection-panel/collection-panel-action';
17 import * as WorkbenchActions from 'store/workbench/workbench-actions';
18
19 export const uploadCollectionFiles = (collectionUuid: string) =>
20     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
21         dispatch(fileUploaderActions.START_UPLOAD());
22         const files = getState().fileUploader.map(file => file.file);
23         await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch));
24         dispatch(WorkbenchActions.loadCollection(collectionUuid));
25         dispatch(fileUploaderActions.CLEAR_UPLOAD());
26     };
27
28 export const COLLECTION_UPLOAD_FILES_DIALOG = 'uploadCollectionFilesDialog';
29
30 export const openUploadCollectionFilesDialog = () => (dispatch: Dispatch) => {
31     dispatch(reset(COLLECTION_UPLOAD_FILES_DIALOG));
32     dispatch(fileUploaderActions.CLEAR_UPLOAD());
33     dispatch<any>(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG, data: {} }));
34 };
35
36 export const submitCollectionFiles = () =>
37     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         const currentCollection = getState().collectionPanel.item;
39         if (currentCollection) {
40             try {
41                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
42                 dispatch(startSubmit(COLLECTION_UPLOAD_FILES_DIALOG));
43                 await dispatch<any>(uploadCollectionFiles(currentCollection.uuid))
44                     .then(() => dispatch<any>(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() })));
45                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
46                 dispatch<any>(loadCollectionPanel(currentCollection.uuid));
47                 dispatch(closeUploadCollectionFilesDialog());
48                 dispatch(snackbarActions.OPEN_SNACKBAR({
49                     message: 'Data has been uploaded.',
50                     hideDuration: 2000,
51                     kind: SnackbarKind.SUCCESS
52                 }));
53                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
54             } catch (e) {
55                 dispatch(stopSubmit(COLLECTION_UPLOAD_FILES_DIALOG));
56                 dispatch(closeUploadCollectionFilesDialog());
57                 dispatch(snackbarActions.OPEN_SNACKBAR({
58                     message: 'Data has not been uploaded. Too large file',
59                     hideDuration: 2000,
60                     kind: SnackbarKind.ERROR
61                 }));
62                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
63             }
64         }
65     };
66
67 export const closeUploadCollectionFilesDialog = () => dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG });
68
69 const handleUploadProgress = (dispatch: Dispatch) => (fileId: number, loaded: number, total: number, currentTime: number) => {
70     dispatch(fileUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
71 };