9e9fea5263032ebb78fef86f4548a13c0fbb7db4
[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 { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { fileUploaderActions } from 'store/file-uploader/file-uploader-actions';
11 import { reset, startSubmit, stopSubmit } from 'redux-form';
12 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
13 import { collectionPanelFilesAction } from 'store/collection-panel/collection-panel-files/collection-panel-files-actions';
14 import { createTree } from 'models/tree';
15 import * as WorkbenchActions from 'store/workbench/workbench-actions';
16
17 export const uploadCollectionFiles = (collectionUuid: string, targetLocation?: string) =>
18     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
19         dispatch(fileUploaderActions.START_UPLOAD());
20         const files = getState().fileUploader.map(file => file.file);
21         await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch), targetLocation);
22         dispatch(WorkbenchActions.loadCollection(collectionUuid));
23         dispatch(fileUploaderActions.CLEAR_UPLOAD());
24     };
25
26 export const COLLECTION_UPLOAD_FILES_DIALOG = 'uploadCollectionFilesDialog';
27
28 export const openUploadCollectionFilesDialog = (targetLocation?: string) => (dispatch: Dispatch) => {
29     dispatch(reset(COLLECTION_UPLOAD_FILES_DIALOG));
30     dispatch(fileUploaderActions.CLEAR_UPLOAD());
31     dispatch<any>(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG, data: { targetLocation } }));
32 };
33
34 export const submitCollectionFiles = (targetLocation?: string) =>
35     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36         const currentCollection = getState().collectionPanel.item;
37         if (currentCollection) {
38             try {
39                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
40                 dispatch(startSubmit(COLLECTION_UPLOAD_FILES_DIALOG));
41                 await dispatch<any>(uploadCollectionFiles(currentCollection.uuid, targetLocation))
42                     .then(() => dispatch<any>(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() })));
43                 dispatch(closeUploadCollectionFilesDialog());
44                 dispatch(snackbarActions.OPEN_SNACKBAR({
45                     message: 'Data has been uploaded.',
46                     hideDuration: 2000,
47                     kind: SnackbarKind.SUCCESS
48                 }));
49                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
50             } catch (e) {
51                 dispatch(stopSubmit(COLLECTION_UPLOAD_FILES_DIALOG));
52                 dispatch(closeUploadCollectionFilesDialog());
53                 dispatch(snackbarActions.OPEN_SNACKBAR({
54                     message: 'Data has not been uploaded. Too large file',
55                     hideDuration: 2000,
56                     kind: SnackbarKind.ERROR
57                 }));
58                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPLOAD_FILES_DIALOG));
59             }
60         }
61     };
62
63 export const closeUploadCollectionFilesDialog = () => dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG });
64
65 const handleUploadProgress = (dispatch: Dispatch) => (fileId: number, loaded: number, total: number, currentTime: number) => {
66     dispatch(fileUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
67 };