Add "Cancel Upload" button

This commit is contained in:
Stefano Brilli 2021-01-17 12:19:16 +01:00
parent 6290c34601
commit 9afb30f7fb
3 changed files with 33 additions and 4 deletions

View File

@ -1,6 +1,9 @@
import React from 'react'; import React, { useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { useShallowEqualSelector } from '../utils'; import { useShallowEqualSelector } from '../utils';
import { actions as uploadDialogActions } from '../redux/upload-dialog-feature';
import Dialog from '@material-ui/core/Dialog'; import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions'; import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent'; import DialogContent from '@material-ui/core/DialogContent';
@ -11,6 +14,7 @@ import LinearProgress from '@material-ui/core/LinearProgress';
import Box from '@material-ui/core/Box'; import Box from '@material-ui/core/Box';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import { TransitionProps } from '@material-ui/core/transitions'; import { TransitionProps } from '@material-ui/core/transitions';
import { Button } from '@material-ui/core';
const useStyles = makeStyles(theme => ({ const useStyles = makeStyles(theme => ({
progressPerc: { progressPerc: {
@ -33,9 +37,11 @@ const Transition = React.forwardRef(function Transition(
export const UploadDialog = (props: {}) => { export const UploadDialog = (props: {}) => {
const classes = useStyles(); const classes = useStyles();
const dispatch = useDispatch();
let { let {
visible, visible,
cancelled,
writtenProgress, writtenProgress,
encryptedProgress, encryptedProgress,
totalProgress, totalProgress,
@ -47,6 +53,10 @@ export const UploadDialog = (props: {}) => {
titleConverting, titleConverting,
} = useShallowEqualSelector(state => state.uploadDialog); } = useShallowEqualSelector(state => state.uploadDialog);
const handleCancelUpload = useCallback(() => {
dispatch(uploadDialogActions.setCancelUpload(true));
}, [dispatch]);
let progressValue = Math.floor((writtenProgress / totalProgress) * 100); let progressValue = Math.floor((writtenProgress / totalProgress) * 100);
let bufferValue = Math.floor((encryptedProgress / totalProgress) * 100); let bufferValue = Math.floor((encryptedProgress / totalProgress) * 100);
let convertedValue = Math.floor((trackConverting / trackTotal) * 100); let convertedValue = Math.floor((trackConverting / trackTotal) * 100);
@ -86,7 +96,11 @@ export const UploadDialog = (props: {}) => {
/> />
<Box className={classes.progressPerc}>{progressValue}%</Box> <Box className={classes.progressPerc}>{progressValue}%</Box>
</DialogContent> </DialogContent>
<DialogActions></DialogActions> <DialogActions>
<Button disabled={cancelled} onClick={handleCancelUpload}>
{cancelled ? `Stopping after current track...` : `Cancel Recording`}
</Button>
</DialogActions>
</Dialog> </Dialog>
); );
}; };

View File

@ -282,12 +282,16 @@ export function convertAndUpload(files: File[], format: string, titleSource: Tit
const wireformat = WireformatDict[format]; const wireformat = WireformatDict[format];
await netmdService?.stop(); await netmdService?.stop();
dispatch(uploadDialogActions.setVisible(true)); dispatch(batchActions([uploadDialogActions.setVisible(true), uploadDialogActions.setCancelUpload(false)]));
const updateProgressCallback = ({ written, encrypted, total }: { written: number; encrypted: number; total: number }) => { const updateProgressCallback = ({ written, encrypted, total }: { written: number; encrypted: number; total: number }) => {
dispatch(uploadDialogActions.setWriteProgress({ written, encrypted, total })); dispatch(uploadDialogActions.setWriteProgress({ written, encrypted, total }));
}; };
const hasUploadBeenCancelled = () => {
return getState().uploadDialog.cancelled;
};
let trackUpdate: { let trackUpdate: {
current: number; current: number;
converting: number; converting: number;
@ -310,7 +314,7 @@ export function convertAndUpload(files: File[], format: string, titleSource: Tit
let i = 0; let i = 0;
function convertNext() { function convertNext() {
if (i === files.length) { if (i === files.length || hasUploadBeenCancelled()) {
trackUpdate.converting = i; trackUpdate.converting = i;
trackUpdate.titleConverting = ``; trackUpdate.titleConverting = ``;
updateTrack(); updateTrack();
@ -357,6 +361,10 @@ export function convertAndUpload(files: File[], format: string, titleSource: Tit
let errorMessage = ``; let errorMessage = ``;
let i = 1; let i = 1;
for await (let item of conversionIterator(files)) { for await (let item of conversionIterator(files)) {
if (hasUploadBeenCancelled()) {
break;
}
const { file, data } = item; const { file, data } = item;
let title = file.name; let title = file.name;

View File

@ -3,6 +3,8 @@ import { enableBatching } from 'redux-batched-actions';
export interface LoadingDialogState { export interface LoadingDialogState {
visible: boolean; visible: boolean;
cancelled: boolean;
writtenProgress: number; writtenProgress: number;
encryptedProgress: number; encryptedProgress: number;
totalProgress: number; totalProgress: number;
@ -17,6 +19,8 @@ export interface LoadingDialogState {
const initialState: LoadingDialogState = { const initialState: LoadingDialogState = {
visible: false, visible: false,
cancelled: false,
// Current Track Upload // Current Track Upload
writtenProgress: 0, writtenProgress: 0,
encryptedProgress: 0, encryptedProgress: 0,
@ -42,6 +46,9 @@ export const slice = createSlice({
state.writtenProgress = action.payload.written; state.writtenProgress = action.payload.written;
state.totalProgress = action.payload.total; state.totalProgress = action.payload.total;
}, },
setCancelUpload: (state, action: PayloadAction<boolean>) => {
state.cancelled = action.payload;
},
setTrackProgress: ( setTrackProgress: (
state, state,
action: PayloadAction<{ total: number; current: number; converting: number; titleCurrent: string; titleConverting: string }> action: PayloadAction<{ total: number; current: number; converting: number; titleCurrent: string; titleConverting: string }>