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';
34 export const setCollectionFiles = (files, joinParents = true) => (dispatch: any) => {
35 const tree = createCollectionFilesTree(files, joinParents);
36 const sorted = sortFilesTree(tree);
37 const mapped = mapTreeValues(servicesProvider.getServices().collectionService.extendFileURL)(sorted);
38 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
41 export const loadCollectionFiles = (uuid: string) =>
42 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
44 services.collectionService.files(uuid).then(files => {
45 // Given the array of directories and files, create the appropriate tree nodes,
46 // sort them, and add the complete url to each.
47 const tree = createCollectionFilesTree(files);
48 const sorted = sortFilesTree(tree);
49 const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
50 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
51 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
53 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
54 dispatch(snackbarActions.OPEN_SNACKBAR({
55 message: `Error getting file list`,
57 kind: SnackbarKind.ERROR
62 export const removeCollectionFiles = (filePaths: string[]) =>
63 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64 const currentCollection = getState().collectionPanel.item;
65 if (currentCollection) {
66 services.collectionService.deleteFiles(currentCollection.uuid, filePaths).then(() => {
67 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
68 dispatch(snackbarActions.OPEN_SNACKBAR({
71 kind: SnackbarKind.SUCCESS
74 dispatch(snackbarActions.OPEN_SNACKBAR({
75 message: 'Could not remove file.',
77 kind: SnackbarKind.ERROR
83 export const removeCollectionsSelectedFiles = () =>
84 (dispatch: Dispatch, getState: () => RootState) => {
85 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
86 .map(getFileFullPath);
87 dispatch<any>(removeCollectionFiles(paths));
90 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
92 export const openFileRemoveDialog = (filePath: string) =>
93 (dispatch: Dispatch, getState: () => RootState) => {
94 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
96 const isDirectory = file.type === CollectionFileType.DIRECTORY;
97 const title = isDirectory
98 ? 'Removing directory'
100 const text = isDirectory
101 ? 'Are you sure you want to remove this directory?'
102 : 'Are you sure you want to remove this file?';
103 const info = isDirectory
104 ? 'Removing files will change content address.'
105 : 'Removing a file will change content address.';
107 dispatch(dialogActions.OPEN_DIALOG({
108 id: FILE_REMOVE_DIALOG,
113 confirmButtonLabel: 'Remove',
120 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
122 export const openMultipleFilesRemoveDialog = () =>
123 dialogActions.OPEN_DIALOG({
124 id: MULTIPLE_FILES_REMOVE_DIALOG,
126 title: 'Removing files',
127 text: 'Are you sure you want to remove selected files?',
128 info: 'Removing files will change content address.',
129 confirmButtonLabel: 'Remove'
133 export const RENAME_FILE_DIALOG = 'renameFileDialog';
134 export interface RenameFileDialogData {
140 export const openRenameFileDialog = (data: RenameFileDialogData) =>
141 (dispatch: Dispatch) => {
142 dispatch(initialize(RENAME_FILE_DIALOG, data));
143 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
146 export const renameFile = (newFullPath: string) =>
147 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
148 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
149 const currentCollection = getState().collectionPanel.item;
150 if (dialog && currentCollection) {
151 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
153 dispatch(startSubmit(RENAME_FILE_DIALOG));
154 const oldPath = getFileFullPath(file);
155 const newPath = newFullPath;
156 services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath).then(() => {
157 dispatch<any>(loadCollectionPanel(currentCollection.uuid, true));
158 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
159 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
161 const errors: FormErrors<RenameFileDialogData, string> = {
162 path: `Could not rename the file: ${e.responseText}`
164 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));