mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-17 05:09:52 +03:00
eslint
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user