merge master
[arvados.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
15 const mapStateToProps = (state: RootState) => ({
16     open: state.collectionCreation.creator.opened
17 });
18
19 const mapDispatchToProps = (dispatch: Dispatch) => ({
20     handleClose: () => {
21         dispatch(collectionCreateActions.CLOSE_COLLECTION_CREATOR());
22     },
23     onSubmit: (data: { name: string, description: string }) => {
24         return dispatch<any>(addCollection(data))
25             .catch((e: any) => {
26                 throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Collection with this name already exists." : "" });
27             });
28     }
29 });
30
31 const addCollection = (data: { name: string, description: string }) =>
32     (dispatch: Dispatch) => {
33         return dispatch<any>(createCollection(data)).then(() => {
34             dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
35         });
36     };
37
38 export const CreateCollectionDialog = connect(mapStateToProps, mapDispatchToProps)(DialogCollectionCreate);
39