1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { FormErrors, 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 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
16 export interface ProjectUpdateFormDialogData {
22 export const PROJECT_UPDATE_FORM_NAME = 'projectUpdateFormName';
24 export const openProjectUpdateDialog = (resource: ContextMenuResource) =>
25 (dispatch: Dispatch, getState: () => RootState) => {
26 const project = getResource(resource.uuid)(getState().resources);
27 dispatch(initialize(PROJECT_UPDATE_FORM_NAME, project));
28 dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_UPDATE_FORM_NAME, data: {} }));
31 export const updateProject = (project: Partial<ProjectResource>) =>
32 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33 const uuid = project.uuid || '';
34 dispatch(startSubmit(PROJECT_UPDATE_FORM_NAME));
36 const updatedProject = await services.projectService.update(uuid, project);
37 dispatch(projectPanelActions.REQUEST_ITEMS());
38 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_UPDATE_FORM_NAME }));
39 return updatedProject;
41 const error = getCommonResourceServiceError(e);
42 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
43 dispatch(stopSubmit(PROJECT_UPDATE_FORM_NAME, { name: 'Project with the same name already exists.' } as FormErrors));