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 servicesProvider from 'common/service-provider';
8 import { CollectionFilesTree, CollectionFileType, createCollectionFilesTree } from "models/collection-file";
9 import { ServiceRepository } from "services/services";
10 import { RootState } from "../../store";
11 import { snackbarActions, SnackbarKind } from "../../snackbar/snackbar-actions";
12 import { dialogActions } from '../../dialog/dialog-actions';
13 import { getNodeValue, mapTreeValues } from "models/tree";
14 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
15 import { startSubmit, stopSubmit, initialize, FormErrors } from 'redux-form';
16 import { getDialog } from "store/dialog/dialog-reducer";
17 import { getFileFullPath, sortFilesTree } from "services/collection-service/collection-service-files-response";
18 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
19 import { loadCollectionPanel } from "../collection-panel-action";
21 export const collectionPanelFilesAction = unionize({
22 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
23 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
24 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
25 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
26 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
27 ON_SEARCH_CHANGE: ofType<string>(),
30 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
32 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
33 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
35 export const setCollectionFiles = (files, joinParents = true) => (dispatch: any) => {
36 const tree = createCollectionFilesTree(files, joinParents);
37 const sorted = sortFilesTree(tree);
38 const mapped = mapTreeValues(servicesProvider.getServices().collectionService.extendFileURL)(sorted);
39 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
42 export const loadCollectionFiles = (uuid: string) =>
43 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
45 services.collectionService.files(uuid).then(files => {
46 // Given the array of directories and files, create the appropriate tree nodes,
47 // sort them, and add the complete url to each.
48 const tree = createCollectionFilesTree(files);
49 const sorted = sortFilesTree(tree);
50 const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
51 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
52 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
54 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
55 dispatch(snackbarActions.OPEN_SNACKBAR({
56 message: `Error getting file list`,
58 kind: SnackbarKind.ERROR
63 export const removeCollectionFiles = (filePaths: string[]) =>
64 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
65 const currentCollection = getState().collectionPanel.item;
66 if (currentCollection) {
67 services.collectionService.deleteFiles(currentCollection.uuid, filePaths).then(() => {
68 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
69 dispatch(snackbarActions.OPEN_SNACKBAR({
72 kind: SnackbarKind.SUCCESS
75 dispatch(snackbarActions.OPEN_SNACKBAR({
76 message: 'Could not remove file.',
78 kind: SnackbarKind.ERROR
84 export const removeCollectionsSelectedFiles = () =>
85 (dispatch: Dispatch, getState: () => RootState) => {
86 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
87 .map(getFileFullPath);
88 dispatch<any>(removeCollectionFiles(paths));
91 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
93 export const openFileRemoveDialog = (filePath: string) =>
94 (dispatch: Dispatch, getState: () => RootState) => {
95 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
97 const isDirectory = file.type === CollectionFileType.DIRECTORY;
98 const title = isDirectory
99 ? 'Removing directory'
101 const text = isDirectory
102 ? 'Are you sure you want to remove this directory?'
103 : 'Are you sure you want to remove this file?';
104 const info = isDirectory
105 ? 'Removing files will change content address.'
106 : 'Removing a file will change content address.';
108 dispatch(dialogActions.OPEN_DIALOG({
109 id: FILE_REMOVE_DIALOG,
114 confirmButtonLabel: 'Remove',
121 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
123 export const openMultipleFilesRemoveDialog = () =>
124 dialogActions.OPEN_DIALOG({
125 id: MULTIPLE_FILES_REMOVE_DIALOG,
127 title: 'Removing files',
128 text: 'Are you sure you want to remove selected files?',
129 info: 'Removing files will change content address.',
130 confirmButtonLabel: 'Remove'
134 export const RENAME_FILE_DIALOG = 'renameFileDialog';
135 export interface RenameFileDialogData {
141 export const openRenameFileDialog = (data: RenameFileDialogData) =>
142 (dispatch: Dispatch) => {
143 dispatch(initialize(RENAME_FILE_DIALOG, data));
144 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
147 export const renameFile = (newFullPath: string) =>
148 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
149 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
150 const currentCollection = getState().collectionPanel.item;
151 if (dialog && currentCollection) {
152 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
154 dispatch(startSubmit(RENAME_FILE_DIALOG));
155 const oldPath = getFileFullPath(file);
156 const newPath = newFullPath;
157 services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath).then(() => {
158 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
159 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
160 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
162 const errors: FormErrors<RenameFileDialogData, string> = {
163 path: `Could not rename the file: ${e.responseText}`
165 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));