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";
25 import { loadSidePanelTreeProjects, SidePanelTreeCategory } from "store/side-panel-tree/side-panel-tree-actions";
27 export interface CollectionUpdateFormDialogData {
31 storageClassesDesired?: string[];
32 properties?: CollectionProperties;
35 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
36 export const COLLECTION_UPDATE_PROPERTIES_FORM_NAME = "collectionUpdatePropertiesFormName";
37 export const COLLECTION_UPDATE_FORM_SELECTOR = formValueSelector(COLLECTION_UPDATE_FORM_NAME);
39 export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) =>
40 (dispatch: Dispatch) => {
41 dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
42 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
45 export const updateCollection = (collection: CollectionUpdateFormDialogData) =>
46 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
47 const uuid = collection.uuid || '';
48 dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
49 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
51 const cachedCollection = getResource<CollectionResource>(collection.uuid)(getState().resources);
52 services.collectionService.update(uuid, {
53 name: collection.name,
54 storageClassesDesired: collection.storageClassesDesired,
55 description: collection.description,
56 properties: collection.properties }, false
57 ).then(updatedCollection => {
58 updatedCollection = {...cachedCollection, ...updatedCollection};
59 dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
60 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
61 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
62 dispatch(snackbarActions.OPEN_SNACKBAR({
63 message: "Collection has been successfully updated.",
65 kind: SnackbarKind.SUCCESS
67 dispatch<any>(updateResources([updatedCollection]));
68 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
69 dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.FAVORITES))
71 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
72 const error = getCommonResourceServiceError(e);
73 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
74 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
76 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
77 const errMsg = e.errors
79 : 'There was an error while updating the collection';
80 dispatch(snackbarActions.OPEN_SNACKBAR({
83 kind: SnackbarKind.ERROR }));