15781: Fixes project tag add/delete error handling.
[arvados-workbench2.git] / src / store / details-panel / details-panel-action.ts
index 8083716e7adfc9f7138907fb40888b4b2fc46781..c5d472ade5a268de6d53ccaa39747b036438488e 100644 (file)
@@ -13,6 +13,7 @@ import { TagProperty } from '~/models/tag';
 import { startSubmit, stopSubmit } from 'redux-form';
 import { resourcesActions } from '~/store/resources/resources-actions';
 import {snackbarActions, SnackbarKind} from '~/store/snackbar/snackbar-actions';
+import { addProperty, deleteProperty } from '~/lib/resource-properties';
 
 export const SLIDE_TIMEOUT = 500;
 
@@ -36,20 +37,23 @@ export const openProjectPropertiesDialog = () =>
         dispatch<any>(dialogActions.OPEN_DIALOG({ id: PROJECT_PROPERTIES_DIALOG_NAME, data: { } }));
     };
 
-export const deleteProjectProperty = (key: string) =>
+export const deleteProjectProperty = (key: string, value: string) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const { detailsPanel, resources } = getState();
         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
+        if (!project) { return; }
+
+        const properties = Object.assign({}, project.properties);
+
         try {
-            if (project) {
-                delete project.properties[key];
-                const updatedProject = await services.projectService.update(project.uuid, { properties: project.properties });
-                dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
-                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
-            }
+            const updatedProject = await services.projectService.update(
+                project.uuid, {
+                    properties: deleteProperty(properties, key, value),
+                });
+            dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
         } catch (e) {
-            dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
-            throw new Error('Could not remove property from the project.');
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
         }
     };
 
@@ -57,19 +61,23 @@ export const createProjectProperty = (data: TagProperty) =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const { detailsPanel, resources } = getState();
         const project = getResource(detailsPanel.resourceUuid)(resources) as ProjectResource;
+        if (!project) { return; }
+
         dispatch(startSubmit(PROJECT_PROPERTIES_FORM_NAME));
         try {
-            if (project) {
-                project.properties[data.key] = data.value;
-                const updatedProject = await services.projectService.update(project.uuid, { properties: project.properties });
-                dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
-                dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
-                dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
-            }
-            return;
+            const key = data.keyID || data.key;
+            const value = data.valueID || data.value;
+            const properties = Object.assign({}, project.properties);
+            const updatedProject = await services.projectService.update(
+                project.uuid, {
+                    properties: addProperty(properties, key, value),
+                }
+            );
+            dispatch(resourcesActions.SET_RESOURCES([updatedProject]));
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Property has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
+            dispatch(stopSubmit(PROJECT_PROPERTIES_FORM_NAME));
         } catch (e) {
-            dispatch(dialogActions.CLOSE_DIALOG({ id: PROJECT_PROPERTIES_FORM_NAME }));
-            throw new Error('Could not add property to the project.');
+            dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
         }
     };
 export const toggleDetailsPanel = () => (dispatch: Dispatch) => {