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