16d31405565a809e5c9abefa2194c5f9069b0920
[arvados.git] / src / store / collections / collection-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 { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
11 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
12 import { projectPanelActions } from "store/data-explorer/data-explorer-action";
13 // import { projectPanelActions } from 'store/project-panel/project-panel-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 { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
17 import { initProjectsTreePicker } from "store/tree-picker/tree-picker-actions";
18 import { getResource } from "store/resources/resources";
19 import { CollectionResource } from "models/collection";
20
21 export const COLLECTION_MOVE_FORM_NAME = "collectionMoveFormName";
22
23 export const openMoveCollectionDialog = (resource: { name: string; uuid: string }) => (dispatch: Dispatch) => {
24     dispatch<any>(resetPickerProjectTree());
25     dispatch<any>(initProjectsTreePicker(COLLECTION_MOVE_FORM_NAME));
26     dispatch(initialize(COLLECTION_MOVE_FORM_NAME, resource));
27     dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MOVE_FORM_NAME, data: {} }));
28 };
29
30 export const moveCollection =
31     (resource: MoveToFormDialogData) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         dispatch(startSubmit(COLLECTION_MOVE_FORM_NAME));
33         let cachedCollection = getResource<CollectionResource>(resource.uuid)(getState().resources);
34         try {
35             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_MOVE_FORM_NAME));
36             if (!cachedCollection) {
37                 cachedCollection = await services.collectionService.get(resource.uuid);
38             }
39             const collection = await services.collectionService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
40             dispatch(projectPanelActions.REQUEST_ITEMS());
41             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
42             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
43             return { ...cachedCollection, ...collection };
44         } catch (e) {
45             const error = getCommonResourceServiceError(e);
46             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
47                 dispatch(
48                     stopSubmit(COLLECTION_MOVE_FORM_NAME, {
49                         ownerUuid: "A collection with the same name already exists in the target project.",
50                     } as FormErrors)
51                 );
52             } else {
53                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
54                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Could not move the collection.", hideDuration: 2000, kind: SnackbarKind.ERROR }));
55             }
56             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
57             return;
58         }
59     };