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