15685: Merge branch 'master' into 15685-file-renaming-empty-name
[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
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>(),
26 });
27
28 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
29
30 export const COLLECTION_PANEL_LOAD_FILES = 'collectionPanelLoadFiles';
31 export const COLLECTION_PANEL_LOAD_FILES_THRESHOLD = 40000;
32
33 export const loadCollectionFiles = (uuid: string) =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PANEL_LOAD_FILES));
36         const files = await services.collectionService.files(uuid);
37
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     };
46
47 export const removeCollectionFiles = (filePaths: string[]) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const currentCollection = getState().collectionPanel.item;
50         if (currentCollection) {
51             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
52             try {
53                 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
54                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
55                 dispatch(snackbarActions.OPEN_SNACKBAR({
56                     message: 'Removed.',
57                     hideDuration: 2000,
58                     kind: SnackbarKind.SUCCESS
59                 }));
60             } catch (e) {
61                 dispatch(snackbarActions.OPEN_SNACKBAR({
62                     message: 'Could not remove file.',
63                     hideDuration: 2000,
64                     kind: SnackbarKind.ERROR
65                 }));
66             }
67         }
68     };
69
70 export const removeCollectionsSelectedFiles = () =>
71     (dispatch: Dispatch, getState: () => RootState) => {
72         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
73             .map(getFileFullPath);
74         dispatch<any>(removeCollectionFiles(paths));
75     };
76
77 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
78
79 export const openFileRemoveDialog = (filePath: string) =>
80     (dispatch: Dispatch, getState: () => RootState) => {
81         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
82         if (file) {
83             const isDirectory = file.type === CollectionFileType.DIRECTORY;
84             const title = isDirectory
85                 ? 'Removing directory'
86                 : 'Removing file';
87             const text = isDirectory
88                 ? 'Are you sure you want to remove this directory?'
89                 : 'Are you sure you want to remove this file?';
90             const info = isDirectory
91                 ? 'Removing files will change content address.'
92                 : 'Removing a file will change content address.';
93
94             dispatch(dialogActions.OPEN_DIALOG({
95                 id: FILE_REMOVE_DIALOG,
96                 data: {
97                     title,
98                     text,
99                     info,
100                     confirmButtonLabel: 'Remove',
101                     filePath
102                 }
103             }));
104         }
105     };
106
107 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
108
109 export const openMultipleFilesRemoveDialog = () =>
110     dialogActions.OPEN_DIALOG({
111         id: MULTIPLE_FILES_REMOVE_DIALOG,
112         data: {
113             title: 'Removing files',
114             text: 'Are you sure you want to remove selected files?',
115             info: 'Removing files will change content address.',
116             confirmButtonLabel: 'Remove'
117         }
118     });
119
120 export const RENAME_FILE_DIALOG = 'renameFileDialog';
121 export interface RenameFileDialogData {
122     name: string;
123     id: string;
124 }
125
126 export const openRenameFileDialog = (data: RenameFileDialogData) =>
127     (dispatch: Dispatch) => {
128         dispatch(initialize(RENAME_FILE_DIALOG, data));
129         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
130     };
131
132 export const renameFile = (newName: string) =>
133     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
134         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
135         const currentCollection = getState().collectionPanel.item;
136         if (dialog && currentCollection) {
137             const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
138             if (file) {
139                 dispatch(startSubmit(RENAME_FILE_DIALOG));
140                 const oldPath = getFileFullPath(file);
141                 const newPath = getFileFullPath({ ...file, name: newName });
142                 try {
143                     await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
144                     dispatch<any>(loadCollectionFiles(currentCollection.uuid));
145                     dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
146                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
147                 } catch (e) {
148                     const errors: FormErrors<RenameFileDialogData, string> = {
149                         name: `Could not rename the file: ${e.responseText}`
150                     };
151                     dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));
152                 }
153             }
154         }
155     };