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, createCollectionFilesTree } from "~/models/collection-file";
8 import { ServiceRepository } from "~/services/services";
9 import { RootState } from "../../store";
10 import { snackbarActions, SnackbarKind } from "../../snackbar/snackbar-actions";
11 import { dialogActions } from '../../dialog/dialog-actions';
12 import { getNodeValue, mapTreeValues } from "~/models/tree";
13 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
14 import { startSubmit, stopSubmit, initialize, FormErrors } from 'redux-form';
15 import { getDialog } from "~/store/dialog/dialog-reducer";
16 import { getFileFullPath, sortFilesTree } from "~/services/collection-service/collection-service-files-response";
17 import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions";
19 export const collectionPanelFilesAction = unionize({
20 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
21 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
22 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
23 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
24 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
27 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
29 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
30 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
32 export const loadCollectionFiles = (uuid: string) =>
33 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
35 const files = await services.collectionService.files(uuid);
37 // Given the array of directories and files, create the appropriate tree nodes,
38 // sort them, and add the complete url to each.
39 const tree = createCollectionFilesTree(files);
40 const sorted = sortFilesTree(tree);
41 const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
42 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
43 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
46 export const removeCollectionFiles = (filePaths: string[]) =>
47 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48 const currentCollection = getState().collectionPanel.item;
49 if (currentCollection) {
50 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
52 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
53 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
54 dispatch(snackbarActions.OPEN_SNACKBAR({
57 kind: SnackbarKind.SUCCESS
60 dispatch(snackbarActions.OPEN_SNACKBAR({
61 message: 'Could not remove file.',
63 kind: SnackbarKind.ERROR
69 export const removeCollectionsSelectedFiles = () =>
70 (dispatch: Dispatch, getState: () => RootState) => {
71 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
72 .map(getFileFullPath);
73 dispatch<any>(removeCollectionFiles(paths));
76 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
78 export const openFileRemoveDialog = (filePath: string) =>
79 (dispatch: Dispatch, getState: () => RootState) => {
80 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
82 const isDirectory = file.type === CollectionFileType.DIRECTORY;
83 const title = isDirectory
84 ? 'Removing directory'
86 const text = isDirectory
87 ? 'Are you sure you want to remove this directory?'
88 : 'Are you sure you want to remove this file?';
89 const info = isDirectory
90 ? 'Removing files will change content address.'
91 : 'Removing a file will change content address.';
93 dispatch(dialogActions.OPEN_DIALOG({
94 id: FILE_REMOVE_DIALOG,
99 confirmButtonLabel: 'Remove',
106 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
108 export const openMultipleFilesRemoveDialog = () =>
109 dialogActions.OPEN_DIALOG({
110 id: MULTIPLE_FILES_REMOVE_DIALOG,
112 title: 'Removing files',
113 text: 'Are you sure you want to remove selected files?',
114 info: 'Removing files will change content address.',
115 confirmButtonLabel: 'Remove'
119 export const RENAME_FILE_DIALOG = 'renameFileDialog';
120 export interface RenameFileDialogData {
125 export const openRenameFileDialog = (data: RenameFileDialogData) =>
126 (dispatch: Dispatch) => {
127 dispatch(initialize(RENAME_FILE_DIALOG, data));
128 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
131 export const renameFile = (newName: string) =>
132 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
133 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
134 const currentCollection = getState().collectionPanel.item;
135 if (dialog && currentCollection) {
136 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
138 dispatch(startSubmit(RENAME_FILE_DIALOG));
139 const oldPath = getFileFullPath(file);
140 const newPath = getFileFullPath({ ...file, name: newName });
142 await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
143 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
144 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
145 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
147 const errors: FormErrors<RenameFileDialogData, string> = {
148 name: 'Could not rename the file'
150 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));