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