Format resource-type-filters
[arvados.git] / src / store / file-uploader / file-uploader-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { UploadFile, fileUploaderActions, FileUploaderAction } from "./file-uploader-actions";
6
7 export type UploaderState = UploadFile[];
8
9 const initialState: UploaderState = [];
10
11 export const fileUploaderReducer = (state: UploaderState = initialState, action: FileUploaderAction) => {
12     return fileUploaderActions.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 };