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