Add multiple files uploading
[arvados-workbench2.git] / src / views-components / create-collection-dialog / create-collection-dialog.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { connect } from "react-redux";
6 import { Dispatch } from "redux";
7 import { SubmissionError } from "redux-form";
8
9 import { RootState } from "../../store/store";
10 import { DialogCollectionCreate } from "../dialog-create/dialog-collection-create";
11 import { collectionCreateActions, createCollection } from "../../store/collections/creator/collection-creator-action";
12 import { dataExplorerActions } from "../../store/data-explorer/data-explorer-action";
13 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
14 import { snackbarActions } from "../../store/snackbar/snackbar-actions";
15 import { ServiceRepository } from "../../services/services";
16 import { CollectionResource } from "../../models/collection";
17
18 const mapStateToProps = (state: RootState) => ({
19     open: state.collections.creator.opened
20 });
21
22 const mapDispatchToProps = (dispatch: Dispatch) => ({
23     handleClose: () => {
24         dispatch(collectionCreateActions.CLOSE_COLLECTION_CREATOR());
25     },
26     onSubmit: (data: { name: string, description: string, files: File[] }) => {
27         return dispatch<any>(addCollection(data))
28             .catch((e: any) => {
29                 throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Collection with this name already exists." : "" });
30             });
31     }
32 });
33
34 const addCollection = (data: { name: string, description: string, files: File[] }) =>
35     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36         return dispatch<any>(createCollection(data)).then((collection: CollectionResource) => {
37             dispatch(snackbarActions.OPEN_SNACKBAR({
38                 message: "Collection has been successfully created.",
39                 hideDuration: 2000
40             }));
41             services.collectionService.uploadFiles(collection.uuid, data.files).then(() => {
42                 dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
43             });
44         });
45     };
46
47 export const CreateCollectionDialog = connect(mapStateToProps, mapDispatchToProps)(DialogCollectionCreate);
48