0096bc4828c1642926c3f00c0a4fa3b604a22935
[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     change,
8     FormErrors,
9     formValueSelector,
10     initialize,
11     startSubmit,
12     stopSubmit
13 } from 'redux-form';
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";
28
29 export interface CollectionUpdateFormDialogData {
30     uuid: string;
31     name: string;
32     description?: string;
33     storageClassesDesired?: string[];
34     properties?: CollectionProperties;
35 }
36
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);
40
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: {} }));
45     };
46
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));
52
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.",
66                 hideDuration: 2000,
67                 kind: SnackbarKind.SUCCESS
68             }));
69             dispatch<any>(updateResources([updatedCollection]));
70             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
71         }).catch (e => {
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));
76             } else {
77                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_UPDATE_FORM_NAME }));
78                 dispatch(snackbarActions.OPEN_SNACKBAR({
79                     message: e.errors.join(''),
80                     hideDuration: 2000,
81                     kind: SnackbarKind.ERROR }));
82                 }
83             }
84         );
85     };
86
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;
92         dispatch(change(
93             COLLECTION_UPDATE_FORM_NAME,
94             'properties',
95             addProperty(properties, key, value)));
96     };
97
98 export const removePropertyFromUpdateCollectionForm = (key: string, value: string) =>
99     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
100         const properties = { ...COLLECTION_UPDATE_FORM_SELECTOR(getState(), 'properties') };
101         dispatch(change(
102             COLLECTION_UPDATE_FORM_NAME,
103             'properties',
104             deleteProperty(properties, key, value)));
105     };