Merge branch '14102-actions-repository'
[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, initialize, stopSubmit, reset } from 'redux-form';
15 import { getCommonResourceServiceError, CommonResourceServiceError } from "~/common/api/common-resource-service";
16 import { getDialog } from "~/store/dialog/dialog-reducer";
17 import { resetPickerProjectTree } from '~/store/project-tree-picker/project-tree-picker-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     };
34
35 export const removeCollectionFiles = (filePaths: string[]) =>
36     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37         const currentCollection = getState().collectionPanel.item;
38         if (currentCollection) {
39             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
40             await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
41             dispatch<any>(loadCollectionFiles(currentCollection.uuid));
42             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
43         }
44     };
45
46 export const removeCollectionsSelectedFiles = () =>
47     (dispatch: Dispatch, getState: () => RootState) => {
48         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).map(file => file.id);
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 COLLECTION_PARTIAL_COPY = 'COLLECTION_PARTIAL_COPY';
90
91 export interface CollectionPartialCopyFormData {
92     name: string;
93     description: string;
94     projectUuid: string;
95 }
96
97 export const openCollectionPartialCopyDialog = () =>
98     (dispatch: Dispatch, getState: () => RootState) => {
99         const currentCollection = getState().collectionPanel.item;
100         if (currentCollection) {
101             const initialData = {
102                 name: currentCollection.name,
103                 description: currentCollection.description,
104                 projectUuid: ''
105             };
106             dispatch(initialize(COLLECTION_PARTIAL_COPY, initialData));
107             dispatch<any>(resetPickerProjectTree());
108             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY, data: {} }));
109         }
110     };
111
112 export const doCollectionPartialCopy = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
113     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
114         dispatch(startSubmit(COLLECTION_PARTIAL_COPY));
115         const state = getState();
116         const currentCollection = state.collectionPanel.item;
117         if (currentCollection) {
118             try {
119                 const collection = await services.collectionService.get(currentCollection.uuid);
120                 const collectionCopy = {
121                     ...collection,
122                     name,
123                     description,
124                     ownerUuid: projectUuid,
125                     uuid: undefined
126                 };
127                 const newCollection = await services.collectionService.create(collectionCopy);
128                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
129                 await services.collectionService.deleteFiles(newCollection.uuid, paths);
130                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY }));
131                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'New collection created.', hideDuration: 2000 }));
132             } catch (e) {
133                 const error = getCommonResourceServiceError(e);
134                 if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
135                     dispatch(stopSubmit(COLLECTION_PARTIAL_COPY, { name: 'Collection with this name already exists.' }));
136                 } else if (error === CommonResourceServiceError.UNKNOWN) {
137                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY }));
138                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000 }));
139                 } else {
140                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY }));
141                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000 }));
142                 }
143             }
144         }
145     };
146
147 export const RENAME_FILE_DIALOG = 'renameFileDialog';
148 export interface RenameFileDialogData {
149     name: string;
150     id: string;
151 }
152
153 export const openRenameFileDialog = (data: RenameFileDialogData) =>
154     (dispatch: Dispatch) => {
155         dispatch(reset(RENAME_FILE_DIALOG));
156         dispatch(dialogActions.OPEN_DIALOG({ id: RENAME_FILE_DIALOG, data }));
157     };
158
159 export const renameFile = (newName: string) =>
160     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
161         const dialog = getDialog<RenameFileDialogData>(getState().dialog, RENAME_FILE_DIALOG);
162         const currentCollection = getState().collectionPanel.item;
163         if (dialog && currentCollection) {
164             dispatch(startSubmit(RENAME_FILE_DIALOG));
165             const oldPath = dialog.data.id;
166             const newPath = dialog.data.id.replace(dialog.data.name, newName);
167             try {
168                 await services.collectionService.moveFile(currentCollection.uuid, oldPath, newPath);
169                 dispatch<any>(loadCollectionFiles(currentCollection.uuid));
170                 dispatch(dialogActions.CLOSE_DIALOG({ id: RENAME_FILE_DIALOG }));
171                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'File name changed.', hideDuration: 2000 }));
172             } catch (e) {
173                 dispatch(stopSubmit(RENAME_FILE_DIALOG, { name: 'Could not rename the file' }));
174             }
175         }
176     };