15407: Simplifies code handling workflow json mounts.
[arvados.git] / src / store / projects / project-update-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 { FormErrors, initialize, startSubmit, stopSubmit } 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 { ServiceRepository } from "~/services/services";
11 import { ProjectResource } from '~/models/project';
12 import { ContextMenuResource } from "~/store/context-menu/context-menu-actions";
13 import { getResource } from '~/store/resources/resources';
14 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
15
16 export interface ProjectUpdateFormDialogData {
17     uuid: string;
18     name: string;
19     description: string;
20 }
21
22 export const PROJECT_UPDATE_FORM_NAME = 'projectUpdateFormName';
23
24 export const openProjectUpdateDialog = (resource: ContextMenuResource) =>
25     (dispatch: Dispatch, getState: () => RootState) => {
26         const project = getResource(resource.uuid)(getState().resources);
27         dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
28         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_UPDATE_FORM_NAME, data: {} }));
29     };
30
31 export const updateProject = (project: Partial<ProjectResource>) =>
32     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33         const uuid = project.uuid || '';
34         dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
35         try {
36             const updatedProject = await services.projectService.update(uuid, project);
37             dispatch(projectPanelActions.REQUEST_ITEMS());
38             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
39             return updatedProject;
40         } catch (e) {
41             const error = getCommonResourceServiceError(e);
42             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
43                 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
44             }
45             return ;
46         }
47     };