Implement collection partial copying
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Sat, 18 Aug 2018 06:55:58 +0000 (08:55 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Sat, 18 Aug 2018 06:55:58 +0000 (08:55 +0200)
Feature #14014

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/services/collection-service/collection-service.ts
src/store/collection-panel/collection-panel-files/collection-panel-files-actions.ts
src/store/collection-panel/collection-panel-files/collection-panel-files-reducer.ts
src/store/collection-panel/collection-panel-files/collection-panel-files-state.ts
src/views-components/collection-partial-copy-dialog/collection-partial-copy-dialog.tsx
src/views-components/context-menu/action-sets/collection-files-action-set.ts
src/views-components/form-dialog/collection-form-dialog.tsx

index b154f40a951235aeed7f8b558baa297fe5bafbb3..671a1e1556155ba39529920e2be6e2f4b1d49bd6 100644 (file)
@@ -28,8 +28,10 @@ export class CollectionService extends CommonResourceService<CollectionResource>
         return Promise.reject();
     }
 
-    async deleteFile(collectionUuid: string, filePath: string) {
-        return this.webdavClient.delete(`/c=${collectionUuid}${filePath}`);
+    async deleteFiles(collectionUuid: string, filePaths: string[]) {
+        for (const path of filePaths) {
+            await this.webdavClient.delete(`/c=${collectionUuid}${path}`);
+        }
     }
 
     async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress) {
index cedfbebef5aded305b3237e125ee80857c96361b..114446a0f0107c3e6c064410ea73907b972edab2 100644 (file)
@@ -8,9 +8,11 @@ import { CollectionFilesTree, CollectionFileType } from "~/models/collection-fil
 import { ServiceRepository } from "~/services/services";
 import { RootState } from "../../store";
 import { snackbarActions } from "../../snackbar/snackbar-actions";
-import { dialogActions } from "../../dialog/dialog-actions";
-import { getNodeValue, getNodeDescendants } from "~/models/tree";
-import { CollectionPanelDirectory, CollectionPanelFile } from "./collection-panel-files-state";
+import { dialogActions } from '../../dialog/dialog-actions';
+import { getNodeValue } from "~/models/tree";
+import { filterCollectionFilesBySelection } from './collection-panel-files-state';
+import { startSubmit, initialize } from 'redux-form';
+import { loadProjectTreePickerProjects } from '../../../views-components/project-tree-picker/project-tree-picker';
 
 export const collectionPanelFilesAction = unionize({
     SET_COLLECTION_FILES: ofType<CollectionFilesTree>(),
@@ -30,30 +32,23 @@ export const loadCollectionFiles = (uuid: string) =>
 
 export const removeCollectionFiles = (filePaths: string[]) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const { item } = getState().collectionPanel;
-        if (item) {
+        const currentCollection = getState().collectionPanel.item;
+        if (currentCollection) {
             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...' }));
-            const promises = filePaths.map(filePath => services.collectionService.deleteFile(item.uuid, filePath));
-            await Promise.all(promises);
-            dispatch<any>(loadCollectionFiles(item.uuid));
+            await services.collectionService.deleteFiles(currentCollection.uuid, filePaths);
+            dispatch<any>(loadCollectionFiles(currentCollection.uuid));
             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removed.', hideDuration: 2000 }));
         }
     };
 
 export const removeCollectionsSelectedFiles = () =>
     (dispatch: Dispatch, getState: () => RootState) => {
-        const tree = getState().collectionPanelFiles;
-        const allFiles = getNodeDescendants('')(tree)
-            .map(id => getNodeValue(id)(tree))
-            .filter(file => file !== undefined) as Array<CollectionPanelDirectory | CollectionPanelFile>;
-
-        const selectedDirectories = allFiles.filter(file => file.selected && file.type === CollectionFileType.DIRECTORY);
-        const selectedFiles = allFiles.filter(file => file.selected && !selectedDirectories.some(dir => dir.id === file.path));
-        const paths = [...selectedDirectories, ...selectedFiles].map(file => file.id);
+        const paths = filterCollectionFilesBySelection(getState().collectionPanelFiles, true).map(file => file.id);
         dispatch<any>(removeCollectionFiles(paths));
     };
 
 export const FILE_REMOVE_DIALOG = 'fileRemoveDialog';
+
 export const openFileRemoveDialog = (filePath: string) =>
     (dispatch: Dispatch, getState: () => RootState) => {
         const file = getNodeValue(filePath)(getState().collectionPanelFiles);
@@ -78,6 +73,7 @@ export const openFileRemoveDialog = (filePath: string) =>
     };
 
 export const MULTIPLE_FILES_REMOVE_DIALOG = 'multipleFilesRemoveDialog';
+
 export const openMultipleFilesRemoveDialog = () =>
     dialogActions.OPEN_DIALOG({
         id: MULTIPLE_FILES_REMOVE_DIALOG,
@@ -87,3 +83,49 @@ export const openMultipleFilesRemoveDialog = () =>
             confirmButtonLabel: 'Remove'
         }
     });
+
+export const COLLECTION_PARTIAL_COPY = 'COLLECTION_PARTIAL_COPY';
+
+export interface CollectionPartialCopyFormData {
+    name: string;
+    description: string;
+    projectUuid: string;
+}
+
+export const openCollectionPartialCopyDialog = () =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        const currentCollection = getState().collectionPanel.item;
+        if (currentCollection) {
+            const initialData = {
+                name: currentCollection.name,
+                description: currentCollection.description,
+                projectUuid: ''
+            };
+            dispatch(initialize(COLLECTION_PARTIAL_COPY, initialData));
+            dispatch<any>(loadProjectTreePickerProjects(''));
+            dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY, data: {} }));
+        }
+    };
+
+export const doCollectionPartialCopy = ({ name, description, projectUuid }: CollectionPartialCopyFormData) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(startSubmit(COLLECTION_PARTIAL_COPY));
+        const state = getState();
+        const currentCollection = state.collectionPanel.item;
+        if (currentCollection) {
+            const collection = await services.collectionService.get(currentCollection.uuid);
+            const collectionCopy = {
+                ...collection,
+                name,
+                description,
+                ownerUuid: projectUuid,
+                uuid: undefined
+            };
+            const newCollection = await services.collectionService.create(collectionCopy);
+            const paths = filterCollectionFilesBySelection(state.collectionPanelFiles, false).map(file => file.id);
+            await services.collectionService.deleteFiles(newCollection.uuid, paths);
+            dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_PARTIAL_COPY }));
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'New collection created.', hideDuration: 2000 }));
+        }
+    };
+
index 08b60308c42bb9d4f3dfdeb72eae23f4b45946de..a34fc21e9af2281c82e59c7af72767b71964e683 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { CollectionPanelFilesState, CollectionPanelFile, CollectionPanelDirectory, mapCollectionFileToCollectionPanelFile, mergeCollectionPanelFilesStates } from "./collection-panel-files-state";
+import { CollectionPanelFilesState, CollectionPanelFile, CollectionPanelDirectory, mapCollectionFileToCollectionPanelFile, mergeCollectionPanelFilesStates } from './collection-panel-files-state';
 import { CollectionPanelFilesAction, collectionPanelFilesAction } from "./collection-panel-files-actions";
 import { createTree, mapTreeValues, getNode, setNode, getNodeAncestors, getNodeDescendants, setNodeValueWith, mapTree } from "~/models/tree";
 import { CollectionFileType } from "~/models/collection-file";
index 35b81d2e121e134b1b10f8aad6223a0267da9765..8c5ebd72936a5d60e0714589e33b6635187b47bd 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Tree, TreeNode, mapTreeValues, getNodeValue } from '~/models/tree';
+import { Tree, TreeNode, mapTreeValues, getNodeValue, getNodeDescendants } from '~/models/tree';
 import { CollectionFile, CollectionDirectory, CollectionFileType } from '~/models/collection-file';
 
 export type CollectionPanelFilesState = Tree<CollectionPanelDirectory | CollectionPanelFile>;
@@ -34,4 +34,14 @@ export const mergeCollectionPanelFilesStates = (oldState: CollectionPanelFilesSt
                 : { ...value, selected: oldValue.selected }
             : value;
     })(newState);
-}; 
+};
+
+export const filterCollectionFilesBySelection = (tree: CollectionPanelFilesState, selected: boolean) => {
+    const allFiles = getNodeDescendants('')(tree)
+        .map(id => getNodeValue(id)(tree))
+        .filter(file => file !== undefined) as Array<CollectionPanelDirectory | CollectionPanelFile>;
+
+    const selectedDirectories = allFiles.filter(file => file.selected === selected && file.type === CollectionFileType.DIRECTORY);
+    const selectedFiles = allFiles.filter(file => file.selected === selected && !selectedDirectories.some(dir => dir.id === file.path));
+    return [...selectedDirectories, ...selectedFiles];
+};
index 0105ad2ce839c13b783cf104b58a30e6931fc7d2..e230470b418e065b0c01956d340fbbcd07b85907 100644 (file)
@@ -2,29 +2,25 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Dispatch, compose } from "redux";
-import { reduxForm, reset, startSubmit, stopSubmit } from "redux-form";
-import { withDialog } from "~/store/dialog/with-dialog";
-import { dialogActions } from "~/store/dialog/dialog-actions";
-import { loadProjectTreePickerProjects } from "../project-tree-picker/project-tree-picker";
-import { CollectionPartialCopyFormDialog } from "~/views-components/form-dialog/collection-form-dialog";
-
-export const COLLECTION_PARTIAL_COPY = 'COLLECTION_PARTIAL_COPY';
-
-export const openCollectionPartialCopyDialog = () =>
-    (dispatch: Dispatch) => {
-        dispatch(reset(COLLECTION_PARTIAL_COPY));
-        dispatch<any>(loadProjectTreePickerProjects(''));
-        dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_PARTIAL_COPY, data: {} }));
-    };
-
+import * as React from "react";
+import { compose } from "redux";
+import { reduxForm, InjectedFormProps } from 'redux-form';
+import { withDialog, WithDialogProps } from '~/store/dialog/with-dialog';
+import { CollectionPartialCopyFields } from '../form-dialog/collection-form-dialog';
+import { FormDialog } from '~/components/form-dialog/form-dialog';
+import { COLLECTION_PARTIAL_COPY, doCollectionPartialCopy, CollectionPartialCopyFormData } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
 
 export const CollectionPartialCopyDialog = compose(
     withDialog(COLLECTION_PARTIAL_COPY),
     reduxForm({
         form: COLLECTION_PARTIAL_COPY,
-        onSubmit: (data, dispatch) => {
-            dispatch(startSubmit(COLLECTION_PARTIAL_COPY));
-            setTimeout(() => dispatch(stopSubmit(COLLECTION_PARTIAL_COPY, { name: 'Invalid name' })), 2000);
+        onSubmit: (data: CollectionPartialCopyFormData, dispatch) => {
+            dispatch(doCollectionPartialCopy(data));
         }
-    }))(CollectionPartialCopyFormDialog);
+    }))((props: WithDialogProps<string> & InjectedFormProps<CollectionPartialCopyFormData>) =>
+        <FormDialog
+            dialogTitle='Create a collection'
+            formFields={CollectionPartialCopyFields}
+            submitLabel='Create a collection'
+            {...props}
+        />);
index 69815efd79058f4ca658f56561ab8e48246a289e..965109cadcaeaae12b4add5f788416175ab8822e 100644 (file)
@@ -4,8 +4,7 @@
 
 import { ContextMenuActionSet } from "../context-menu-action-set";
 import { collectionPanelFilesAction, openMultipleFilesRemoveDialog } from "~/store/collection-panel/collection-panel-files/collection-panel-files-actions";
-import { openCollectionPartialCopyDialog } from "~/views-components/collection-partial-copy-dialog/collection-partial-copy-dialog";
-
+import { openCollectionPartialCopyDialog } from '~/store/collection-panel/collection-panel-files/collection-panel-files-actions';
 
 export const collectionFilesActionSet: ContextMenuActionSet = [[{
     name: "Select all",
index 937558cc9ae2e88188b5385058418b06f73bb61e..d5f1d8521aae89fe693ce44efb9e1248eca42413 100644 (file)
@@ -3,20 +3,10 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import * as React from "react";
-import { InjectedFormProps, Field, WrappedFieldProps } from "redux-form";
-import { WithDialogProps } from "~/store/dialog/with-dialog";
+import { Field, WrappedFieldProps } from "redux-form";
 import { TextField } from "~/components/text-field/text-field";
 import { COLLECTION_NAME_VALIDATION, COLLECTION_DESCRIPTION_VALIDATION, COLLECTION_PROJECT_VALIDATION } from "~/validators/validators";
 import { ProjectTreePicker } from "../project-tree-picker/project-tree-picker";
-import { FormDialog } from '../../components/form-dialog/form-dialog';
-
-export const CollectionPartialCopyFormDialog = (props: WithDialogProps<string> & InjectedFormProps<{ name: string }>) =>
-    <FormDialog
-        dialogTitle='Create a collection'
-        formFields={CollectionPartialCopyFields}
-        submitLabel='Create a collection'
-        {...props}
-    />;
 
 export const CollectionPartialCopyFields = () => <div style={{ display: 'flex' }}>
     <div>