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';
15 import { RouterState } from "react-router-redux";
17 export interface ProjectCreateFormDialogData {
21 properties: ProjectProperties;
24 export interface ProjectProperties {
25 [key: string]: string;
28 export const PROJECT_CREATE_FORM_NAME = 'projectCreateFormName';
29 export const PROJECT_CREATE_PROPERTIES_FORM_NAME = 'projectCreatePropertiesFormName';
30 export const PROJECT_CREATE_FORM_SELECTOR = formValueSelector(PROJECT_CREATE_FORM_NAME);
32 export const isProjectOrRunProcessRoute = (router: RouterState) => {
33 const pathname = router.location ? router.location.pathname : '';
34 const matchProject = matchProjectRoute(pathname);
35 const matchRunProcess = matchRunProcessRoute(pathname);
36 return Boolean(matchProject || matchRunProcess);
39 export const openProjectCreateDialog = (ownerUuid: string) =>
40 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41 const { router } = getState();
42 if (!isProjectOrRunProcessRoute(router)) {
43 const userUuid = getUserUuid(getState());
44 if (!userUuid) { return; }
45 dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid: userUuid }));
47 dispatch(initialize(PROJECT_CREATE_FORM_NAME, { ownerUuid }));
49 dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_CREATE_FORM_NAME, data: {} }));
52 export const createProject = (project: Partial<ProjectResource>) =>
53 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54 dispatch(startSubmit(PROJECT_CREATE_FORM_NAME));
56 const newProject = await services.projectService.create(project);
57 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_CREATE_FORM_NAME }));
58 dispatch(reset(PROJECT_CREATE_FORM_NAME));
61 const error = getCommonResourceServiceError(e);
62 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
63 dispatch(stopSubmit(PROJECT_CREATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
69 export const addPropertyToCreateProjectForm = (data: ResourcePropertiesFormData) =>
70 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
71 const properties = { ...PROJECT_CREATE_FORM_SELECTOR(getState(), 'properties') };
72 properties[data.key] = data.value;
73 dispatch(change(PROJECT_CREATE_FORM_NAME, 'properties', properties));
76 export const removePropertyFromCreateProjectForm = (key: string) =>
77 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
78 const properties = { ...PROJECT_CREATE_FORM_SELECTOR(getState(), 'properties') };
79 delete properties[key];
80 dispatch(change(PROJECT_CREATE_FORM_NAME, 'properties', properties));