Merge branch '21449-lsf-maxruntime'
[arvados.git] / services / workbench2 / 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 {
7     reset,
8     startSubmit,
9     stopSubmit,
10     initialize,
11     FormErrors,
12     formValueSelector
13 } from 'redux-form';
14 import { RootState } from 'store/store';
15 import { getUserUuid } from "common/getuser";
16 import { dialogActions } from "store/dialog/dialog-actions";
17 import { getCommonResourceServiceError, CommonResourceServiceError } from 'services/common-service/common-resource-service';
18 import { ProjectResource } from 'models/project';
19 import { ServiceRepository } from 'services/services';
20 import { matchProjectRoute, matchRunProcessRoute } from 'routes/routes';
21 import { RouterState } from "react-router-redux";
22 import { GroupClass } from "models/group";
23 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
24 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
25
26 export interface ProjectCreateFormDialogData {
27     ownerUuid: string;
28     name: string;
29     description: string;
30     properties: ProjectProperties;
31 }
32
33 export interface ProjectProperties {
34     [key: string]: string | string[];
35 }
36
37 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
38 export const PROJECT_CREATE_PROPERTIES_FORM_NAME = 'projectCreatePropertiesFormName';
39 export const PROJECT_CREATE_FORM_SELECTOR = formValueSelector(PROJECT_CREATE_FORM_NAME);
40
41 export const isProjectOrRunProcessRoute = (router: RouterState) => {
42     const pathname = router.location ? router.location.pathname : '';
43     const matchProject = matchProjectRoute(pathname);
44     const matchRunProcess = matchRunProcessRoute(pathname);
45     return Boolean(matchProject || matchRunProcess);
46 };
47
48 export const openProjectCreateDialog = (ownerUuid: string) =>
49     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50         const { router } = getState();
51         if (!isProjectOrRunProcessRoute(router)) {
52             const userUuid = getUserUuid(getState());
53             if (!userUuid) { return; }
54             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid: userUuid }));
55         } else {
56             dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
57         }
58         dispatch(dialogActions.OPEN_DIALOG({
59             id: PROJECT_CREATE_FORM_NAME,
60             data: {
61                 sourcePanel: GroupClass.PROJECT,
62             }
63         }));
64     };
65
66 export const createProject = (project: Partial<ProjectResource>) =>
67     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68         dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
69         try {
70             dispatch(progressIndicatorActions.START_WORKING(PROJECT_CREATE_FORM_NAME));
71             const newProject = await services.projectService.create(project, false);
72             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
73             dispatch(reset(PROJECT_CREATE_FORM_NAME));
74             return newProject;
75         } catch (e) {
76             const error = getCommonResourceServiceError(e);
77             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
78                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
79             } else {
80                 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME));
81                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
82                 const errMsg = e.errors
83                     ? e.errors.join('')
84                     : 'There was an error while creating the collection';
85                 dispatch(snackbarActions.OPEN_SNACKBAR({
86                     message: errMsg,
87                     hideDuration: 2000,
88                     kind: SnackbarKind.ERROR
89                 }));
90             }
91             return undefined;
92         } finally {
93             dispatch(progressIndicatorActions.STOP_WORKING(PROJECT_CREATE_FORM_NAME));
94         }
95     };