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 { 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';
16 export interface ProjectCreateFormDialogData {
20 properties: ProjectProperties;
23 export interface ProjectProperties {
24 [key: string]: string;
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);
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);
38 export const isItemNotInProject = (properties: any) => {
39 if (properties.breadcrumbs) {
40 return Boolean(properties.breadcrumbs[0].label !== 'Projects');
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 }));
55 dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
57 dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
60 export const createProject = (project: Partial<ProjectResource>) =>
61 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
62 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
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));
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));
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));
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));