merge master
[arvados.git] / src / store / collections / updater / collection-updater-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { default as unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7
8 import { RootState } from "../../store";
9 import { ServiceRepository } from "~/services/services";
10 import { CollectionResource } from '~/models/collection';
11 import { initialize } from 'redux-form';
12 import { collectionPanelActions } from "../../collection-panel/collection-panel-action";
13 import { ContextMenuResource } from "../../context-menu/context-menu-reducer";
14 import { updateDetails } from "~/store/details-panel/details-panel-action";
15
16 export const collectionUpdaterActions = unionize({
17     OPEN_COLLECTION_UPDATER: ofType<{ uuid: string }>(),
18     CLOSE_COLLECTION_UPDATER: ofType<{}>(),
19     UPDATE_COLLECTION_SUCCESS: ofType<{}>(),
20 }, {
21     tag: 'type',
22     value: 'payload'
23 });
24
25
26 export const COLLECTION_FORM_NAME = 'collectionEditDialog';
27
28 export const openUpdater = (item: ContextMenuResource) =>
29     (dispatch: Dispatch, getState: () => RootState) => {
30         if (item) {
31             dispatch(collectionUpdaterActions.OPEN_COLLECTION_UPDATER({ uuid: item.uuid }));
32             dispatch(initialize(COLLECTION_FORM_NAME, { name: item.name, description: item.description }));
33         }
34     };
35
36 export const updateCollection = (collection: Partial<CollectionResource>) =>
37     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
38         const { uuid } = getState().collections.updater;
39         return services.collectionService
40             .update(uuid, collection)
41             .then(collection => {
42                     dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection as CollectionResource }));
43                     dispatch(collectionUpdaterActions.UPDATE_COLLECTION_SUCCESS());
44                     dispatch<any>(updateDetails(collection));
45                 }
46             );
47     };
48
49 export type CollectionUpdaterAction = UnionOf<typeof collectionUpdaterActions>;