Fix correct bytes not being sent, fix showing upload progress and speed
[arvados-workbench2.git] / src / store / collections / creator / collection-creator-action.ts
index d21d622dba1fd6f398d1dd54c3b633797c31b71d..023e5be6aec44677f189c023b4eb58e27231b4e3 100644 (file)
@@ -6,8 +6,9 @@ import { default as unionize, ofType, UnionOf } from "unionize";
 import { Dispatch } from "redux";
 
 import { RootState } from "../../store";
-import { collectionCreationService } from '../../../services/services';
 import { CollectionResource } from '../../../models/collection';
+import { ServiceRepository } from "../../../services/services";
+import { collectionUploaderActions } from "../uploader/collection-uploader-actions";
 
 export const collectionCreateActions = unionize({
     OPEN_COLLECTION_CREATOR: ofType<{ ownerUuid: string }>(),
@@ -15,18 +16,28 @@ export const collectionCreateActions = unionize({
     CREATE_COLLECTION: ofType<{}>(),
     CREATE_COLLECTION_SUCCESS: ofType<{}>(),
 }, {
-        tag: 'type',
-        value: 'payload'
-    });
+    tag: 'type',
+    value: 'payload'
+});
 
-export const createCollection = (collection: Partial<CollectionResource>) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
-        const { ownerUuid } = getState().collectionCreation.creator;
+export const createCollection = (collection: Partial<CollectionResource>, files: File[]) =>
+    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        const { ownerUuid } = getState().collections.creator;
         const collectiontData = { ownerUuid, ...collection };
         dispatch(collectionCreateActions.CREATE_COLLECTION(collectiontData));
-        return collectionCreationService
+        return services.collectionService
             .create(collectiontData)
-            .then(collection => dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection)));
+            .then(collection => {
+                dispatch(collectionUploaderActions.START_UPLOAD());
+                services.collectionService.uploadFiles(collection.uuid, files,
+                    (fileId, loaded, total, currentTime) => {
+                        dispatch(collectionUploaderActions.SET_UPLOAD_PROGRESS({ fileId, loaded, total, currentTime }));
+                    })
+                .then(collection => {
+                    dispatch(collectionCreateActions.CREATE_COLLECTION_SUCCESS(collection));
+                });
+                return collection;
+            });
     };
 
-export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;
\ No newline at end of file
+export type CollectionCreateAction = UnionOf<typeof collectionCreateActions>;