refs #14186 Merge branch 'origin/14186-progress-indicator-store'
[arvados-workbench2.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 } 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/project-panel/project-panel-action';
13 import { MoveToFormDialogData } from '~/store/move-to-dialog/move-to-dialog';
14 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-actions';
15 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
16
17 export const COLLECTION_MOVE_FORM_NAME = 'collectionMoveFormName';
18
19 export const openMoveCollectionDialog = (resource: { name: string, uuid: string }) =>
20     (dispatch: Dispatch) => {
21         dispatch<any>(resetPickerProjectTree());
22         dispatch(initialize(COLLECTION_MOVE_FORM_NAME, resource));
23         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MOVE_FORM_NAME, data: {} }));
24     };
25
26 export const moveCollection = (resource: MoveToFormDialogData) =>
27     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28         dispatch(startSubmit(COLLECTION_MOVE_FORM_NAME));
29         try {
30             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_MOVE_FORM_NAME));
31             const collection = await services.collectionService.get(resource.uuid);
32             await services.collectionService.update(resource.uuid, { ...collection, ownerUuid: resource.ownerUuid });
33             dispatch(projectPanelActions.REQUEST_ITEMS());
34             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
35             dispatch(snackbarActions.OPEN_SNACKBAR({
36                 message: 'Collection has been moved',
37                 hideDuration: 2000,
38                 kind: SnackbarKind.SUCCESS
39             }));
40             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
41             return collection;
42         } catch (e) {
43             const error = getCommonResourceServiceError(e);
44             if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
45                 dispatch(stopSubmit(COLLECTION_MOVE_FORM_NAME, { ownerUuid: 'A collection with the same name already exists in the target project.' }));
46             } else {
47                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
48                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the collection.', hideDuration: 2000 }));
49             }
50             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
51             return;
52         }
53     };