store - change collection update action name, create collection form fields
[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 { CollectionResource } from '~/models/collection';
10 import { ServiceRepository } from "~/services/services";
11 import { uploadCollectionFiles } from '../uploader/collection-uploader-actions';
12 import { reset } from "redux-form";
13
14 export const collectionCreateActions = unionize({
15     OPEN_COLLECTION_CREATOR: ofType<{ ownerUuid: string }>(),
16     CLOSE_COLLECTION_CREATOR: ofType<{}>(),
17     CREATE_COLLECTION: ofType<{}>(),
18     CREATE_COLLECTION_SUCCESS: ofType<{}>(),
19 }, {
20         tag: 'type',
21         value: 'payload'
22     });
23
24 export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;
25
26 export const createCollection = (collection: Partial<CollectionResource>, files: File[]) =>
27     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28         const { ownerUuid } = getState().collections.creator;
29         const collectiontData = { ownerUuid, ...collection };
30         dispatch(collectionCreateActions.CREATE_COLLECTION(collectiontData));
31         const newCollection = await services.collectionService.create(collectiontData);
32         await dispatch<any>(uploadCollectionFiles(newCollection.uuid));
33         dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection));
34         dispatch(reset('collectionCreateDialog'));
35         return newCollection;
36     };
37