Split move to dialog into two separate dialogs
[arvados-workbench2.git] / src / views-components / create-project-dialog / create-project-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 { DialogProjectCreate } from "../dialog-create/dialog-project-create";
11 import { projectActions, createProject, getProjectList } from "~/store/project/project-action";
12 import { projectPanelActions } from "~/store/project-panel/project-panel-action";
13 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
14
15 const mapStateToProps = (state: RootState) => ({
16     open: state.projects.creator.opened
17 });
18
19 const addProject = (data: { name: string, description: string }) =>
20     (dispatch: Dispatch, getState: () => RootState) => {
21         const { ownerUuid } = getState().projects.creator;
22         return dispatch<any>(createProject(data)).then(() => {
23             dispatch(snackbarActions.OPEN_SNACKBAR({
24                 message: "Project has been successfully created.",
25                 hideDuration: 2000
26             }));
27             dispatch(projectPanelActions.REQUEST_ITEMS());
28             dispatch<any>(getProjectList(ownerUuid));
29         });
30     };
31
32 const mapDispatchToProps = (dispatch: Dispatch) => ({
33     handleClose: () => {
34         dispatch(projectActions.CLOSE_PROJECT_CREATOR());
35     },
36     onSubmit: (data: { name: string, description: string }) => {
37         return dispatch<any>(addProject(data))
38             .catch((e: any) => {
39                 throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Project with this name already exists." : "" });
40             });
41     }
42 });
43
44 export const CreateProjectDialog = connect(mapStateToProps, mapDispatchToProps)(DialogProjectCreate);