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: { name: string, uuid: string }) =>
21 (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: {} }));
28 export const moveProject = (resource: MoveToFormDialogData) =>
29 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30 const userUuid = getUserUuid(getState());
31 if (!userUuid) { return; }
32 dispatch(startSubmit(PROJECT_MOVE_FORM_NAME));
34 const newProject = await services.projectService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
35 dispatch(projectPanelActions.REQUEST_ITEMS());
36 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
37 await dispatch<any>(loadSidePanelTreeProjects(userUuid));
40 const error = getCommonResourceServiceError(e);
41 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
42 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'A project with the same name already exists in the target project.' } as FormErrors));
43 } else if (error === CommonResourceServiceError.OWNERSHIP_CYCLE) {
44 dispatch(stopSubmit(PROJECT_MOVE_FORM_NAME, { ownerUuid: 'Cannot move a project into itself.' } as FormErrors));
46 dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_MOVE_FORM_NAME }));
47 throw new Error('Could not move the project.');