Add typescript paths to top level folders
[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 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 const createCollection = (collection: Partial<CollectionResource>, files: File[]) =>
25     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
26         const { ownerUuid } = getState().collections.creator;
27         const collectiontData = { ownerUuid, ...collection };
28         dispatch(collectionCreateActions.CREATE_COLLECTION(collectiontData));
29         return services.collectionService
30             .create(collectiontData)
31             .then(collection => {
32                 dispatch(collectionUploaderActions.START_UPLOAD());
33                 services.collectionService.uploadFiles(collection.uuid, files,
34                     (fileId, loaded, total, currentTime) => {
35                         dispatch(collectionUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
36                     })
37                 .then(collection => {
38                     dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection));
39                     dispatch(reset('collectionCreateDialog'));
40                     dispatch(collectionUploaderActions.CLEAR_UPLOAD());
41                 });
42                 return collection;
43             });
44     };
45
46 export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;