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";
19 export const collectionPanelFilesAction = unionize({
20 SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
21 TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
22 TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
23 SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
24 UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
25 ON_SEARCH_CHANGE: ofType<string>(),
28 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
30 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
32 export const setCollectionFiles = (files, joinParents = true) => (dispatch: any) => {
33 const tree = createCollectionFilesTree(files, joinParents);
34 const sorted = sortFilesTree(tree);
35 const mapped = mapTreeValues(servicesProvider.getServices().collectionService.extendFileURL)(sorted);
36 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
39 export const removeCollectionFiles = (filePaths: string[]) =>
40 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41 const currentCollection = getState().collectionPanel.item;
42 if (currentCollection) {
43 services.collectionService.deleteFiles(currentCollection.uuid, filePaths).then(() => {
44 dispatch(snackbarActions.OPEN_SNACKBAR({
47 kind: SnackbarKind.SUCCESS
50 dispatch(snackbarActions.OPEN_SNACKBAR({
51 message: 'Could not remove file.',
53 kind: SnackbarKind.ERROR
59 export const removeCollectionsSelectedFiles = () =>
60 (dispatch: Dispatch, getState: () => RootState) => {
61 const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
62 .map(getFileFullPath);
63 dispatch<any>(removeCollectionFiles(paths));
66 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
68 export const openFileRemoveDialog = (filePath: string) =>
69 (dispatch: Dispatch, getState: () => RootState) => {
70 const file = getNodeValue(filePath)(getState().collectionPanelFiles);
72 const isDirectory = file.type === CollectionFileType.DIRECTORY;
73 const title = isDirectory
74 ? 'Removing directory'
76 const text = isDirectory
77 ? 'Are you sure you want to remove this directory?'
78 : 'Are you sure you want to remove this file?';
79 const info = isDirectory
80 ? 'Removing files will change content address.'
81 : 'Removing a file will change content address.';
83 dispatch(dialogActions.OPEN_DIALOG({
84 id: FILE_REMOVE_DIALOG,
89 confirmButtonLabel: 'Remove',
96 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
98 export const openMultipleFilesRemoveDialog = () =>
99 dialogActions.OPEN_DIALOG({
100 id: MULTIPLE_FILES_REMOVE_DIALOG,
102 title: 'Removing files',
103 text: 'Are you sure you want to remove selected files?',
104 info: 'Removing files will change content address.',
105 confirmButtonLabel: 'Remove'
109 export const RENAME_FILE_DIALOG = 'renameFileDialog';
110 export interface RenameFileDialogData {
116 export const openRenameFileDialog = (data: RenameFileDialogData) =>
117 (dispatch: Dispatch) => {
118 dispatch(initialize(RENAME_FILE_DIALOG, data));
119 dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
122 export const renameFile = (newFullPath: string) =>
123 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124 const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
125 const currentCollection = getState().collectionPanel.item;
126 if (dialog && currentCollection) {
127 const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
129 dispatch(startSubmit(RENAME_FILE_DIALOG));
130 const oldPath = getFileFullPath(file);
131 const newPath = newFullPath;
132 services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath).then(() => {
133 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
134 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
136 const errors: FormErrors<RenameFileDialogData, string> = {
137 path: `Could not rename the file: ${e.responseText}`
139 dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));