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