1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { unionize, ofType, UnionOf } from "~/common/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 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
14 import { startSubmit, stopSubmit, reset } from 'redux-form';
15 import { getDialog } from "~/store/dialog/dialog-reducer";
17 export const collectionPanelFilesAction = unionize({
18 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
19 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
20 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
21 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
22 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
25 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
27 export const loadCollectionFiles = (uuid: string) =>
28 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29 const files = await services.collectionService.files(uuid);
30 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
33 export const removeCollectionFiles = (filePaths: string[]) =>
34 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35 const currentCollection = getState().collectionPanel.item;
36 if (currentCollection) {
37 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
38 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
39 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
40 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
44 export const removeCollectionsSelectedFiles = () =>
45 (dispatch: Dispatch, getState: () => RootState) => {
46 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).map(file => file.id);
47 dispatch<any>(removeCollectionFiles(paths));
50 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
52 export const openFileRemoveDialog = (filePath: string) =>
53 (dispatch: Dispatch, getState: () => RootState) => {
54 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
56 const title = file.type === CollectionFileType.DIRECTORY
57 ? 'Removing directory'
59 const text = file.type === CollectionFileType.DIRECTORY
60 ? 'Are you sure you want to remove this directory?'
61 : 'Are you sure you want to remove this file?';
63 dispatch(dialogActions.OPEN_DIALOG({
64 id: FILE_REMOVE_DIALOG,
68 confirmButtonLabel: 'Remove',
75 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
77 export const openMultipleFilesRemoveDialog = () =>
78 dialogActions.OPEN_DIALOG({
79 id: MULTIPLE_FILES_REMOVE_DIALOG,
81 title: 'Removing files',
82 text: 'Are you sure you want to remove selected files?',
83 confirmButtonLabel: 'Remove'
87 export const RENAME_FILE_DIALOG = 'renameFileDialog';
88 export interface RenameFileDialogData {
93 export const openRenameFileDialog = (data: RenameFileDialogData) =>
94 (dispatch: Dispatch) => {
95 dispatch(reset(RENAME_FILE_DIALOG));
96 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
99 export const renameFile = (newName: string) =>
100 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
101 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
102 const currentCollection = getState().collectionPanel.item;
103 if (dialog && currentCollection) {
104 dispatch(startSubmit(RENAME_FILE_DIALOG));
105 const oldPath = dialog.data.id;
106 const newPath = dialog.data.id.replace(dialog.data.name, newName);
108 await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
109 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
110 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
111 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
113 dispatch(stopSubmit(RENAME_FILE_DIALOG, { name: 'Could not rename the file' }));