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