1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
14 import { RootState } from "store/store";
15 import { collectionPanelActions } from "store/collection-panel/collection-panel-action";
16 import { dialogActions } from "store/dialog/dialog-actions";
17 import { getCommonResourceServiceError, CommonResourceServiceError } from "services/common-service/common-resource-service";
18 import { ServiceRepository } from "services/services";
19 import { CollectionResource } from 'models/collection';
20 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
21 import { snackbarActions, SnackbarKind } from "../snackbar/snackbar-actions";
22 import { updateResources } from "../resources/resources-actions";
23 import { loadDetailsPanel } from "../details-panel/details-panel-action";
24 import { getResource } from "store/resources/resources";
25 import { CollectionProperties } from "./collection-create-actions";
26 import { ResourcePropertiesFormData } from "views-components/resource-properties-form/resource-properties-form";
27 import { addProperty, deleteProperty } from "lib/resource-properties";
29 export interface CollectionUpdateFormDialogData {
33 storageClassesDesired?: string[];
34 properties?: CollectionProperties;
37 export const COLLECTION_UPDATE_FORM_NAME = 'collectionUpdateFormName';
38 export const COLLECTION_UPDATE_PROPERTIES_FORM_NAME = "collectionCreatePropertiesFormName";
39 export const COLLECTION_UPDATE_FORM_SELECTOR = formValueSelector(COLLECTION_UPDATE_FORM_NAME);
41 export const openCollectionUpdateDialog = (resource: CollectionUpdateFormDialogData) =>
42 (dispatch: Dispatch) => {
43 dispatch(initialize(COLLECTION_UPDATE_FORM_NAME, resource));
44 dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME, data: {} }));
47 export const updateCollection = (collection: CollectionUpdateFormDialogData) =>
48 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49 const uuid = collection.uuid || '';
50 dispatch(startSubmit(COLLECTION_UPDATE_FORM_NAME));
51 dispatch(progressIndicatorActions.START_WORKING(COLLECTION_UPDATE_FORM_NAME));
53 const cachedCollection = getResource<CollectionResource>(collection.uuid)(getState().resources);
54 services.collectionService.update(uuid, {
55 name: collection.name,
56 storageClassesDesired: collection.storageClassesDesired,
57 description: collection.description,
58 properties: collection.properties }
59 ).then(updatedCollection => {
60 updatedCollection = {...cachedCollection, ...updatedCollection};
61 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: updatedCollection as CollectionResource }));
62 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
63 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
64 dispatch(snackbarActions.OPEN_SNACKBAR({
65 message: "Collection has been successfully updated.",
67 kind: SnackbarKind.SUCCESS
69 dispatch<any>(updateResources([updatedCollection]));
70 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
72 dispatch(progressIndicatorActions.STOP_WORKING(COLLECTION_UPDATE_FORM_NAME));
73 const error = getCommonResourceServiceError(e);
74 if (error === CommonResourceServiceError.UNIQUE_NAME_VIOLATION) {
75 dispatch(stopSubmit(COLLECTION_UPDATE_FORM_NAME, { name: 'Collection with the same name already exists.' } as FormErrors));
77 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
78 dispatch(snackbarActions.OPEN_SNACKBAR({
79 message: e.errors.join(''),
81 kind: SnackbarKind.ERROR }));
87 export const addPropertyToUpdateCollectionForm = (data: ResourcePropertiesFormData) =>
88 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
89 const properties = { ...COLLECTION_UPDATE_FORM_SELECTOR(getState(), 'properties') };
90 const key = data.keyID || data.key;
91 const value = data.valueID || data.value;
93 COLLECTION_UPDATE_FORM_NAME,
95 addProperty(properties, key, value)));
98 export const removePropertyFromUpdateCollectionForm = (key: string, value: string) =>
99 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100 const properties = { ...COLLECTION_UPDATE_FORM_SELECTOR(getState(), 'properties') };
102 COLLECTION_UPDATE_FORM_NAME,
104 deleteProperty(properties, key, value)));