Connect shared with me to the service
[arvados.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 } from '~/store/snackbar/snackbar-actions';
11 import { fileUploaderActions } from '~/store/file-uploader/file-uploader-actions';
12 import { reset, startSubmit } from 'redux-form';
13
14 export const uploadCollectionFiles = (collectionUuid: string) =>
15     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
16         dispatch(fileUploaderActions.START_UPLOAD());
17         const files = getState().fileUploader.map(file => file.file);
18         await services.collectionService.uploadFiles(collectionUuid, files, handleUploadProgress(dispatch));
19         dispatch(fileUploaderActions.CLEAR_UPLOAD());
20     };
21
22 export const COLLECTION_UPLOAD_FILES_DIALOG = 'uploadCollectionFilesDialog';
23
24 export const openUploadCollectionFilesDialog = () => (dispatch: Dispatch) => {
25     dispatch(reset(COLLECTION_UPLOAD_FILES_DIALOG));
26     dispatch(fileUploaderActions.CLEAR_UPLOAD());
27     dispatch<any>(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG, data: {} }));
28 };
29
30 export const submitCollectionFiles = () =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         const currentCollection = getState().collectionPanel.item;
33         if (currentCollection) {
34             dispatch(startSubmit(COLLECTION_UPLOAD_FILES_DIALOG));
35             await dispatch<any>(uploadCollectionFiles(currentCollection.uuid));
36             dispatch<any>(loadCollectionFiles(currentCollection.uuid));
37             dispatch(closeUploadCollectionFilesDialog());
38             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Data has been uploaded.', hideDuration: 2000 }));
39         }
40     };
41
42 export const closeUploadCollectionFilesDialog = () => dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPLOAD_FILES_DIALOG });
43
44 const handleUploadProgress = (dispatch: Dispatch) => (fileId: number, loaded: number, total: number, currentTime: number) => {
45     dispatch(fileUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
46 };