Fix removing collection files
[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 } 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 } from "~/models/tree";
13 import { filterCollectionFilesBySelection } from './collection-panel-files-state';
14 import { startSubmit, stopSubmit, reset, initialize } from 'redux-form';
15 import { getDialog } from "~/store/dialog/dialog-reducer";
16 import { getFileFullPath } from "~/services/collection-service/collection-service-files-response";
17 import { resourcesDataActions } from "~/store/resources-data/resources-data-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 loadCollectionFiles = (uuid: string) =>
30     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31         const files = await services.collectionService.files(uuid);
32         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
33         dispatch(resourcesDataActions.SET_FILES({ uuid, files }));
34     };
35
36 export const removeCollectionFiles = (filePaths: string[]) =>
37     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         const currentCollection = getState().collectionPanel.item;
39         if (currentCollection) {
40             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
41             try {
42                 await services.collectionService.deleteFiles('', filePaths);
43                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
44                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
45             } catch (e) {
46                 dispatch(snackbarActions.OPEN_SNACKBAR({
47                     message: 'Could not remove file.',
48                     hideDuration: 2000,
49                     kind: SnackbarKind.ERROR
50                 }));
51             }
52         }
53     };
54
55 export const removeCollectionsSelectedFiles = () =>
56     (dispatch: Dispatch, getState: () => RootState) => {
57         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
58             .map(getFileFullPath);
59         dispatch<any>(removeCollectionFiles(paths));
60     };
61
62 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
63
64 export const openFileRemoveDialog = (filePath: string) =>
65     (dispatch: Dispatch, getState: () => RootState) => {
66         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
67         if (file) {
68             const isDirectory = file.type === CollectionFileType.DIRECTORY;
69             const title = isDirectory
70                 ? 'Removing directory'
71                 : 'Removing file';
72             const text = isDirectory
73                 ? 'Are you sure you want to remove this directory?'
74                 : 'Are you sure you want to remove this file?';
75             const info = isDirectory
76                 ? 'Removing files will change content adress.'
77                 : 'Removing a file will change content adress.';
78
79             dispatch(dialogActions.OPEN_DIALOG({
80                 id: FILE_REMOVE_DIALOG,
81                 data: {
82                     title,
83                     text,
84                     info,
85                     confirmButtonLabel: 'Remove',
86                     filePath
87                 }
88             }));
89         }
90     };
91
92 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
93
94 export const openMultipleFilesRemoveDialog = () =>
95     dialogActions.OPEN_DIALOG({
96         id: MULTIPLE_FILES_REMOVE_DIALOG,
97         data: {
98             title: 'Removing files',
99             text: 'Are you sure you want to remove selected files?',
100             info: 'Removing files will change content adress.',
101             confirmButtonLabel: 'Remove'
102         }
103     });
104
105 export const RENAME_FILE_DIALOG = 'renameFileDialog';
106 export interface RenameFileDialogData {
107     name: string;
108     id: string;
109 }
110
111 export const openRenameFileDialog = (data: RenameFileDialogData) =>
112     (dispatch: Dispatch) => {
113         dispatch(initialize(RENAME_FILE_DIALOG, data));
114         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
115     };
116
117 export const renameFile = (newName: string) =>
118     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
119         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
120         const currentCollection = getState().collectionPanel.item;
121         if (dialog && currentCollection) {
122             const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
123             if (file) {
124                 dispatch(startSubmit(RENAME_FILE_DIALOG));
125                 const oldPath = getFileFullPath(file);
126                 const newPath = getFileFullPath({ ...file, name: newName });
127                 try {
128                     await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
129                     dispatch<any>(loadCollectionFiles(currentCollection.uuid));
130                     dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
131                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
132                 } catch (e) {
133                     dispatch(stopSubmit(RENAME_FILE_DIALOG, { name: 'Could not rename the file' }));
134                 }
135             }
136         }
137     };