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