store - remove collection updater, rewrite collection edit dialog
[arvados.git] / src / store / collections / collection-updater-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 { RootState } from "../store";
7 import { ServiceRepository } from "~/services/services";
8 import { CollectionResource } from '~/models/collection';
9 import { initialize } from 'redux-form';
10 import { collectionPanelActions } from "../collection-panel/collection-panel-action";
11 import { updateDetails } from "~/store/details-panel/details-panel-action";
12 import { dialogActions } from "~/store/dialog/dialog-actions";
13 import { dataExplorerActions } from "~/store/data-explorer/data-explorer-action";
14 import { PROJECT_PANEL_ID } from "~/views/project-panel/project-panel";
15 import { snackbarActions } from "~/store/snackbar/snackbar-actions";
16
17 export interface CollectionUpdateFormDialogData {
18     name: string;
19     description: string;
20 }
21
22 export const COLLECTION_FORM_NAME = 'collectionEditDialog';
23
24 export const openUpdater = (resource: { name: string, uuid: string }) =>
25     (dispatch: Dispatch) => {
26         dispatch(initialize(COLLECTION_FORM_NAME, resource));
27         dispatch(dialogActions.OPEN_DIALOG({ id: COLLECTION_FORM_NAME, data: resource }));
28     };
29
30 export const editCollection = (data: { name: string, description: string }) =>
31     (dispatch: Dispatch) => {
32         return dispatch<any>(updateCollection(data)).then(() => {
33             dispatch(snackbarActions.OPEN_SNACKBAR({
34                 message: "Collection has been successfully updated.",
35                 hideDuration: 2000
36             }));
37             dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
38         });
39     };
40
41 export const updateCollection = (collection: Partial<CollectionResource>) =>
42     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43         const uuid = collection.uuid || '';
44         return services.collectionService
45             .update(uuid, collection)
46             .then(collection => {
47                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection as CollectionResource }));
48                 dispatch<any>(updateDetails(collection));
49                 dispatch(dialogActions.CLOSE_DIALOG({ id: COLLECTION_FORM_NAME }));
50             }
51         );
52     };