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";
18 export const collectionPanelFilesAction = unionize({
19 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
20 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
21 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
22 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
23 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
26 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
28 export const loadCollectionFiles = (uuid: string) =>
29 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
30 const files = await services.collectionService.files(uuid);
32 // Given the array of directories and files, create the appropriate tree nodes,
33 // sort them, and add the complete url to each.
34 const tree = createCollectionFilesTree(files);
35 const sorted = sortFilesTree(tree);
36 const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
37 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
40 export const removeCollectionFiles = (filePaths: string[]) =>
41 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42 const currentCollection = getState().collectionPanel.item;
43 if (currentCollection) {
44 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
46 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
47 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
48 dispatch(snackbarActions.OPEN_SNACKBAR({
51 kind: SnackbarKind.SUCCESS
54 dispatch(snackbarActions.OPEN_SNACKBAR({
55 message: 'Could not remove file.',
57 kind: SnackbarKind.ERROR
63 export const removeCollectionsSelectedFiles = () =>
64 (dispatch: Dispatch, getState: () => RootState) => {
65 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
66 .map(getFileFullPath);
67 dispatch<any>(removeCollectionFiles(paths));
70 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
72 export const openFileRemoveDialog = (filePath: string) =>
73 (dispatch: Dispatch, getState: () => RootState) => {
74 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
76 const isDirectory = file.type === CollectionFileType.DIRECTORY;
77 const title = isDirectory
78 ? 'Removing directory'
80 const text = isDirectory
81 ? 'Are you sure you want to remove this directory?'
82 : 'Are you sure you want to remove this file?';
83 const info = isDirectory
84 ? 'Removing files will change content address.'
85 : 'Removing a file will change content address.';
87 dispatch(dialogActions.OPEN_DIALOG({
88 id: FILE_REMOVE_DIALOG,
93 confirmButtonLabel: 'Remove',
100 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
102 export const openMultipleFilesRemoveDialog = () =>
103 dialogActions.OPEN_DIALOG({
104 id: MULTIPLE_FILES_REMOVE_DIALOG,
106 title: 'Removing files',
107 text: 'Are you sure you want to remove selected files?',
108 info: 'Removing files will change content address.',
109 confirmButtonLabel: 'Remove'
113 export const RENAME_FILE_DIALOG = 'renameFileDialog';
114 export interface RenameFileDialogData {
119 export const openRenameFileDialog = (data: RenameFileDialogData) =>
120 (dispatch: Dispatch) => {
121 dispatch(initialize(RENAME_FILE_DIALOG, data));
122 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
125 export const renameFile = (newName: string) =>
126 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
127 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
128 const currentCollection = getState().collectionPanel.item;
129 if (dialog && currentCollection) {
130 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
132 dispatch(startSubmit(RENAME_FILE_DIALOG));
133 const oldPath = getFileFullPath(file);
134 const newPath = getFileFullPath({ ...file, name: newName });
136 await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
137 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
138 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
139 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
141 const errors: FormErrors<RenameFileDialogData, string> = {
142 name: 'Could not rename the file'
144 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));