15781: Adds support for multi-value property additions on collections.
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-action.ts
index 3c6601657083296a056f40f711ad331f579a516c..f4244389fdf22ab2a2b0f5f7546dda687636bd9c 100644 (file)
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { unionize, ofType, UnionOf } from "unionize";
-import { CommonResourceService } from "../../common/api/common-resource-service";
-import { apiClient } from "../../common/api/server-api";
 import { Dispatch } from "redux";
-import { ResourceKind } from "../../models/resource";
-import { CollectionResource } from "../../models/collection";
+import { loadCollectionFiles } from "./collection-panel-files/collection-panel-files-actions";
+import { CollectionResource } from '~/models/collection';
+import { collectionPanelFilesAction } from "./collection-panel-files/collection-panel-files-actions";
+import { createTree } from "~/models/tree";
+import { RootState } from "~/store/store";
+import { ServiceRepository } from "~/services/services";
+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';
+import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
+import { navigateTo } from '~/store/navigation/navigation-action';
+import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
 
 export const collectionPanelActions = unionize({
-    LOAD_COLLECTION: ofType<{ uuid: string, kind: ResourceKind }>(),
-    LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
-}, { tag: 'type', value: 'payload' });
+    SET_COLLECTION: ofType<CollectionResource>(),
+    LOAD_COLLECTION: ofType<{ uuid: string }>(),
+    LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
+});
 
 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
 
-export const loadCollection = (uuid: string, kind: ResourceKind) =>
-    (dispatch: Dispatch) => {
-        dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid, kind }));
-        return new CommonResourceService(apiClient, "collections")
-            .get(uuid)
-            .then(item => {
-                dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: item as CollectionResource }));
-            });
+export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
+
+export const loadCollectionPanel = (uuid: string) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
+        dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
+        const collection = await services.collectionService.get(uuid);
+        dispatch(loadDetailsPanel(collection.uuid));
+        dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
+        dispatch(resourcesActions.SET_RESOURCES([collection]));
+        dispatch<any>(loadCollectionFiles(collection.uuid));
+        return collection;
     };
 
+export const createCollectionTag = (data: TagProperty) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const item = getState().collectionPanel.item;
+        const uuid = item ? item.uuid : '';
+        try {
+            if (item) {
+                const key = data.keyID || data.key;
+                const value = data.valueID || data.value;
+                if (item.properties[key]) {
+                    if (Array.isArray(item.properties[key])) {
+                        item.properties[key] = [...item.properties[key], value];
+                        // Remove potential duplicates
+                        item.properties[key] = Array.from(new Set(item.properties[key]));
+                    } else {
+                        item.properties[key] = [item.properties[key], value];
+                    }
+                } else {
+                    item.properties[key] = value;
+                }
+                const updatedCollection = await services.collectionService.update(
+                    uuid, {
+                        properties: {...JSON.parse(JSON.stringify(item.properties))}
+                    }
+                );
+                item.properties = updatedCollection.properties;
+                dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
+                return updatedCollection;
+            }
+            return;
+        } catch (e) {
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
+            return;
+        }
+    };
 
+export const navigateToProcess = (uuid: string) =>
+    async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
+        try {
+            await services.containerRequestService.get(uuid);
+            dispatch<any>(navigateTo(uuid));
+        } catch {
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
+        }
+    };
 
+export const deleteCollectionTag = (key: string, value: string) =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const item = getState().collectionPanel.item;
+        const uuid = item ? item.uuid : '';
+        try {
+            if (item) {
+                if (Array.isArray(item.properties[key])) {
+                    item.properties[key] = item.properties[key].filter((v: string) => v !== value);
+                    if (item.properties[key].length === 1) {
+                        item.properties[key] = item.properties[key][0];
+                    } else if (item.properties[key].length === 0) {
+                        delete item.properties[key];
+                    }
+                } else if (item.properties[key] === value) {
+                    delete item.properties[key];
+                }
+
+                const updatedCollection = await services.collectionService.update(
+                    uuid, {
+                        properties: {...item.properties}
+                    }
+                );
+                dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
+                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
+                return updatedCollection;
+            }
+            return;
+        } catch (e) {
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
+            return;
+        }
+    };