15768: created action set for ms actions Arvados-DCO-1.1-Signed-off-by: Lisa Knox...
[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, 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 { projectPanelActions } from "store/project-panel/project-panel-action";
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(projectPanelActions.REQUEST_ITEMS());
38         dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
39         await dispatch<any>(loadSidePanelTreeProjects(userUuid));
40         return newProject;
41     } catch (e) {
42         const error = getCommonResourceServiceError(e);
43         if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
44             dispatch(
45                 stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "A project with the same name already exists in the target project." } as FormErrors)
46             );
47         } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
48             dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "Cannot move a project into itself." } as FormErrors));
49         } else {
50             dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
51             throw new Error("Could not move the project.");
52         }
53         return;
54     }
55 };