Merge branch 'master' into 13903-edit-collection-popup
[arvados-workbench2.git] / src / store / collections / creator / collection-creator-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 collectionCreateActions = unionize({
13     OPEN_COLLECTION_CREATOR: ofType<{ ownerUuid: string }>(),
14     CLOSE_COLLECTION_CREATOR: ofType<{}>(),
15     CREATE_COLLECTION: ofType<{}>(),
16     CREATE_COLLECTION_SUCCESS: ofType<{}>(),
17 }, {
18         tag: 'type',
19         value: 'payload'
20     });
21
22 export const createCollection = (collection: Partial<CollectionResource>) =>
23     (dispatch: Dispatch, getState: () => RootState) => {
24         const { ownerUuid } = getState().collections.creator;
25         const collectiontData = { ownerUuid, ...collection };
26         dispatch(collectionCreateActions.CREATE_COLLECTION(collectiontData));
27         return collectionService
28             .create(collectiontData)
29             .then(collection => dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection)));
30     };
31
32 export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;