15840: Project panel uses collection attributes for file count and size
[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, 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         const tree = createCollectionFilesTree(files);
32         const sorted = sortFilesTree(tree);
33         const mapped = mapTreeValues(services.collectionService.extendFileURL)(sorted);
34         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(mapped));
35     };
36
37 export const removeCollectionFiles = (filePaths: string[]) =>
38     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39         const currentCollection = getState().collectionPanel.item;
40         if (currentCollection) {
41             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing...' }));
42             try {
43                 await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
44                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
45                 dispatch(snackbarActions.OPEN_SNACKBAR({
46                     message: 'Removed.',
47                     hideDuration: 2000,
48                     kind: SnackbarKind.SUCCESS
49                 }));
50             } catch (e) {
51                 dispatch(snackbarActions.OPEN_SNACKBAR({
52                     message: 'Could not remove file.',
53                     hideDuration: 2000,
54                     kind: SnackbarKind.ERROR
55                 }));
56             }
57         }
58     };
59
60 export const removeCollectionsSelectedFiles = () =>
61     (dispatch: Dispatch, getState: () => RootState) => {
62         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true)
63             .map(getFileFullPath);
64         dispatch<any>(removeCollectionFiles(paths));
65     };
66
67 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
68
69 export const openFileRemoveDialog = (filePath: string) =>
70     (dispatch: Dispatch, getState: () => RootState) => {
71         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
72         if (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 }
115
116 export const openRenameFileDialog = (data: RenameFileDialogData) =>
117     (dispatch: Dispatch) => {
118         dispatch(initialize(RENAME_FILE_DIALOG, data));
119         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
120     };
121
122 export const renameFile = (newName: string) =>
123     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
125         const currentCollection = getState().collectionPanel.item;
126         if (dialog && currentCollection) {
127             const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
128             if (file) {
129                 dispatch(startSubmit(RENAME_FILE_DIALOG));
130                 const oldPath = getFileFullPath(file);
131                 const newPath = getFileFullPath({ ...file, name: newName });
132                 try {
133                     await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
134                     dispatch<any>(loadCollectionFiles(currentCollection.uuid));
135                     dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
136                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
137                 } catch (e) {
138                     const errors: FormErrors<RenameFileDialogData, string> = {
139                         name: 'Could not rename the file'
140                     };
141                     dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));
142                 }
143             }
144         }
145     };