17782: Merge branch 'main' into 17782-react-scripts-ts-migration
[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, 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/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 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
17
18 export const COLLECTION_MOVE_FORM_NAME = 'collectionMoveFormName';
19
20 export const openMoveCollectionDialog = (resource: { name: string, uuid: string }) =>
21     (dispatch: Dispatch) => {
22         dispatch<any>(resetPickerProjectTree());
23         dispatch<any>(initProjectsTreePicker(COLLECTION_MOVE_FORM_NAME));
24         dispatch(initialize(COLLECTION_MOVE_FORM_NAME, resource));
25         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_MOVE_FORM_NAME, data: {} }));
26     };
27
28 export const moveCollection = (resource: MoveToFormDialogData) =>
29     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30         dispatch(startSubmit(COLLECTION_MOVE_FORM_NAME));
31         try {
32             dispatch(progressIndicatorActions.START_WORKING(COLLECTION_MOVE_FORM_NAME));
33             const collection = await services.collectionService.update(resource.uuid, { ownerUuid: resource.ownerUuid });
34             dispatch(projectPanelActions.REQUEST_ITEMS());
35             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
36             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
37             return collection;
38         } catch (e) {
39             const error = getCommonResourceServiceError(e);
40             if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
41                 dispatch(stopSubmit(COLLECTION_MOVE_FORM_NAME, { ownerUuid: 'A collection with the same name already exists in the target project.' } as FormErrors));
42             } else {
43                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_MOVE_FORM_NAME }));
44                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not move the collection.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
45             }
46             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_MOVE_FORM_NAME));
47             return;
48         }
49     };