c0a58432d7f6be7c4e5412ec5f5f72658d190c13
[arvados-workbench2.git] / src / store / collections / collection-version-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 { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
9 import { resourcesActions } from "../resources/resources-actions";
10 import { navigateTo } from "../navigation/navigation-action";
11 import { dialogActions } from "../dialog/dialog-actions";
12
13 export const COLLECTION_RESTORE_VERSION_DIALOG = 'collectionRestoreVersionDialog';
14
15 export const openRestoreCollectionVersionDialog = (uuid: string) =>
16     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
17         dispatch(dialogActions.OPEN_DIALOG({
18             id: COLLECTION_RESTORE_VERSION_DIALOG,
19             data: {
20                 title: 'Restore version',
21                 text: "This will copy the content of the selected version to the head. To make a new collection with the content of the selected version, use 'Make a copy' instead.",
22                 confirmButtonLabel: 'Restore',
23                 uuid
24             }
25         }));
26     };
27
28 export const restoreVersion = (resourceUuid: string) =>
29     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30         try {
31             // Request que entire record because stored old versions usually
32             // don't include the manifest_text field.
33             const oldVersion = await services.collectionService.get(resourceUuid);
34             const { uuid, version, ...rest} = oldVersion;
35             const headVersion = await services.collectionService.update(
36                 oldVersion.currentVersionUuid,
37                 { ...rest }
38             );
39             dispatch(resourcesActions.SET_RESOURCES([headVersion]));
40             dispatch<any>(navigateTo(headVersion.uuid));
41         } catch (e) {
42             dispatch(snackbarActions.OPEN_SNACKBAR({
43                 message: `Couldn't restore version: ${e.errors[0]}`,
44                 hideDuration: 2000,
45                 kind: SnackbarKind.ERROR
46             }));
47         }
48     };