Merge branch '17579-Clear-table-filter-when-changing-the-project' into main
[arvados-workbench2.git] / src / store / collections / collection-partial-copy-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from 'redux';
6 import { difference } from "lodash";
7 import { RootState } from 'store/store';
8 import { FormErrors, initialize, startSubmit, stopSubmit } from 'redux-form';
9 import { resetPickerProjectTree } from 'store/project-tree-picker/project-tree-picker-actions';
10 import { dialogActions } from 'store/dialog/dialog-actions';
11 import { ServiceRepository } from 'services/services';
12 import { filterCollectionFilesBySelection } from '../collection-panel/collection-panel-files/collection-panel-files-state';
13 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
14 import { getCommonResourceServiceError, CommonResourceServiceError } from 'services/common-service/common-resource-service';
15 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
16 import { initProjectsTreePicker } from 'store/tree-picker/tree-picker-actions';
17
18 export const COLLECTION_PARTIAL_COPY_FORM_NAME = 'COLLECTION_PARTIAL_COPY_DIALOG';
19 export const COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION = 'COLLECTION_PARTIAL_COPY_TO_SELECTED_DIALOG';
20
21 export interface CollectionPartialCopyFormData {
22     name: string;
23     description: string;
24     projectUuid: string;
25 }
26
27 export interface CollectionPartialCopyToSelectedCollectionFormData {
28     collectionUuid: string;
29 }
30
31 export const openCollectionPartialCopyDialog = () =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const currentCollection = getState().collectionPanel.item;
34         if (currentCollection) {
35             const initialData = {
36                 name: currentCollection.name,
37                 description: currentCollection.description,
38                 projectUuid: ''
39             };
40             dispatch(initialize(COLLECTION_PARTIAL_COPY_FORM_NAME, initialData));
41             dispatch<any>(resetPickerProjectTree());
42             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_FORM_NAME));
43             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME, data: {} }));
44         }
45     };
46
47 export const copyCollectionPartial = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         dispatch(startSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME));
50         const state = getState();
51         const currentCollection = state.collectionPanel.item;
52         if (currentCollection) {
53             try {
54                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
55                 const collectionManifestText = await services.collectionService.get(currentCollection.uuid, undefined, ['manifestText']);
56                 const collectionCopy = {
57                     name,
58                     description,
59                     ownerUuid: projectUuid,
60                     uuid: undefined,
61                     manifestText: collectionManifestText.manifestText,
62                 };
63                 const newCollection = await services.collectionService.create(collectionCopy);
64                 const copiedFiles = await services.collectionService.files(newCollection.uuid);
65                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, true).map(file => file.id);
66                 const filesToDelete = copiedFiles.map(({ id }) => id).filter(file => {
67                     return !paths.find(path => path.indexOf(file.replace(newCollection.uuid, '')) > -1);
68                 });
69                 await services.collectionService.deleteFiles(
70                     newCollection.uuid,
71                     filesToDelete
72                 );
73                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
74                 dispatch(snackbarActions.OPEN_SNACKBAR({
75                     message: 'New collection created.',
76                     hideDuration: 2000,
77                     kind: SnackbarKind.SUCCESS
78                 }));
79                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
80             } catch (e) {
81                 const error = getCommonResourceServiceError(e);
82                 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
83                     dispatch(stopSubmit(COLLECTION_PARTIAL_COPY_FORM_NAME, { name: 'Collection with this name already exists.' } as FormErrors));
84                 } else if (error === CommonResourceServiceError.UNKNOWN) {
85                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
86                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not create a copy of collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
87                 } else {
88                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_FORM_NAME }));
89                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Collection has been copied but may contain incorrect files.', hideDuration: 2000, kind: SnackbarKind.ERROR }));
90                 }
91                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_FORM_NAME));
92             }
93         }
94     };
95
96 export const openCollectionPartialCopyToSelectedCollectionDialog = () =>
97     (dispatch: Dispatch, getState: () => RootState) => {
98         const currentCollection = getState().collectionPanel.item;
99         if (currentCollection) {
100             const initialData = {
101                 collectionUuid: ''
102             };
103             dispatch(initialize(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, initialData));
104             dispatch<any>(resetPickerProjectTree());
105             dispatch<any>(initProjectsTreePicker(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
106             dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION, data: {} }));
107         }
108     };
109
110 export const copyCollectionPartialToSelectedCollection = ({ collectionUuid }: CollectionPartialCopyToSelectedCollectionFormData) =>
111     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
112         dispatch(startSubmit(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
113         const state = getState();
114         const currentCollection = state.collectionPanel.item;
115
116         if (currentCollection && !currentCollection.manifestText) {
117             const fetchedCurrentCollection = await services.collectionService.get(currentCollection.uuid, undefined, ['manifestText']);
118             currentCollection.manifestText = fetchedCurrentCollection.manifestText;
119             currentCollection.unsignedManifestText = fetchedCurrentCollection.unsignedManifestText;
120         }
121
122         if (currentCollection) {
123             try {
124                 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
125                 const selectedCollection = await services.collectionService.get(collectionUuid);
126                 const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
127                 const pathsToRemove = paths.filter(path => {
128                     const a = path.split('/');
129                     const fileExistsInSelectedCollection = selectedCollection.manifestText.includes(a[1]);
130                     if (fileExistsInSelectedCollection) {
131                         return path;
132                     } else {
133                         return null;
134                     }
135                 });
136                 const diffPathToRemove = difference(paths, pathsToRemove);
137                 await services.collectionService.deleteFiles(selectedCollection.uuid, pathsToRemove.map(path => path.replace(currentCollection.uuid, collectionUuid)));
138                 const collectionWithDeletedFiles = await services.collectionService.get(collectionUuid, undefined, ['uuid', 'manifestText']);
139                 await services.collectionService.update(collectionUuid, { manifestText: `${collectionWithDeletedFiles.manifestText}${(currentCollection.manifestText ? currentCollection.manifestText : currentCollection.unsignedManifestText) || ''}` });
140                 await services.collectionService.deleteFiles(collectionWithDeletedFiles.uuid, diffPathToRemove.map(path => path.replace(currentCollection.uuid, collectionUuid)));
141                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
142                 dispatch(snackbarActions.OPEN_SNACKBAR({
143                     message: 'Files has been copied to selected collection.',
144                     hideDuration: 2000,
145                     kind: SnackbarKind.SUCCESS
146                 }));
147                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
148             } catch (e) {
149                 const error = getCommonResourceServiceError(e);
150                 if (error === CommonResourceServiceError.UNKNOWN) {
151                     dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION }));
152                     dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Could not copy this files to selected collection', hideDuration: 2000, kind: SnackbarKind.ERROR }));
153                 }
154                 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_PARTIAL_COPY_TO_SELECTED_COLLECTION));
155             }
156         }
157     };