Implement single file remove action
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-files / collection-panel-files-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { default as unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { CollectionFilesTree, CollectionFileType } from "../../../models/collection-file";
8 import { ServiceRepository } from "../../../services/services";
9 import { RootState } from "../../store";
10 import { snackbarActions } from "../../snackbar/snackbar-actions";
11 import { dialogActions } from "../../dialog/dialog-actions";
12 import { getNodeValue } from "../../../models/tree";
13
14 export const collectionPanelFilesAction = unionize({
15     SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
16     TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
17     TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
18     SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
19     UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
20 }, { tag: 'type', value: 'payload' });
21
22 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
23
24 export const loadCollectionFiles = (uuid: string) =>
25     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26         const files = await services.collectionService.files(uuid);
27         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
28     };
29
30 export const removeCollectionFiles = (filePaths: string[]) =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         const { item } = getState().collectionPanel;
33         if (item) {
34             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
35             const promises = filePaths.map(filePath => services.collectionService.deleteFile(item.uuid, filePath));
36             await Promise.all(promises);
37             dispatch<any>(loadCollectionFiles(item.uuid));
38             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
39         }
40     };
41
42 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
43 export const openFileRemoveDialog = (filePath: string) =>
44     (dispatch: Dispatch, getState: () => RootState) => {
45         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
46         if (file) {
47             const title = file.type === CollectionFileType.DIRECTORY
48                 ? 'Removing directory'
49                 : 'Removing file';
50             const text = file.type === CollectionFileType.DIRECTORY
51                 ? 'Are you sure you want to remove this directory?'
52                 : 'Are you sure you want to remove this file?';
53
54             dispatch(dialogActions.OPEN_DIALOG({
55                 id: FILE_REMOVE_DIALOG,
56                 data: {
57                     title,
58                     text,
59                     confirmButtonLabel: 'Remove',
60                     filePath
61                 }
62             }));
63         }
64     };