1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
19 export interface ProjectUpdateFormDialogData {
22 users?: Participant[];
24 properties?: ProjectProperties;
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);
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));
36 dialogActions.OPEN_DIALOG({
37 id: PROJECT_UPDATE_FORM_NAME,
39 sourcePanel: GroupClass.PROJECT,
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));
50 const updatedProject = await services.projectService.update(
54 description: project.description,
55 properties: project.properties,
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;
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));
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";
71 snackbarActions.OPEN_SNACKBAR({
74 kind: SnackbarKind.ERROR,