Merge branch 'master'
[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, getNodeDescendants } from "~/models/tree";
13 import { CollectionPanelDirectory, CollectionPanelFile } from "./collection-panel-files-state";
14
15 export const collectionPanelFilesAction = unionize({
16     SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
17     TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
18     TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
19     SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
20     UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
21 }, { tag: 'type', value: 'payload' });
22
23 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
24
25 export const loadCollectionFiles = (uuid: string) =>
26     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
27         const files = await services.collectionService.files(uuid);
28         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
29     };
30
31 export const removeCollectionFiles = (filePaths: string[]) =>
32     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33         const { item } = getState().collectionPanel;
34         if (item) {
35             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
36             const promises = filePaths.map(filePath => services.collectionService.deleteFile(item.uuid, filePath));
37             await Promise.all(promises);
38             dispatch<any>(loadCollectionFiles(item.uuid));
39             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
40         }
41     };
42
43 export const removeCollectionsSelectedFiles = () =>
44     (dispatch: Dispatch, getState: () => RootState) => {
45         const tree = getState().collectionPanelFiles;
46         const allFiles = getNodeDescendants('')(tree)
47             .map(id => getNodeValue(id)(tree))
48             .filter(file => file !== undefined) as Array<CollectionPanelDirectory | CollectionPanelFile>;
49
50         const selectedDirectories = allFiles.filter(file => file.selected && file.type === CollectionFileType.DIRECTORY);
51         const selectedFiles = allFiles.filter(file => file.selected && !selectedDirectories.some(dir => dir.id === file.path));
52         const paths = [...selectedDirectories, ...selectedFiles].map(file => file.id);
53         dispatch<any>(removeCollectionFiles(paths));
54     };
55
56 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
57 export const openFileRemoveDialog = (filePath: string) =>
58     (dispatch: Dispatch, getState: () => RootState) => {
59         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
60         if (file) {
61             const title = file.type === CollectionFileType.DIRECTORY
62                 ? 'Removing directory'
63                 : 'Removing file';
64             const text = file.type === CollectionFileType.DIRECTORY
65                 ? 'Are you sure you want to remove this directory?'
66                 : 'Are you sure you want to remove this file?';
67
68             dispatch(dialogActions.OPEN_DIALOG({
69                 id: FILE_REMOVE_DIALOG,
70                 data: {
71                     title,
72                     text,
73                     confirmButtonLabel: 'Remove',
74                     filePath
75                 }
76             }));
77         }
78     };
79
80 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
81 export const openMultipleFilesRemoveDialog = () =>
82     dialogActions.OPEN_DIALOG({
83         id: MULTIPLE_FILES_REMOVE_DIALOG,
84         data: {
85             title: 'Removing files',
86             text: 'Are you sure you want to remove selected files?',
87             confirmButtonLabel: 'Remove'
88         }
89     });