Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[arvados-workbench2.git] / src / store / collections / collection-update-actions.ts
index 13df864df2dd0c5b06bd5d49f71b523ad5f10db0..82418d27abe75b8bbeef49b07132151ef52187bb 100644 (file)
@@ -3,56 +3,80 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Dispatch } from "redux";
-import { getCommonResourceServiceError, CommonResourceServiceError } from "~/common/api/common-resource-service";
-import { ServiceRepository } from "~/services/services";
-import { CollectionResource } from '~/models/collection';
-import { RootState } from "~/store/store";
-import { initialize, startSubmit, stopSubmit } from 'redux-form';
-import { collectionPanelActions } from "~/store/collection-panel/collection-panel-action";
-import { updateDetails } from "~/store/details-panel/details-panel-action";
-import { dialogActions } from "~/store/dialog/dialog-actions";
-import { dataExplorerActions } from "~/store/data-explorer/data-explorer-action";
-import { snackbarActions } from "~/store/snackbar/snackbar-actions";
-import { ContextMenuResource } from '~/store/context-menu/context-menu-reducer';
-import { PROJECT_PANEL_ID } from "~/views/project-panel/project-panel";
+import {
+    FormErrors,
+    formValueSelector,
+    initialize,
+    startSubmit,
+    stopSubmit
+} from 'redux-form';
+import { RootState } from "store/store";
+import { collectionPanelActions } from "store/collection-panel/collection-panel-action";
+import { dialogActions } from "store/dialog/dialog-actions";
+import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
+import { ServiceRepository } from "services/services";
+import { CollectionResource } from 'models/collection';
+import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
+import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
+import { updateResources } from "../resources/resources-actions";
+import { loadDetailsPanel } from "../details-panel/details-panel-action";
+import { getResource } from "store/resources/resources";
+import { CollectionProperties } from "./collection-create-actions";
 
 export interface CollectionUpdateFormDialogData {
     uuid: string;
     name: string;
-    description: string;
+    description?: string;
+    storageClassesDesired?: string[];
+    properties?: CollectionProperties;
 }
 
-export const COLLECTION_FORM_NAME = 'collectionEditDialog';
+export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
+export const COLLECTION_UPDATE_PROPERTIES_FORM_NAME = "collectionUpdatePropertiesFormName";
+export const COLLECTION_UPDATE_FORM_SELECTOR = formValueSelector(COLLECTION_UPDATE_FORM_NAME);
 
-export const openUpdater = (resource: ContextMenuResource) =>
+export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) =>
     (dispatch: Dispatch) => {
-        dispatch(initialize(COLLECTION_FORM_NAME, resource));
-        dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_FORM_NAME, data: {} }));
+        dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
+        dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
     };
 
-export const editCollection = (data: CollectionUpdateFormDialogData) =>
-    async (dispatch: Dispatch) => {
-        await dispatch<any>(updateCollection(data));
-        dispatch(snackbarActions.OPEN_SNACKBAR({
-            message: "Collection has been successfully updated.",
-            hideDuration: 2000
-        }));
-    };
-
-export const updateCollection = (collection: Partial<CollectionResource>) =>
-    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+export const updateCollection = (collection: CollectionUpdateFormDialogData) =>
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
         const uuid = collection.uuid || '';
-        dispatch(startSubmit(COLLECTION_FORM_NAME));
-        try {
-            const updatedCollection = await services.collectionService.update(uuid, collection);
+        dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
+        dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
+
+        const cachedCollection = getResource<CollectionResource>(collection.uuid)(getState().resources);
+        services.collectionService.update(uuid, {
+            name: collection.name,
+            storageClassesDesired: collection.storageClassesDesired,
+            description: collection.description,
+            properties: collection.properties }
+        ).then(updatedCollection => {
+            updatedCollection = {...cachedCollection, ...updatedCollection};
             dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
-            dispatch<any>(updateDetails(updatedCollection));
-            dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
-            dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_FORM_NAME }));
-        } catch(e) {
+            dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
+            dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
+            dispatch(snackbarActions.OPEN_SNACKBAR({
+                message: "Collection has been successfully updated.",
+                hideDuration: 2000,
+                kind: SnackbarKind.SUCCESS
+            }));
+            dispatch<any>(updateResources([updatedCollection]));
+            dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
+        }).catch (e => {
+            dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
             const error = getCommonResourceServiceError(e);
-            if (error === CommonResourceServiceError.UNIQUE_VIOLATION) {
-                dispatch(stopSubmit(COLLECTION_FORM_NAME, { name: 'Collection with the same name already exists.' }));
+            if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
+                dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
+            } else {
+                dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
+                dispatch(snackbarActions.OPEN_SNACKBAR({
+                    message: e.errors.join(''),
+                    hideDuration: 2000,
+                    kind: SnackbarKind.ERROR }));
+                }
             }
-        }
-    };
\ No newline at end of file
+        );
+    };