5272baa8df612f364f63948406a55ce0fec56d86
[arvados-workbench2.git] / src / store / collections / updator / collection-updator-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 { collectionService } from '../../../services/services';
10 import { CollectionResource } from '../../../models/collection';
11
12 export const collectionUpdatorActions = unionize({
13     OPEN_COLLECTION_UPDATOR: ofType<{ ownerUuid: string }>(),
14     CLOSE_COLLECTION_UPDATOR: ofType<{}>(),
15     UPDATE_COLLECTION: ofType<{}>(),
16     UPDATE_COLLECTION_SUCCESS: ofType<{}>(),
17 }, {
18         tag: 'type',
19         value: 'payload'
20     });
21
22 export const updateCollection = (collection: Partial<CollectionResource>) =>
23     (dispatch: Dispatch, getState: () => RootState) => {
24         const { ownerUuid } = getState().collections.creator;
25         const collectiontData = { ownerUuid, ...collection };
26         dispatch(collectionUpdatorActions.UPDATE_COLLECTION(collectiontData));
27         return collectionService
28             // change for update
29             .create(collectiontData)
30             .then(collection => dispatch(collectionUpdatorActions.UPDATE_COLLECTION_SUCCESS(collection)));
31     };
32
33 export type CollectionUpdatorAction = UnionOf<typeof collectionUpdatorActions>;