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