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