19642: Updating queued chip to be outline not filled
[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 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
28
29 export interface ProjectUpdateFormDialogData {
30     uuid: string;
31     name: string;
32     users?: Participant[];
33     description?: string;
34     properties?: ProjectProperties;
35 }
36
37 export const PROJECT_UPDATE_FORM_NAME = 'projectUpdateFormName';
38 export const PROJECT_UPDATE_PROPERTIES_FORM_NAME = 'projectUpdatePropertiesFormName';
39 export const PROJECT_UPDATE_FORM_SELECTOR = formValueSelector(PROJECT_UPDATE_FORM_NAME);
40
41 export const openProjectUpdateDialog = (resource: ProjectUpdateFormDialogData) =>
42     (dispatch: Dispatch, getState: () => RootState) => {
43         // Get complete project resource from store to handle consumers passing in partial resources
44         const project = getResource<ProjectResource>(resource.uuid)(getState().resources);
45         dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
46         dispatch(dialogActions.OPEN_DIALOG({
47             id: PROJECT_UPDATE_FORM_NAME,
48             data: {
49                 sourcePanel: GroupClass.PROJECT,
50             }
51         }));
52     };
53
54 export const updateProject = (project: ProjectUpdateFormDialogData) =>
55     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         const uuid = project.uuid || '';
57         dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
58         try {
59             const updatedProject = await services.projectService.update(
60                 uuid,
61                 {
62                     name: project.name,
63                     description: project.description,
64                     properties: project.properties,
65                 },
66                 false);
67             dispatch(projectPanelActions.REQUEST_ITEMS());
68             dispatch(reset(PROJECT_UPDATE_FORM_NAME));
69             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
70             return updatedProject;
71         } catch (e) {
72             const error = getCommonResourceServiceError(e);
73             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
74                 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));
75             } else {
76                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
77                 const errMsg = e.errors
78                     ? e.errors.join('')
79                     : 'There was an error while updating the project';
80                 dispatch(snackbarActions.OPEN_SNACKBAR({
81                     message: errMsg,
82                     hideDuration: 2000,
83                     kind: SnackbarKind.ERROR }));
84             }
85             return;
86         }
87     };