Merge remote-tracking branch 'origin/main' into 18207-Workbench2-is-not-clearing...
[arvados-workbench2.git] / src / store / collections / collection-update-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import {
7     FormErrors,
8     formValueSelector,
9     initialize,
10     startSubmit,
11     stopSubmit
12 } from 'redux-form';
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
26 export interface CollectionUpdateFormDialogData {
27     uuid: string;
28     name: string;
29     description?: string;
30     storageClassesDesired?: string[];
31     properties?: CollectionProperties;
32 }
33
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);
37
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: {} }));
42     };
43
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));
49
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.",
63                 hideDuration: 2000,
64                 kind: SnackbarKind.SUCCESS
65             }));
66             dispatch<any>(updateResources([updatedCollection]));
67             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
68         }).catch (e => {
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));
73             } else {
74                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
75                 dispatch(snackbarActions.OPEN_SNACKBAR({
76                     message: e.errors.join(''),
77                     hideDuration: 2000,
78                     kind: SnackbarKind.ERROR }));
79                 }
80             }
81         );
82     };