Update ownership error check
[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 { snackbarActions } from "~/store/snackbar/snackbar-actions";
13 import { UploadFile } from "~/store/collections/uploader/collection-uploader-actions";
14 import { projectPanelActions } from "~/store/project-panel/project-panel-action";
15
16 const mapStateToProps = (state: RootState) => ({
17     open: state.collections.creator.opened
18 });
19
20 const mapDispatchToProps = (dispatch: Dispatch) => ({
21     handleClose: () => {
22         dispatch(collectionCreateActions.CLOSE_COLLECTION_CREATOR());
23     },
24     onSubmit: (data: { name: string, description: string }, files: UploadFile[]) => {
25         return dispatch<any>(addCollection(data, files.map(f => f.file)))
26             .catch((e: any) => {
27                 throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Collection with this name already exists." : "" });
28             });
29     }
30 });
31
32 const addCollection = (data: { name: string, description: string }, files: File[]) =>
33     (dispatch: Dispatch) => {
34         return dispatch<any>(createCollection(data, files)).then(() => {
35             dispatch(snackbarActions.OPEN_SNACKBAR({
36                 message: "Collection has been successfully created.",
37                 hideDuration: 2000
38             }));
39             dispatch(projectPanelActions.REQUEST_ITEMS());
40         });
41     };
42
43 export const CreateCollectionDialog = connect(mapStateToProps, mapDispatchToProps)(DialogCollectionCreate);
44