This commit is contained in:
proddy
2021-05-14 12:45:57 +02:00
parent 15df0c0552
commit fec5ff3132
108 changed files with 3508 additions and 2455 deletions

View File

@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import { Component } from 'react';
import { SectionContent } from '../components';
import { UPLOAD_FIRMWARE_ENDPOINT } from '../api';
@@ -12,8 +12,10 @@ interface UploadFirmwareControllerState {
progress?: ProgressEvent;
}
class UploadFirmwareController extends Component<WithSnackbarProps, UploadFirmwareControllerState> {
class UploadFirmwareController extends Component<
WithSnackbarProps,
UploadFirmwareControllerState
> {
state: UploadFirmwareControllerState = {
xhr: undefined,
progress: undefined
@@ -25,47 +27,67 @@ class UploadFirmwareController extends Component<WithSnackbarProps, UploadFirmwa
updateProgress = (progress: ProgressEvent) => {
this.setState({ progress });
}
};
uploadFile = (file: File) => {
if (this.state.xhr) {
return;
}
var xhr = new XMLHttpRequest();
const xhr = new XMLHttpRequest();
this.setState({ xhr });
redirectingAuthorizedUpload(xhr, UPLOAD_FIRMWARE_ENDPOINT, file, this.updateProgress).then(() => {
if (xhr.status !== 200) {
throw Error("Invalid status code: " + xhr.status);
}
this.props.enqueueSnackbar("Activating new firmware", { variant: 'success' });
this.setState({ xhr: undefined, progress: undefined });
}).catch((error: Error) => {
if (error.name === 'AbortError') {
this.props.enqueueSnackbar("Upload cancelled by user", { variant: 'warning' });
} else {
const errorMessage = error.name === 'UploadError' ? "Error during upload" : (error.message || "Unknown error");
this.props.enqueueSnackbar("Problem uploading: " + errorMessage, { variant: 'error' });
redirectingAuthorizedUpload(
xhr,
UPLOAD_FIRMWARE_ENDPOINT,
file,
this.updateProgress
)
.then(() => {
if (xhr.status !== 200) {
throw Error('Invalid status code: ' + xhr.status);
}
this.props.enqueueSnackbar('Activating new firmware', {
variant: 'success'
});
this.setState({ xhr: undefined, progress: undefined });
}
});
}
})
.catch((error: Error) => {
if (error.name === 'AbortError') {
this.props.enqueueSnackbar('Upload cancelled by user', {
variant: 'warning'
});
} else {
const errorMessage =
error.name === 'UploadError'
? 'Error during upload'
: error.message || 'Unknown error';
this.props.enqueueSnackbar('Problem uploading: ' + errorMessage, {
variant: 'error'
});
this.setState({ xhr: undefined, progress: undefined });
}
});
};
cancelUpload = () => {
if (this.state.xhr) {
this.state.xhr.abort();
this.setState({ xhr: undefined, progress: undefined });
}
}
};
render() {
const { xhr, progress } = this.state;
return (
<SectionContent title="Upload Firmware">
<UploadFirmwareForm onFileSelected={this.uploadFile} onCancel={this.cancelUpload} uploading={!!xhr} progress={progress} />
<UploadFirmwareForm
onFileSelected={this.uploadFile}
onCancel={this.cancelUpload}
uploading={!!xhr}
progress={progress}
/>
</SectionContent>
);
}
}
export default withSnackbar(UploadFirmwareController);