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