1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
15 export interface ProjectCreateFormDialogData {
19 properties: ProjectProperties;
22 export interface ProjectProperties {
23 [key: string]: string;
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);
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);
37 export const isItemNotInProject = (properties: any) => {
38 if (properties.breadcrumbs) {
39 return Boolean(properties.breadcrumbs[0].label !== 'Projects');
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 }));
53 dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
55 dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
58 export const createProject = (project: Partial<ProjectResource>) =>
59 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
60 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
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));
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));
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 ));
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 ));