1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
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";
26 export interface ProjectCreateFormDialogData {
30 properties: ProjectProperties;
33 export interface ProjectProperties {
34 [key: string]: string | string[];
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);
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);
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 }));
56 dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
58 dispatch(dialogActions.OPEN_DIALOG({
59 id: PROJECT_CREATE_FORM_NAME,
61 sourcePanel: GroupClass.PROJECT,
66 export const createProject = (project: Partial<ProjectResource>) =>
67 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
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));
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));
80 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME));
81 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
82 const errMsg = e.errors
84 : 'There was an error while creating the collection';
85 dispatch(snackbarActions.OPEN_SNACKBAR({
88 kind: SnackbarKind.ERROR
93 dispatch(progressIndicatorActions.STOP_WORKING(PROJECT_CREATE_FORM_NAME));