14720: Fix tests
[arvados.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, FormErrors, formValueSelector, change } 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 import { ResourcePropertiesFormData } from '~/views-components/resource-properties-form/resource-properties-form';
14
15 export interface ProjectCreateFormDialogData {
16     ownerUuid: string;
17     name: string;
18     description: string;
19     properties: ProjectProperties;
20 }
21
22 export interface ProjectProperties {
23     [key: string]: string;
24 }
25
26 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
27 export const PROJECT_CREATE_PROPERTIES_FORM_NAME = 'projectCreatePropertiesFormName';
28 export const PROJECT_CREATE_FORM_SELECTOR = formValueSelector(PROJECT_CREATE_FORM_NAME);
29
30 export const isProjectOrRunProcessRoute = ({ router }: RootState) => {
31     const pathname = router.location ? router.location.pathname : '';
32     const matchProject = matchProjectRoute(pathname);
33     const matchRunProcess = matchRunProcessRoute(pathname);
34     return Boolean(matchProject || matchRunProcess);
35 };
36
37 export const isItemNotInProject = (properties: any) => {
38     if (properties.breadcrumbs) {
39         return Boolean(properties.breadcrumbs[0].label !== 'Projects');
40     } else {
41         return ;
42     }
43 };
44
45 export const openProjectCreateDialog = (ownerUuid: string) =>
46     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47         const router = getState();
48         const properties = getState().properties;
49         if (isItemNotInProject(properties) || !isProjectOrRunProcessRoute(router)) {
50             const userUuid = getState().auth.user!.uuid;
51             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { userUuid }));
52         } else {
53             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
54         }
55         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
56     };
57
58 export const createProject = (project: Partial<ProjectResource>) =>
59     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
60         dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
61         try {
62             const newProject = await services.projectService.create(project);
63             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
64             dispatch(reset(PROJECT_CREATE_FORM_NAME));
65             return newProject;
66         } catch (e) {
67             const error = getCommonResourceServiceError(e);
68             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
69                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
70             }
71             return undefined;
72         }
73     };
74
75 export const addPropertyToCreateProjectForm = (data: ResourcePropertiesFormData) =>
76     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
77         const properties = { ...PROJECT_CREATE_FORM_SELECTOR(getState(), 'properties') };
78         properties[data.key] = data.value;
79         dispatch(change(PROJECT_CREATE_FORM_NAME, 'properties', properties ));
80     };
81
82 export const removePropertyFromCreateProjectForm = (key: string) =>
83     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
84         const properties = { ...PROJECT_CREATE_FORM_SELECTOR(getState(), 'properties') };
85         delete properties[key];
86         dispatch(change(PROJECT_CREATE_FORM_NAME, 'properties', properties ));
87     };