Merge branch '21225-project-panel-tabs' into main. Closes #21225
[arvados.git] / services / workbench2 / 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, FormErrors } from "redux-form";
8 import { ServiceRepository } from "services/services";
9 import { RootState } from "store/store";
10 import { getUserUuid } from "common/getuser";
11 import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
12 import { MoveToFormDialogData } from "store/move-to-dialog/move-to-dialog";
13 import { resetPickerProjectTree } from "store/project-tree-picker/project-tree-picker-actions";
14 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
15 import { projectPanelDataActions } from "store/project-panel/project-panel-action-bind";
16 import { loadSidePanelTreeProjects } from "../side-panel-tree/side-panel-tree-actions";
17
18 export const PROJECT_MOVE_FORM_NAME = "projectMoveFormName";
19
20 export const openMoveProjectDialog = (resource: any) => {
21     return (dispatch: Dispatch) => {
22         dispatch<any>(resetPickerProjectTree());
23         dispatch<any>(initProjectsTreePicker(PROJECT_MOVE_FORM_NAME));
24         dispatch(initialize(PROJECT_MOVE_FORM_NAME, resource));
25         dispatch(dialogActions.OPEN_DIALOG({ id: PROJECT_MOVE_FORM_NAME, data: {} }));
26     };
27 };
28
29 export const moveProject = (resource: MoveToFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30     const userUuid = getUserUuid(getState());
31     if (!userUuid) {
32         return;
33     }
34     dispatch(startSubmit(PROJECT_MOVE_FORM_NAME));
35     try {
36         const newProject = await services.projectService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
37         dispatch(projectPanelDataActions.REQUEST_ITEMS());
38
39         dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
40         await dispatch<any>(loadSidePanelTreeProjects(userUuid));
41         return newProject;
42     } catch (e) {
43         const error = getCommonResourceServiceError(e);
44         if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
45             dispatch(
46                 stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "A project with the same name already exists in the target project." } as FormErrors)
47             );
48         } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
49             dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "Cannot move a project into itself." } as FormErrors));
50         } else {
51             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
52             throw new Error("Could not move the project.");
53         }
54         return;
55     }
56 };