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";
18 import { loadCollectionPanel } from "../collection-panel-action";
20 export const collectionPanelFilesAction = unionize({
21 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
22 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
23 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
24 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
25 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
26 ON_SEARCH_CHANGE: ofType<string>(),
29 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
31 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
32 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
34 export const loadCollectionFiles = (uuid: string) =>
35 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
36 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
37 services.collectionService.files(uuid).then(files => {
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));
46 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
47 dispatch(snackbarActions.OPEN_SNACKBAR({
48 message: `Error getting file list`,
50 kind: SnackbarKind.ERROR
55 export const removeCollectionFiles = (filePaths: string[]) =>
56 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
57 const currentCollection = getState().collectionPanel.item;
58 if (currentCollection) {
59 services.collectionService.deleteFiles(currentCollection.uuid, filePaths).then(() => {
60 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
61 dispatch(snackbarActions.OPEN_SNACKBAR({
64 kind: SnackbarKind.SUCCESS
67 dispatch(snackbarActions.OPEN_SNACKBAR({
68 message: 'Could not remove file.',
70 kind: SnackbarKind.ERROR
76 export const removeCollectionsSelectedFiles = () =>
77 (dispatch: Dispatch, getState: () => RootState) => {
78 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
79 .map(getFileFullPath);
80 dispatch<any>(removeCollectionFiles(paths));
83 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
85 export const openFileRemoveDialog = (filePath: string) =>
86 (dispatch: Dispatch, getState: () => RootState) => {
87 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
89 const isDirectory = file.type === CollectionFileType.DIRECTORY;
90 const title = isDirectory
91 ? 'Removing directory'
93 const text = isDirectory
94 ? 'Are you sure you want to remove this directory?'
95 : 'Are you sure you want to remove this file?';
96 const info = isDirectory
97 ? 'Removing files will change content address.'
98 : 'Removing a file will change content address.';
100 dispatch(dialogActions.OPEN_DIALOG({
101 id: FILE_REMOVE_DIALOG,
106 confirmButtonLabel: 'Remove',
113 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
115 export const openMultipleFilesRemoveDialog = () =>
116 dialogActions.OPEN_DIALOG({
117 id: MULTIPLE_FILES_REMOVE_DIALOG,
119 title: 'Removing files',
120 text: 'Are you sure you want to remove selected files?',
121 info: 'Removing files will change content address.',
122 confirmButtonLabel: 'Remove'
126 export const RENAME_FILE_DIALOG = 'renameFileDialog';
127 export interface RenameFileDialogData {
133 export const openRenameFileDialog = (data: RenameFileDialogData) =>
134 (dispatch: Dispatch) => {
135 dispatch(initialize(RENAME_FILE_DIALOG, data));
136 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
139 export const renameFile = (newFullPath: string) =>
140 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
141 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
142 const currentCollection = getState().collectionPanel.item;
143 if (dialog && currentCollection) {
144 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
146 dispatch(startSubmit(RENAME_FILE_DIALOG));
147 const oldPath = getFileFullPath(file);
148 const newPath = newFullPath;
149 services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath).then(() => {
150 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
151 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
152 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
154 const errors: FormErrors<RenameFileDialogData, string> = {
155 path: `Could not rename the file: ${e.responseText}`
157 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));