023e5be6aec44677f189c023b4eb58e27231b4e3
[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 { collectionUploaderActions } from "../uploader/collection-uploader-actions";
12
13 export const collectionCreateActions = unionize({
14     OPEN_COLLECTION_CREATOR: ofType<{ ownerUuid: string }>(),
15     CLOSE_COLLECTION_CREATOR: ofType<{}>(),
16     CREATE_COLLECTION: ofType<{}>(),
17     CREATE_COLLECTION_SUCCESS: ofType<{}>(),
18 }, {
19     tag: 'type',
20     value: 'payload'
21 });
22
23 export const createCollection = (collection: Partial<CollectionResource>, files: File[]) =>
24     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25         const { ownerUuid } = getState().collections.creator;
26         const collectiontData = { ownerUuid, ...collection };
27         dispatch(collectionCreateActions.CREATE_COLLECTION(collectiontData));
28         return services.collectionService
29             .create(collectiontData)
30             .then(collection => {
31                 dispatch(collectionUploaderActions.START_UPLOAD());
32                 services.collectionService.uploadFiles(collection.uuid, files,
33                     (fileId, loaded, total, currentTime) => {
34                         dispatch(collectionUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
35                     })
36                 .then(collection => {
37                     dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection));
38                 });
39                 return collection;
40             });
41     };
42
43 export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;