Creation dialog with redux-form validation
[arvados-workbench2.git] / src / views-components / create-project-dialog / create-project-dialog.tsx
index 2cdd479033a009e23473e60dbebfc0a4f925cd74..f75c459347500da68ea97b196d7be098691b8bbd 100644 (file)
@@ -3,7 +3,9 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { connect } from "react-redux";
-import { Dispatch } from "../../../node_modules/redux";
+import { Dispatch } from "redux";
+import { SubmissionError } from "redux-form";
+
 import { RootState } from "../../store/store";
 import DialogProjectCreate from "../dialog-create/dialog-project-create";
 import actions, { createProject, getProjectList } from "../../store/project/project-action";
@@ -11,15 +13,13 @@ import dataExplorerActions from "../../store/data-explorer/data-explorer-action"
 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
 
 const mapStateToProps = (state: RootState) => ({
-    open: state.projects.creator.opened,
-    pending: state.projects.creator.pending,
-    error: state.projects.creator.error
+    open: state.projects.creator.opened
 });
 
-const submit = (data: { name: string, description: string }) =>
+export const addProject = (data: { name: string, description: string }) =>
     (dispatch: Dispatch, getState: () => RootState) => {
         const { ownerUuid } = getState().projects.creator;
-        dispatch<any>(createProject(data)).then(() => {
+        return dispatch<any>(createProject(data)).then(() => {
             dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
             dispatch<any>(getProjectList(ownerUuid));
         });
@@ -30,7 +30,10 @@ const mapDispatchToProps = (dispatch: Dispatch) => ({
         dispatch(actions.CLOSE_PROJECT_CREATOR());
     },
     onSubmit: (data: { name: string, description: string }) => {
-        dispatch<any>(submit(data));
+        return dispatch<any>(addProject(data))
+            .catch((e: any) => {
+                throw new SubmissionError({ name: e.errors.join("").includes("UniqueViolation") ? "Project with this name already exists." : "" });
+            });
     }
 });