1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
18 export const PROJECT_MOVE_FORM_NAME = "projectMoveFormName";
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: {} }));
29 export const moveProject = (resource: MoveToFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30 const userUuid = getUserUuid(getState());
34 dispatch(startSubmit(PROJECT_MOVE_FORM_NAME));
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));
42 const error = getCommonResourceServiceError(e);
43 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
45 stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "A project with the same name already exists in the target project." } as FormErrors)
47 } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
48 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: "Cannot move a project into itself." } as FormErrors));
50 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
51 throw new Error("Could not move the project.");