c661d9f8b2d9c2ee8c95e90ca04953f8629e4f3e
[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, initialize, FormErrors } 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(currentCollection.uuid, filePaths);
43                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
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 = (filePath: string) =>
69     (dispatch: Dispatch, getState: () => RootState) => {
70         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
71         if (file) {
72             const isDirectory = file.type === CollectionFileType.DIRECTORY;
73             const title = isDirectory
74                 ? 'Removing directory'
75                 : 'Removing file';
76             const text = isDirectory
77                 ? 'Are you sure you want to remove this directory?'
78                 : 'Are you sure you want to remove this file?';
79             const info = isDirectory
80                 ? 'Removing files will change content address.'
81                 : 'Removing a file will change content address.';
82
83             dispatch(dialogActions.OPEN_DIALOG({
84                 id: FILE_REMOVE_DIALOG,
85                 data: {
86                     title,
87                     text,
88                     info,
89                     confirmButtonLabel: 'Remove',
90                     filePath
91                 }
92             }));
93         }
94     };
95
96 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
97
98 export const openMultipleFilesRemoveDialog = () =>
99     dialogActions.OPEN_DIALOG({
100         id: MULTIPLE_FILES_REMOVE_DIALOG,
101         data: {
102             title: 'Removing files',
103             text: 'Are you sure you want to remove selected files?',
104             info: 'Removing files will change content address.',
105             confirmButtonLabel: 'Remove'
106         }
107     });
108
109 export const RENAME_FILE_DIALOG = 'renameFileDialog';
110 export interface RenameFileDialogData {
111     name: string;
112     id: string;
113 }
114
115 export const openRenameFileDialog = (data: RenameFileDialogData) =>
116     (dispatch: Dispatch) => {
117         dispatch(initialize(RENAME_FILE_DIALOG, data));
118         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
119     };
120
121 export const renameFile = (newName: string) =>
122     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
123         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
124         const currentCollection = getState().collectionPanel.item;
125         if (dialog && currentCollection) {
126             const file = getNodeValue(dialog.data.id)(getState().collectionPanelFiles);
127             if (file) {
128                 dispatch(startSubmit(RENAME_FILE_DIALOG));
129                 const oldPath = getFileFullPath(file);
130                 const newPath = getFileFullPath({ ...file, name: newName });
131                 try {
132                     await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
133                     dispatch<any>(loadCollectionFiles(currentCollection.uuid));
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                         name: 'Could not rename the file'
139                     };
140                     dispatch(stopSubmit(RENAME_FILE_DIALOG, errors));
141                 }
142             }
143         }
144     };