Merge remote-tracking branch 'origin/main' into 18692-frozen-projects-workbench-support
[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 {
7     FormErrors,
8     formValueSelector,
9     initialize,
10     reset,
11     startSubmit,
12     stopSubmit
13 } from 'redux-form';
14 import { RootState } from "store/store";
15 import { dialogActions } from "store/dialog/dialog-actions";
16 import {
17     getCommonResourceServiceError,
18     CommonResourceServiceError
19 } from "services/common-service/common-resource-service";
20 import { ServiceRepository } from "services/services";
21 import { projectPanelActions } from 'store/project-panel/project-panel-action';
22 import { GroupClass } from "models/group";
23 import { Participant } from "views-components/sharing-dialog/participant-select";
24 import { ProjectProperties } from "./project-create-actions";
25 import { getResource } from "store/resources/resources";
26 import { ProjectResource } from "models/project";
27
28 export interface ProjectUpdateFormDialogData {
29     uuid: string;
30     name: string;
31     users?: Participant[];
32     description?: string;
33     properties?: ProjectProperties;
34 }
35
36 export const PROJECT_UPDATE_FORM_NAME = 'projectUpdateFormName';
37 export const PROJECT_UPDATE_PROPERTIES_FORM_NAME = 'projectUpdatePropertiesFormName';
38 export const PROJECT_UPDATE_FORM_SELECTOR = formValueSelector(PROJECT_UPDATE_FORM_NAME);
39
40 export const openProjectUpdateDialog = (resource: ProjectUpdateFormDialogData) =>
41     (dispatch: Dispatch, getState: () => RootState) => {
42         // Get complete project resource from store to handle consumers passing in partial resources
43         const project = getResource<ProjectResource>(resource.uuid)(getState().resources);
44         dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
45         dispatch(dialogActions.OPEN_DIALOG({
46             id: PROJECT_UPDATE_FORM_NAME,
47             data: {
48                 sourcePanel: GroupClass.PROJECT,
49             }
50         }));
51     };
52
53 export const updateProject = (project: ProjectUpdateFormDialogData) =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         const uuid = project.uuid || '';
56         dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
57         try {
58             const updatedProject = await services.projectService.update(
59                 uuid,
60                 {
61                     name: project.name,
62                     description: project.description,
63                     properties: project.properties,
64                 });
65             dispatch(projectPanelActions.REQUEST_ITEMS());
66             dispatch(reset(PROJECT_UPDATE_FORM_NAME));
67             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
68             return updatedProject;
69         } catch (e) {
70             const error = getCommonResourceServiceError(e);
71             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
72                 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
73             }
74             return ;
75         }
76     };