Merge branch '17016-delete-single-file-deletes-whole-collection'
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-files / collection-panel-files-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
19
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>(),
27 });
28
29 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
30
31 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
32 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
33
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));
45         }).catch(() => {
46             dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PANEL_LOAD_FILES));
47             dispatch(snackbarActions.OPEN_SNACKBAR({
48                 message: `Error getting file list`,
49                 hideDuration: 2000,
50                 kind: SnackbarKind.ERROR
51             }));
52         });
53     };
54
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({
62                     message: 'Removed.',
63                     hideDuration: 2000,
64                     kind: SnackbarKind.SUCCESS
65                 }));
66             }).catch(e =>
67                 dispatch(snackbarActions.OPEN_SNACKBAR({
68                     message: 'Could not remove file.',
69                     hideDuration: 2000,
70                     kind: SnackbarKind.ERROR
71                 }))
72             );
73         }
74     };
75
76 export const removeCollectionsSelectedFiles = () =>
77     (dispatch: Dispatch, getState: () => RootState) => {
78         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
79             .map(getFileFullPath);
80         dispatch<any>(removeCollectionFiles(paths));
81     };
82
83 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
84
85 export const openFileRemoveDialog = (filePath: string) =>
86     (dispatch: Dispatch, getState: () => RootState) => {
87         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
88         if (file) {
89             const isDirectory = file.type === CollectionFileType.DIRECTORY;
90             const title = isDirectory
91                 ? 'Removing directory'
92                 : 'Removing file';
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.';
99
100             dispatch(dialogActions.OPEN_DIALOG({
101                 id: FILE_REMOVE_DIALOG,
102                 data: {
103                     title,
104                     text,
105                     info,
106                     confirmButtonLabel: 'Remove',
107                     filePath
108                 }
109             }));
110         }
111     };
112
113 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
114
115 export const openMultipleFilesRemoveDialog = () =>
116     dialogActions.OPEN_DIALOG({
117         id: MULTIPLE_FILES_REMOVE_DIALOG,
118         data: {
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'
123         }
124     });
125
126 export const RENAME_FILE_DIALOG = 'renameFileDialog';
127 export interface RenameFileDialogData {
128     name: string;
129     id: string;
130     path: string;
131 }
132
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 }));
137     };
138
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);
145             if (file) {
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 }));
153                 }).catch(e => {
154                     const errors: FormErrors<RenameFileDialogData, string> = {
155                         path: `Could not rename the file: ${e.responseText}`
156                     };
157                     dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));
158                 });
159             }
160         }
161     };