Implement collection partial copying
[arvados.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 { default as unionize, ofType, UnionOf } from "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 } from 'redux-form';
15 import { loadProjectTreePickerProjects } from '../../../views-components/project-tree-picker/project-tree-picker';
16
17 export const collectionPanelFilesAction = unionize({
18     SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
19     TOGGLE_COLLECTION_FILE_COLLAPSE: ofType<{ id: string }>(),
20     TOGGLE_COLLECTION_FILE_SELECTION: ofType<{ id: string }>(),
21     SELECT_ALL_COLLECTION_FILES: ofType<{}>(),
22     UNSELECT_ALL_COLLECTION_FILES: ofType<{}>(),
23 }, { tag: 'type', value: 'payload' });
24
25 export type CollectionPanelFilesAction = UnionOf<typeof collectionPanelFilesAction>;
26
27 export const loadCollectionFiles = (uuid: string) =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         const files = await services.collectionService.files(uuid);
30         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
31     };
32
33 export const removeCollectionFiles = (filePaths: string[]) =>
34     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
35         const currentCollection = getState().collectionPanel.item;
36         if (currentCollection) {
37             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
38             await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
39             dispatch<any>(loadCollectionFiles(currentCollection.uuid));
40             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
41         }
42     };
43
44 export const removeCollectionsSelectedFiles = () =>
45     (dispatch: Dispatch, getState: () => RootState) => {
46         const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).map(file => file.id);
47         dispatch<any>(removeCollectionFiles(paths));
48     };
49
50 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
51
52 export const openFileRemoveDialog = (filePath: string) =>
53     (dispatch: Dispatch, getState: () => RootState) => {
54         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
55         if (file) {
56             const title = file.type === CollectionFileType.DIRECTORY
57                 ? 'Removing directory'
58                 : 'Removing file';
59             const text = file.type === CollectionFileType.DIRECTORY
60                 ? 'Are you sure you want to remove this directory?'
61                 : 'Are you sure you want to remove this file?';
62
63             dispatch(dialogActions.OPEN_DIALOG({
64                 id: FILE_REMOVE_DIALOG,
65                 data: {
66                     title,
67                     text,
68                     confirmButtonLabel: 'Remove',
69                     filePath
70                 }
71             }));
72         }
73     };
74
75 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
76
77 export const openMultipleFilesRemoveDialog = () =>
78     dialogActions.OPEN_DIALOG({
79         id: MULTIPLE_FILES_REMOVE_DIALOG,
80         data: {
81             title: 'Removing files',
82             text: 'Are you sure you want to remove selected files?',
83             confirmButtonLabel: 'Remove'
84         }
85     });
86
87 export const COLLECTION_PARTIAL_COPY = 'COLLECTION_PARTIAL_COPY';
88
89 export interface CollectionPartialCopyFormData {
90     name: string;
91     description: string;
92     projectUuid: string;
93 }
94
95 export const openCollectionPartialCopyDialog = () =>
96     (dispatch: Dispatch, getState: () => RootState) => {
97         const currentCollection = getState().collectionPanel.item;
98         if (currentCollection) {
99             const initialData = {
100                 name: currentCollection.name,
101                 description: currentCollection.description,
102                 projectUuid: ''
103             };
104             dispatch(initialize(COLLECTION_PARTIAL_COPY, initialData));
105             dispatch<any>(loadProjectTreePickerProjects(''));
106             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY, data: {} }));
107         }
108     };
109
110 export const doCollectionPartialCopy = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
111     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
112         dispatch(startSubmit(COLLECTION_PARTIAL_COPY));
113         const state = getState();
114         const currentCollection = state.collectionPanel.item;
115         if (currentCollection) {
116             const collection = await services.collectionService.get(currentCollection.uuid);
117             const collectionCopy = {
118                 ...collection,
119                 name,
120                 description,
121                 ownerUuid: projectUuid,
122                 uuid: undefined
123             };
124             const newCollection = await services.collectionService.create(collectionCopy);
125             const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
126             await services.collectionService.deleteFiles(newCollection.uuid, paths);
127             dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY }));
128             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'New collection created.', hideDuration: 2000 }));
129         }
130     };
131