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<{}>(),
25 ON_SEARCH_CHANGE: ofType<string>(),
28 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
30 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
31 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
33 export const loadCollectionFiles = (uuid: string) =>
34 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
36 const files = await services.collectionService.files(uuid);
38 // Given the array of directories and files, create the appropriate tree nodes,
39 // sort them, and add the complete url to each.
40 const tree = createCollectionFilesTree(files);
41 const sorted = sortFilesTree(tree);
42 const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
43 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
44 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
47 export const removeCollectionFiles = (filePaths: string[]) =>
48 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49 const currentCollection = getState().collectionPanel.item;
50 if (currentCollection) {
51 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
53 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
54 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
55 dispatch(snackbarActions.OPEN_SNACKBAR({
58 kind: SnackbarKind.SUCCESS
61 dispatch(snackbarActions.OPEN_SNACKBAR({
62 message: 'Could not remove file.',
64 kind: SnackbarKind.ERROR
70 export const removeCollectionsSelectedFiles = () =>
71 (dispatch: Dispatch, getState: () => RootState) => {
72 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
73 .map(getFileFullPath);
74 dispatch<any>(removeCollectionFiles(paths));
77 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
79 export const openFileRemoveDialog = (filePath: string) =>
80 (dispatch: Dispatch, getState: () => RootState) => {
81 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
83 const isDirectory = file.type === CollectionFileType.DIRECTORY;
84 const title = isDirectory
85 ? 'Removing directory'
87 const text = isDirectory
88 ? 'Are you sure you want to remove this directory?'
89 : 'Are you sure you want to remove this file?';
90 const info = isDirectory
91 ? 'Removing files will change content address.'
92 : 'Removing a file will change content address.';
94 dispatch(dialogActions.OPEN_DIALOG({
95 id: FILE_REMOVE_DIALOG,
100 confirmButtonLabel: 'Remove',
107 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
109 export const openMultipleFilesRemoveDialog = () =>
110 dialogActions.OPEN_DIALOG({
111 id: MULTIPLE_FILES_REMOVE_DIALOG,
113 title: 'Removing files',
114 text: 'Are you sure you want to remove selected files?',
115 info: 'Removing files will change content address.',
116 confirmButtonLabel: 'Remove'
120 export const RENAME_FILE_DIALOG = 'renameFileDialog';
121 export interface RenameFileDialogData {
127 export const openRenameFileDialog = (data: RenameFileDialogData) =>
128 (dispatch: Dispatch) => {
129 dispatch(initialize(RENAME_FILE_DIALOG, data));
130 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
133 export const renameFile = (newFullPath: string) =>
134 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
135 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
136 const currentCollection = getState().collectionPanel.item;
137 if (dialog && currentCollection) {
138 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
140 dispatch(startSubmit(RENAME_FILE_DIALOG));
141 const oldPath = getFileFullPath(file);
142 const newPath = newFullPath;
144 await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
145 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
146 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
147 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
149 const errors: FormErrors<RenameFileDialogData, string> = {
150 path: `Could not rename the file: ${e.responseText}`
152 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));