Merge branch '17579-Clear-table-filter-when-changing-the-project' into main
[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 import { getResource } from "store/resources/resources";
13 import { CollectionResource } from "models/collection";
14
15 export const COLLECTION_RESTORE_VERSION_DIALOG = 'collectionRestoreVersionDialog';
16
17 export const openRestoreCollectionVersionDialog = (uuid: string) =>
18     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
19         dispatch(dialogActions.OPEN_DIALOG({
20             id: COLLECTION_RESTORE_VERSION_DIALOG,
21             data: {
22                 title: 'Restore version',
23                 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.",
24                 confirmButtonLabel: 'Restore',
25                 uuid
26             }
27         }));
28     };
29
30 export const restoreVersion = (resourceUuid: string) =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         try {
33             // Request the manifest text because stored old versions usually
34             // don't include them.
35             let oldVersion = getResource<CollectionResource>(resourceUuid)(getState().resources);
36             if (!oldVersion) {
37                 oldVersion = await services.collectionService.get(resourceUuid);
38             }
39             const oldVersionManifest = await services.collectionService.get(resourceUuid, undefined, ['manifestText']);
40             oldVersion.manifestText = oldVersionManifest.manifestText;
41
42             const { uuid, version, ...rest} = oldVersion;
43             const headVersion = await services.collectionService.update(
44                 oldVersion.currentVersionUuid,
45                 { ...rest }
46             );
47             dispatch(resourcesActions.SET_RESOURCES([headVersion]));
48             dispatch<any>(navigateTo(headVersion.uuid));
49         } catch (e) {
50             dispatch(snackbarActions.OPEN_SNACKBAR({
51                 message: `Couldn't restore version: ${e.errors[0]}`,
52                 hideDuration: 2000,
53                 kind: SnackbarKind.ERROR
54             }));
55         }
56     };