Merge branch 'master' into 13858-process-view-information-card
[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 "~/common/api/common-resource-service";
11 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
12 import { projectPanelActions } from '~/store/project-panel/project-panel-action';
13 import { getProjectList } from '~/store/project/project-action';
14 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
15 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
16 import { findTreeItem } from '~/store/project/project-reducer';
17
18 export const PROJECT_MOVE_FORM_NAME = 'projectMoveFormName';
19
20 export const openMoveProjectDialog = (resource: { name: string, uuid: string }) =>
21     (dispatch: Dispatch) => {
22         dispatch<any>(resetPickerProjectTree());
23         dispatch(initialize(PROJECT_MOVE_FORM_NAME, resource));
24         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_MOVE_FORM_NAME, data: {} }));
25     };
26
27 export const moveProject = (resource: MoveToFormDialogData) =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         dispatch(startSubmit(PROJECT_MOVE_FORM_NAME));
30         try {
31             const project = await services.projectService.get(resource.uuid);
32             await services.projectService.update(resource.uuid, { ...project, ownerUuid: resource.ownerUuid });
33             dispatch(projectPanelActions.REQUEST_ITEMS());
34             dispatch<any>(getProjectList(project.ownerUuid));
35             const { projects } = getState();
36             if (findTreeItem(projects.items, resource.ownerUuid)) {
37                 dispatch<any>(getProjectList(resource.ownerUuid));
38             }
39             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
40             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Project has been moved', hideDuration: 2000 }));
41         } catch (e) {
42             const error = getCommonResourceServiceError(e);
43             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
44                 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'A project with the same name already exists in the target project.' }));
45             } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
46                 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'Cannot move a project into itself.' }));
47             } else {
48                 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
49                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the project.', hideDuration: 2000 }));
50             }
51         }
52     };