Merge branch '14013-upload-collection-files-using-webdav' into 14014-create-collectio...
[arvados.git] / src / store / collections / uploader / collection-uploader-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CollectionUploaderAction, collectionUploaderActions, UploadFile } from "./collection-uploader-actions";
6
7 export type CollectionUploaderState = UploadFile[];
8
9 const initialState: CollectionUploaderState = [];
10
11 export const collectionUploaderReducer = (state: CollectionUploaderState = initialState, action: CollectionUploaderAction) => {
12     return collectionUploaderActions.match(action, {
13         SET_UPLOAD_FILES: files => files.map((f, idx) => ({
14             id: idx,
15             file: f,
16             prevLoaded: 0,
17             loaded: 0,
18             total: 0,
19             startTime: 0,
20             prevTime: 0,
21             currentTime: 0
22         })),
23         START_UPLOAD: () => {
24             const startTime = Date.now();
25             return state.map(f => ({...f, startTime, prevTime: startTime}));
26         },
27         SET_UPLOAD_PROGRESS: ({ fileId, loaded, total, currentTime }) =>
28             state.map(f => f.id === fileId ? {
29                 ...f,
30                 prevLoaded: f.loaded,
31                 loaded,
32                 total,
33                 prevTime: f.currentTime,
34                 currentTime
35             } : f),
36         CLEAR_UPLOAD: () => [],
37         default: () => state
38     });
39 };