1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { UploadFile, fileUploaderActions, FileUploaderAction } from "./file-uploader-actions";
6 import { uniqBy } from 'lodash';
8 export type UploaderState = UploadFile[];
10 const initialState: UploaderState = [];
12 export const fileUploaderReducer = (state: UploaderState = initialState, action: FileUploaderAction) => {
13 return fileUploaderActions.match(action, {
14 SET_UPLOAD_FILES: files => files.map((f, idx) => ({
24 UPDATE_UPLOAD_FILES: files => {
25 const updateFiles = files.map((f, idx) => ({
26 id: state.length + idx,
35 const updatedState = state.concat(updateFiles);
36 const uniqUpdatedState = uniqBy(updatedState, 'file.name');
38 return uniqUpdatedState;
40 DELETE_UPLOAD_FILE: file => {
41 const idToDelete: number = file.id;
42 const updatedState = state.filter(file => file.id !== idToDelete);
44 const key: string | undefined = Object.keys((window as any).cancelTokens)
45 .find(key => key.indexOf(file.file.name) > -1);
48 (window as any).cancelTokens[key]();
49 delete (window as any).cancelTokens[key];
54 CANCEL_FILES_UPLOAD: () => {
55 Object.keys((window as any).cancelTokens)
57 (window as any).cancelTokens[key]();
58 delete (window as any).cancelTokens[key];
64 const startTime = Date.now();
65 return state.map(f => ({ ...f, startTime, prevTime: startTime }));
67 SET_UPLOAD_PROGRESS: ({ fileId, loaded, total, currentTime }) =>
68 state.map(f => f.id === fileId ? {
73 prevTime: f.currentTime,
76 CLEAR_UPLOAD: () => [],