X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/59b24ea9a90ba60563316a5c2ad4c7ce8a8c423d..4a4da2249d80ed4116f534689684abff61ee2cd9:/src/store/file-uploader/file-uploader-reducer.ts diff --git a/src/store/file-uploader/file-uploader-reducer.ts b/src/store/file-uploader/file-uploader-reducer.ts index 625306f0..4218fbee 100644 --- a/src/store/file-uploader/file-uploader-reducer.ts +++ b/src/store/file-uploader/file-uploader-reducer.ts @@ -3,6 +3,7 @@ // SPDX-License-Identifier: AGPL-3.0 import { UploadFile, fileUploaderActions, FileUploaderAction } from "./file-uploader-actions"; +import { uniqBy } from 'lodash'; export type UploaderState = UploadFile[]; @@ -20,9 +21,46 @@ export const fileUploaderReducer = (state: UploaderState = initialState, action: prevTime: 0, currentTime: 0 })), + UPDATE_UPLOAD_FILES: files => { + const updateFiles = files.map((f, idx) => ({ + id: state.length + idx, + file: f, + prevLoaded: 0, + loaded: 0, + total: 0, + startTime: 0, + prevTime: 0, + currentTime: 0 + })); + const updatedState = state.concat(updateFiles); + const uniqUpdatedState = uniqBy(updatedState, 'file.name'); + + return uniqUpdatedState; + }, + DELETE_UPLOAD_FILE: file => { + const idToDelete: number = file.id; + const updatedState = state.filter(file => file.id !== idToDelete); + + return updatedState; + }, + CANCEL_FILES_UPLOAD: () => { + state.forEach((file) => { + let interval = setInterval(() => { + const key = Object.keys((window as any).cancelTokens).find(key => key.indexOf(file.file.name) > -1); + + if (key) { + clearInterval(interval); + (window as any).cancelTokens[key](); + delete (window as any).cancelTokens[key]; + } + }, 100); + }); + + return []; + }, START_UPLOAD: () => { const startTime = Date.now(); - return state.map(f => ({...f, startTime, prevTime: startTime})); + return state.map(f => ({ ...f, startTime, prevTime: startTime })); }, SET_UPLOAD_PROGRESS: ({ fileId, loaded, total, currentTime }) => state.map(f => f.id === fileId ? {