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