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 }));
54 export const removeCollectionFiles = (filePaths: string[]) =>
55 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56 const currentCollection = getState().collectionPanel.item;
57 if (currentCollection) {
58 services.collectionService.deleteFiles(currentCollection.uuid, filePaths).then(() => {
59 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
60 dispatch(snackbarActions.OPEN_SNACKBAR({
63 kind: SnackbarKind.SUCCESS
66 dispatch(snackbarActions.OPEN_SNACKBAR({
67 message: 'Could not remove file.',
69 kind: SnackbarKind.ERROR
75 export const removeCollectionsSelectedFiles = () =>
76 (dispatch: Dispatch, getState: () => RootState) => {
77 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
78 .map(getFileFullPath);
79 dispatch<any>(removeCollectionFiles(paths));
82 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
84 export const openFileRemoveDialog = (filePath: string) =>
85 (dispatch: Dispatch, getState: () => RootState) => {
86 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
88 const isDirectory = file.type === CollectionFileType.DIRECTORY;
89 const title = isDirectory
90 ? 'Removing directory'
92 const text = isDirectory
93 ? 'Are you sure you want to remove this directory?'
94 : 'Are you sure you want to remove this file?';
95 const info = isDirectory
96 ? 'Removing files will change content address.'
97 : 'Removing a file will change content address.';
99 dispatch(dialogActions.OPEN_DIALOG({
100 id: FILE_REMOVE_DIALOG,
105 confirmButtonLabel: 'Remove',
112 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
114 export const openMultipleFilesRemoveDialog = () =>
115 dialogActions.OPEN_DIALOG({
116 id: MULTIPLE_FILES_REMOVE_DIALOG,
118 title: 'Removing files',
119 text: 'Are you sure you want to remove selected files?',
120 info: 'Removing files will change content address.',
121 confirmButtonLabel: 'Remove'
125 export const RENAME_FILE_DIALOG = 'renameFileDialog';
126 export interface RenameFileDialogData {
132 export const openRenameFileDialog = (data: RenameFileDialogData) =>
133 (dispatch: Dispatch) => {
134 dispatch(initialize(RENAME_FILE_DIALOG, data));
135 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
138 export const renameFile = (newFullPath: string) =>
139 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
140 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
141 const currentCollection = getState().collectionPanel.item;
142 if (dialog && currentCollection) {
143 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
145 dispatch(startSubmit(RENAME_FILE_DIALOG));
146 const oldPath = getFileFullPath(file);
147 const newPath = newFullPath;
148 services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath).then(() => {
149 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
150 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
151 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
153 const errors: FormErrors<RenameFileDialogData, string> = {
154 path: `Could not rename the file: ${e.responseText}`
156 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));