1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
13 import { RootState } from "store/store";
14 import { collectionPanelActions } from "store/collection-panel/collection-panel-action";
15 import { dialogActions } from "store/dialog/dialog-actions";
16 import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
17 import { ServiceRepository } from "services/services";
18 import { CollectionResource } from 'models/collection';
19 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
20 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
21 import { updateResources } from "../resources/resources-actions";
22 import { loadDetailsPanel } from "../details-panel/details-panel-action";
23 import { getResource } from "store/resources/resources";
24 import { CollectionProperties } from "./collection-create-actions";
26 export interface CollectionUpdateFormDialogData {
30 storageClassesDesired?: string[];
31 properties?: CollectionProperties;
34 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
35 export const COLLECTION_UPDATE_PROPERTIES_FORM_NAME = "collectionUpdatePropertiesFormName";
36 export const COLLECTION_UPDATE_FORM_SELECTOR = formValueSelector(COLLECTION_UPDATE_FORM_NAME);
38 export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) =>
39 (dispatch: Dispatch) => {
40 dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
41 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
44 export const updateCollection = (collection: CollectionUpdateFormDialogData) =>
45 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46 const uuid = collection.uuid || '';
47 dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
48 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
50 const cachedCollection = getResource<CollectionResource>(collection.uuid)(getState().resources);
51 services.collectionService.update(uuid, {
52 name: collection.name,
53 storageClassesDesired: collection.storageClassesDesired,
54 description: collection.description,
55 properties: collection.properties }
56 ).then(updatedCollection => {
57 updatedCollection = {...cachedCollection, ...updatedCollection};
58 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
59 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
60 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
61 dispatch(snackbarActions.OPEN_SNACKBAR({
62 message: "Collection has been successfully updated.",
64 kind: SnackbarKind.SUCCESS
66 dispatch<any>(updateResources([updatedCollection]));
67 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
69 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
70 const error = getCommonResourceServiceError(e);
71 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
72 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
74 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
75 dispatch(snackbarActions.OPEN_SNACKBAR({
76 message: e.errors.join(''),
78 kind: SnackbarKind.ERROR }));