Merge branch '21578-mount-debug'
[arvados.git] / services / workbench2 / 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 import { loadSidePanelTreeProjects, SidePanelTreeCategory } from "store/side-panel-tree/side-panel-tree-actions";
26
27 export interface CollectionUpdateFormDialogData {
28     uuid: string;
29     name: string;
30     description?: string;
31     storageClassesDesired?: string[];
32     properties?: CollectionProperties;
33 }
34
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);
38
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: {} }));
43     };
44
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));
50
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.",
64                 hideDuration: 2000,
65                 kind: SnackbarKind.SUCCESS
66             }));
67             dispatch<any>(updateResources([updatedCollection]));
68             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
69             dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.FAVORITES))
70         }).catch (e => {
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));
75             } else {
76                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
77                 const errMsg = e.errors
78                     ? e.errors.join('')
79                     : 'There was an error while updating the collection';
80                 dispatch(snackbarActions.OPEN_SNACKBAR({
81                     message: errMsg,
82                     hideDuration: 2000,
83                     kind: SnackbarKind.ERROR }));
84                 }
85             }
86         );
87     };