Switch the tagging adding properties) to use the collections API
authorJanicki Artur <artur.janicki@contractors.roche.com>
Mon, 24 Sep 2018 13:05:41 +0000 (15:05 +0200)
committerJanicki Artur <artur.janicki@contractors.roche.com>
Mon, 24 Sep 2018 13:05:41 +0000 (15:05 +0200)
Feature #14043

Arvados-DCO-1.1-Signed-off-by: Janicki Artur <artur.janicki@contractors.roche.com>

src/store/collection-panel/collection-panel-action.ts
src/store/collection-panel/collection-panel-reducer.ts
src/views/collection-panel/collection-panel.tsx

index 97b6d49c7d25d154bd51471c94ef992fcb2d27f8..5e991619e77e3a405722ae3b30115b555c3e62af 100644 (file)
@@ -9,20 +9,14 @@ import { collectionPanelFilesAction } from "./collection-panel-files/collection-
 import { createTree } from "~/models/tree";
 import { RootState } from "../store";
 import { ServiceRepository } from "~/services/services";
-import { TagResource, TagProperty } from "~/models/tag";
+import { TagProperty } from "~/models/tag";
 import { snackbarActions } from "../snackbar/snackbar-actions";
 import { resourcesActions } from "~/store/resources/resources-actions";
 import { unionize, ofType, UnionOf } from '~/common/unionize';
 
 export const collectionPanelActions = unionize({
     LOAD_COLLECTION: ofType<{ uuid: string }>(),
-    LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
-    LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
-    LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: TagResource[] }>(),
-    CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
-    CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: TagResource }>(),
-    DELETE_COLLECTION_TAG: ofType<{ uuid: string }>(),
-    DELETE_COLLECTION_TAG_SUCCESS: ofType<{ uuid: string }>()
+    LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
 });
 
 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
@@ -37,46 +31,41 @@ export const loadCollectionPanel = (uuid: string) =>
         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
         dispatch(resourcesActions.SET_RESOURCES([collection]));
         dispatch<any>(loadCollectionFiles(collection.uuid));
-        dispatch<any>(loadCollectionTags(collection.uuid));
         return collection;
     };
 
-export const loadCollectionTags = (uuid: string) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
-        return services.tagService
-            .list(uuid)
-            .then(tags => {
-                dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
-            });
-    };
-
-export const createCollectionTag = (data: TagProperty) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
+export const createCollectionTag = (data: TagProperty) => 
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const item = getState().collectionPanel.item;
         const uuid = item ? item.uuid : '';
-        return services.tagService
-            .create(uuid, data)
-            .then(tag => {
-                dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Tag has been successfully added.",
-                    hideDuration: 2000
-                }));
-            });
+        try {
+            if (item) {
+                item.properties[data.key] = data.value;
+                const updatedCollection = await services.collectionService.update(uuid, item);
+                dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000 }));
+                return updatedCollection;
+            }
+            return;
+        } catch (e) {
+            return;
+        }
     };
 
-export const deleteCollectionTag = (uuid: string) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
-        return services.linkService
-            .delete(uuid)
-            .then(tag => {
-                dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
-                dispatch(snackbarActions.OPEN_SNACKBAR({
-                    message: "Tag has been successfully deleted.",
-                    hideDuration: 2000
-                }));
-            });
+export const deleteCollectionTag = (key: string) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const item = getState().collectionPanel.item;
+        const uuid = item ? item.uuid : '';
+        try {
+            if (item) {
+                delete item.properties[key];
+                const updatedCollection = await services.collectionService.update(uuid, item);
+                dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000 }));
+                return updatedCollection;
+            }
+            return;
+        } catch (e) {
+            return;
+        }
     };
index 2c3edf1a5afce13ec9059f523f042df3eef9751a..b4951b81826b14426b72e813eda7e93d14c2f8f6 100644 (file)
@@ -4,23 +4,17 @@
 
 import { collectionPanelActions, CollectionPanelAction } from "./collection-panel-action";
 import { CollectionResource } from "~/models/collection";
-import { TagResource } from "~/models/tag";
 
 export interface CollectionPanelState {
     item: CollectionResource | null;
-    tags: TagResource[];
 }
 
 const initialState = {
-    item: null,
-    tags: []
+    item: null
 };
 
 export const collectionPanelReducer = (state: CollectionPanelState = initialState, action: CollectionPanelAction) =>
     collectionPanelActions.match(action, {
         default: () => state,
         LOAD_COLLECTION_SUCCESS: ({ item }) => ({ ...state, item }),
-        LOAD_COLLECTION_TAGS_SUCCESS: ({ tags }) => ({...state, tags }),
-        CREATE_COLLECTION_TAG_SUCCESS: ({ tag }) => ({...state, tags: [...state.tags, tag] }),
-        DELETE_COLLECTION_TAG_SUCCESS: ({ uuid }) => ({...state, tags: state.tags.filter(tag => tag.uuid !== uuid) })
     });
index 8e46385cc9cfa694c938c6b1ce5183e32d7f858c..b796abeb70f6899d44782de94fe5d73710505896 100644 (file)
@@ -66,8 +66,7 @@ export const CollectionPanel = withStyles(styles)(
     connect((state: RootState, props: RouteComponentProps<{ id: string }>) => {
         const collection = getResource(props.match.params.id)(state.resources);
         return {
-            item: collection,
-            tags: state.collectionPanel.tags
+            item: collection
         };
     })(
         class extends React.Component<CollectionPanelProps> {
@@ -118,10 +117,10 @@ export const CollectionPanel = withStyles(styles)(
                                 <Grid item xs={12}><CollectionTagForm /></Grid>
                                 <Grid item xs={12}>
                                     {
-                                        tags.map(tag => {
-                                            return <Chip key={tag.etag} className={classes.tag}
-                                                onDelete={this.handleDelete(tag.uuid)}
-                                                label={renderTagLabel(tag)} />;
+                                        Object.keys(item.properties).map( key => {
+                                            return <Chip key={key} className={classes.tag}
+                                                onDelete={this.handleDelete(key)}
+                                                label={`${key}: ${item.properties[key]}`} />;
                                         })
                                     }
                                 </Grid>
@@ -147,8 +146,8 @@ export const CollectionPanel = withStyles(styles)(
                 this.props.dispatch<any>(openContextMenu(event, resource));
             }
 
-            handleDelete = (uuid: string) => () => {
-                this.props.dispatch<any>(deleteCollectionTag(uuid));
+            handleDelete = (key: string) => () => {
+                this.props.dispatch<any>(deleteCollectionTag(key));
             }
 
             onCopy = () => {
@@ -160,8 +159,3 @@ export const CollectionPanel = withStyles(styles)(
         }
     )
 );
-
-const renderTagLabel = (tag: TagResource) => {
-    const { properties } = tag;
-    return `${properties.key}: ${properties.value}`;
-};