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