21846: applied select to context menu open
[arvados.git] / services / workbench2 / 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, formValueSelector, initialize, reset, 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 { projectPanelActions } from "store/project-panel/project-panel-action-bind";
12 import { GroupClass } from "models/group";
13 import { Participant } from "views-components/sharing-dialog/participant-select";
14 import { ProjectProperties } from "./project-create-actions";
15 import { getResource } from "store/resources/resources";
16 import { ProjectResource } from "models/project";
17 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
18
19 export interface ProjectUpdateFormDialogData {
20     uuid: string;
21     name: string;
22     users?: Participant[];
23     description?: string;
24     properties?: ProjectProperties;
25 }
26
27 export const PROJECT_UPDATE_FORM_NAME = "projectUpdateFormName";
28 export const PROJECT_UPDATE_PROPERTIES_FORM_NAME = "projectUpdatePropertiesFormName";
29 export const PROJECT_UPDATE_FORM_SELECTOR = formValueSelector(PROJECT_UPDATE_FORM_NAME);
30
31 export const openProjectUpdateDialog = (resource: ProjectUpdateFormDialogData) => (dispatch: Dispatch, getState: () => RootState) => {
32     // Get complete project resource from store to handle consumers passing in partial resources
33     const project = getResource<ProjectResource>(resource.uuid)(getState().resources);
34     dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
35     dispatch(
36         dialogActions.OPEN_DIALOG({
37             id: PROJECT_UPDATE_FORM_NAME,
38             data: {
39                 sourcePanel: GroupClass.PROJECT,
40             },
41         })
42     );
43 };
44
45 export const updateProject =
46     (project: ProjectUpdateFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47         const uuid = project.uuid || "";
48         dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
49         try {
50             const updatedProject = await services.projectService.update(
51                 uuid,
52                 {
53                     name: project.name,
54                     description: project.description,
55                     properties: project.properties,
56                 },
57                 false
58             );
59             dispatch(projectPanelActions.REQUEST_ITEMS());
60             dispatch(reset(PROJECT_UPDATE_FORM_NAME));
61             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
62             return updatedProject;
63         } catch (e) {
64             const error = getCommonResourceServiceError(e);
65             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
66                 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: "Project with the same name already exists." } as FormErrors));
67             } else {
68                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
69                 const errMsg = e.errors ? e.errors.join("") : "There was an error while updating the project";
70                 dispatch(
71                     snackbarActions.OPEN_SNACKBAR({
72                         message: errMsg,
73                         hideDuration: 2000,
74                         kind: SnackbarKind.ERROR,
75                     })
76                 );
77             }
78             return;
79         }
80     };