1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
15 export const COLLECTION_RESTORE_VERSION_DIALOG = 'collectionRestoreVersionDialog';
17 export const openRestoreCollectionVersionDialog = (uuid: string) =>
18 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
19 dispatch(dialogActions.OPEN_DIALOG({
20 id: COLLECTION_RESTORE_VERSION_DIALOG,
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',
30 export const restoreVersion = (resourceUuid: string) =>
31 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33 // Request the manifest text because stored old versions usually
34 // don't include them.
35 let oldVersion = getResource<CollectionResource>(resourceUuid)(getState().resources);
37 oldVersion = await services.collectionService.get(resourceUuid);
39 const oldVersionManifest = await services.collectionService.get(resourceUuid, undefined, ['manifestText']);
40 oldVersion.manifestText = oldVersionManifest.manifestText;
42 const { uuid, version, ...rest} = oldVersion;
43 const headVersion = await services.collectionService.update(
44 oldVersion.currentVersionUuid,
47 dispatch(resourcesActions.SET_RESOURCES([headVersion]));
48 dispatch<any>(navigateTo(headVersion.uuid));
50 dispatch(snackbarActions.OPEN_SNACKBAR({
51 message: `Couldn't restore version: ${e.errors[0]}`,
53 kind: SnackbarKind.ERROR