Merge branch 'master' into 14318-new-button-missing-in-several-places
[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 } 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 isProjectRoute = ({ router }: RootState) => {
23     const pathname = router.location ? router.location.pathname : '';
24     const match = matchProjectRoute(pathname);
25     return !!match;
26 };
27
28 interface Properties {
29     breadcrumbs: Array<{ uuid: string, label: string }>;
30 }
31
32 export const isItemNotInProject = (properties: Properties) => {
33     if (properties.breadcrumbs) {
34         const isItemSharedWithMe = properties.breadcrumbs[0].label !== 'Projects';
35         return isItemSharedWithMe;
36     } else {
37         return false;
38     }
39 };
40
41 export const isNotProjectItem = () => {
42     return isItemNotInProject || !isProjectRoute;
43 };
44
45 export const openProjectCreateDialog = (ownerUuid: string) =>
46     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47         if (isNotProjectItem) {
48             const userUuid = getState().auth.user!.uuid;
49             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { userUuid }));
50         } else {
51             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
52         }        
53         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
54     };
55
56 export const createProject = (project: Partial<ProjectResource>) =>
57     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58         dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
59         try {
60             const newProject = await services.projectService.create(project);
61             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
62             dispatch(reset(PROJECT_CREATE_FORM_NAME));
63             return newProject;
64         } catch (e) {
65             const error = getCommonResourceServiceError(e);
66             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
67                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' }));
68             }
69             return undefined;
70         }
71     };