1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
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' });
22 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
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));
30 export const removeCollectionFiles = (filePaths: string[]) =>
31 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32 const { item } = getState().collectionPanel;
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 }));
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);
47 const title = file.type === CollectionFileType.DIRECTORY
48 ? 'Removing directory'
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?';
54 dispatch(dialogActions.OPEN_DIALOG({
55 id: FILE_REMOVE_DIALOG,
59 confirmButtonLabel: 'Remove',