merge master
[arvados-workbench2.git] / src / store / projects / project-move-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 { dialogActions } from "~/store/dialog/dialog-actions";
7 import { startSubmit, stopSubmit, initialize } from 'redux-form';
8 import { ServiceRepository } from '~/services/services';
9 import { RootState } from '~/store/store';
10 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/services/common-service/common-resource-service";
11 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
12 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
13
14 export const PROJECT_MOVE_FORM_NAME = 'projectMoveFormName';
15
16 export const openMoveProjectDialog = (resource: { name: string, uuid: string }) =>
17     (dispatch: Dispatch) => {
18         dispatch<any>(resetPickerProjectTree());
19         dispatch(initialize(PROJECT_MOVE_FORM_NAME, resource));
20         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_MOVE_FORM_NAME, data: {} }));
21     };
22
23 export const moveProject = (resource: MoveToFormDialogData) =>
24     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25         dispatch(startSubmit(PROJECT_MOVE_FORM_NAME));
26         try {
27             const project = await services.projectService.get(resource.uuid);
28             const newProject = await services.projectService.update(resource.uuid, { ...project, ownerUuid: resource.ownerUuid });
29             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
30             return newProject;
31         } catch (e) {
32             const error = getCommonResourceServiceError(e);
33             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
34                 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'A project with the same name already exists in the target project.' }));
35             } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
36                 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'Cannot move a project into itself.' }));
37             } else {
38                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
39                 throw new Error('Could not move the project.');
40             }
41             return;
42         }
43     };