X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/41df718825e8a136eb2378a07aa60d572bdb00b4..fc84a3f3932af503d3afd04a58af52270c8fc3b6:/src/store/collections/collection-update-actions.ts diff --git a/src/store/collections/collection-update-actions.ts b/src/store/collections/collection-update-actions.ts index 9c859234..04f42b8d 100644 --- a/src/store/collections/collection-update-actions.ts +++ b/src/store/collections/collection-update-actions.ts @@ -3,47 +3,69 @@ // SPDX-License-Identifier: AGPL-3.0 import { Dispatch } from "redux"; -import { 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 { ContextMenuResource } from "~/store/context-menu/context-menu-actions"; -import { progressIndicatorActions } from "~/store/progress-indicator/progress-indicator-actions"; +import { FormErrors, 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"; export interface CollectionUpdateFormDialogData { uuid: string; name: string; - description: string; + description?: string; + storageClassesDesired?: string[]; } export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName'; -export const openCollectionUpdateDialog = (resource: ContextMenuResource) => +export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) => (dispatch: Dispatch) => { dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource)); dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} })); }; -export const updateCollection = (collection: Partial) => - 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_UPDATE_FORM_NAME)); - try { - dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME)); - const updatedCollection = await services.collectionService.update(uuid, collection); + dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME)); + + const cachedCollection = getResource(collection.uuid)(getState().resources); + services.collectionService.update(uuid, { + name: collection.name, + storageClassesDesired: collection.storageClassesDesired, + description: collection.description } + ).then(updatedCollection => { + updatedCollection = {...cachedCollection, ...updatedCollection}; dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource })); dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME })); dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME)); - return updatedCollection; - } catch (e) { + dispatch(snackbarActions.OPEN_SNACKBAR({ + message: "Collection has been successfully updated.", + hideDuration: 2000, + kind: SnackbarKind.SUCCESS + })); + dispatch(updateResources([updatedCollection])); + dispatch(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_UPDATE_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 })); + } } - dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME)); - return; - } + ); };