creating-a-new-project-or-collection-anywhere-in-the-app-creates-it-in-projects-folder
[arvados-workbench2.git] / src / store / projects / project-create-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { reset, startSubmit, stopSubmit, initialize } from 'redux-form';
7 import { RootState } from '~/store/store';
8 import { dialogActions } from "~/store/dialog/dialog-actions";
9 import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
10 import { ProjectResource } from '~/models/project';
11 import { ServiceRepository } from '~/services/services';
12 import { matchProjectRoute, matchRunProcessRoute } from '~/routes/routes';
13
14 export interface ProjectCreateFormDialogData {
15     ownerUuid: string;
16     name: string;
17     description: string;
18 }
19
20 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
21
22 export const isProjectOrRunProcessRoute = ({ router }: RootState) => {
23     const pathname = router.location ? router.location.pathname : '';
24     const matchProject = matchProjectRoute(pathname);
25     const matchRunProcess = matchRunProcessRoute(pathname);
26     return Boolean(matchProject || matchRunProcess);
27 };
28
29 export const isItemNotInProject = (properties: any) => {
30     if (properties.breadcrumbs) {
31         return Boolean(properties.breadcrumbs[0].label !== 'Projects');
32     } else {
33         return ;
34     }
35 };
36
37 export const openProjectCreateDialog = (ownerUuid: string) =>
38     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39         const router = getState();
40         const properties = getState().properties;
41         if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
42             const userUuid = getState().auth.user!.uuid;
43             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { userUuid }));
44         } else {
45             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
46         }        
47         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
48     };
49
50 export const createProject = (project: Partial<ProjectResource>) =>
51     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52         dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
53         try {
54             const newProject = await services.projectService.create(project);
55             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
56             dispatch(reset(PROJECT_CREATE_FORM_NAME));
57             return newProject;
58         } catch (e) {
59             const error = getCommonResourceServiceError(e);
60             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
61                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' }));
62             }
63             return undefined;
64         }
65     };