Handle SELECT_WORKFLOW_PRESET action in reducer
[arvados-workbench2.git] / src / store / projects / project-create-actions.ts
index 6d704c207edb6c5b1cd463466923303cf251ae72..d226048b08c06b4e1fd9ca868bf63ea2ddd92beb 100644 (file)
@@ -3,12 +3,13 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Dispatch } from "redux";
-import { reset, startSubmit, stopSubmit, initialize } from 'redux-form';
+import { reset, startSubmit, stopSubmit, initialize, FormErrors } from 'redux-form';
 import { RootState } from '~/store/store';
 import { dialogActions } from "~/store/dialog/dialog-actions";
 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
 import { ProjectResource } from '~/models/project';
 import { ServiceRepository } from '~/services/services';
+import { matchProjectRoute, matchRunProcessRoute } from '~/routes/routes';
 
 export interface ProjectCreateFormDialogData {
     ownerUuid: string;
@@ -18,9 +19,31 @@ export interface ProjectCreateFormDialogData {
 
 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
 
+export const isProjectOrRunProcessRoute = ({ router }: RootState) => {
+    const pathname = router.location ? router.location.pathname : '';
+    const matchProject = matchProjectRoute(pathname);
+    const matchRunProcess = matchRunProcessRoute(pathname);
+    return Boolean(matchProject || matchRunProcess);
+};
+
+export const isItemNotInProject = (properties: any) => {
+    if (properties.breadcrumbs) {
+        return Boolean(properties.breadcrumbs[0].label !== 'Projects');
+    } else {
+        return ;
+    }
+};
+
 export const openProjectCreateDialog = (ownerUuid: string) =>
-    (dispatch: Dispatch) => {
-        dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const router = getState();
+        const properties = getState().properties;
+        if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
+            const userUuid = getState().auth.user!.uuid;
+            dispatch(initialize(PROJECT_CREATE_FORM_NAME, { userUuid }));
+        } else {
+            dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
+        }
         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
     };
 
@@ -35,7 +58,7 @@ export const createProject = (project: Partial<ProjectResource>) =>
         } catch (e) {
             const error = getCommonResourceServiceError(e);
             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
-                dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' }));
+                dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
             }
             return undefined;
         }