Merge branch 'master' of git.curoverse.com:arvados-workbench2 into 13862-get-current...
[arvados-workbench2.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 { 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
15 export interface ProjectUpdateFormDialogData {
16     uuid: string;
17     name: string;
18     description: string;
19 }
20
21 export const PROJECT_UPDATE_FORM_NAME = 'projectUpdateFormName';
22
23 export const openProjectUpdateDialog = (resource: ContextMenuResource) =>
24     (dispatch: Dispatch, getState: () => RootState) => {
25         const project = getResource(resource.uuid)(getState().resources);
26         dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
27         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_UPDATE_FORM_NAME, data: {} }));
28     };
29
30 export const updateProject = (project: Partial<ProjectResource>) =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         const uuid = project.uuid || '';
33         dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
34         try {
35             const updatedProject = await services.projectService.update(uuid, project);
36             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
37             return updatedProject;
38         } catch (e) {
39             const error = getCommonResourceServiceError(e);
40             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
41                 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Project with the same name already exists.' }));
42             }
43             return ;
44         }
45     };