Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / resources / resources-actions.ts
index 8e6d16f928aed6d54e2f807861a81c6bfb732c1d..aff338f0b48540a5c666852aa40dcad565cc9a8f 100644 (file)
@@ -12,9 +12,13 @@ import { addProperty, deleteProperty } from 'lib/resource-properties';
 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
 import { getResource } from './resources';
 import { TagProperty } from 'models/tag';
+import { change, formValueSelector } from 'redux-form';
+import { ResourcePropertiesFormData } from 'views-components/resource-properties-form/resource-properties-form';
+
+export type ResourceWithDescription = Resource & { description?: string }
 
 export const resourcesActions = unionize({
-    SET_RESOURCES: ofType<Resource[]>(),
+    SET_RESOURCES: ofType<ResourceWithDescription[] >(),
     DELETE_RESOURCES: ofType<string[]>()
 });
 
@@ -93,3 +97,23 @@ export const createResourceProperty = (data: TagProperty) =>
             dispatch(snackbarActions.OPEN_SNACKBAR({ message: errorMsg, hideDuration: 2000, kind: SnackbarKind.ERROR }));
         }
     };
+
+export const addPropertyToResourceForm = (data: ResourcePropertiesFormData, formName: string) =>
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const properties = { ...formValueSelector(formName)(getState(), 'properties') };
+        const key = data.keyID || data.key;
+        const value =  data.valueID || data.value;
+        dispatch(change(
+            formName,
+            'properties',
+            addProperty(properties, key, value)));
+    };
+
+export const removePropertyFromResourceForm = (key: string, value: string, formName: string) =>
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const properties = { ...formValueSelector(formName)(getState(), 'properties') };
+        dispatch(change(
+            formName,
+            'properties',
+            deleteProperty(properties, key, value)));
+    };