Gracefully handle unsupported capture

This commit is contained in:
Ingvar Stepanyan
2021-07-23 19:19:40 +00:00
parent fcb67b1517
commit a8fa8cdf4c

View File

@@ -1,11 +1,12 @@
import { h, Component, Fragment } from 'preact'; import { h, Component, Fragment } from 'preact';
/** @extends Component<{ getFile: () => Promise<File> }, { inProgress: boolean }> */ /** @extends Component<{ getFile: () => Promise<File> }, { status: string }> */
export class CaptureButton extends Component { export class CaptureButton extends Component {
state = { inProgress: false }; state = { status: '📷' };
handleCapture = async () => { handleCapture = async () => {
this.setState({ inProgress: true }); this.setState({ status: '⌛' });
try {
let file = await this.props.getFile(); let file = await this.props.getFile();
let url = URL.createObjectURL(file); let url = URL.createObjectURL(file);
Object.assign(document.createElement('a'), { Object.assign(document.createElement('a'), {
@@ -13,7 +14,11 @@ export class CaptureButton extends Component {
href: url href: url
}).click(); }).click();
URL.revokeObjectURL(url); URL.revokeObjectURL(url);
this.setState({ inProgress: false }); this.setState({ status: '📷' });
} catch (err) {
console.error(err);
this.setState({ status: '❌' });
}
}; };
render() { render() {
@@ -23,10 +28,10 @@ export class CaptureButton extends Component {
h('input', { h('input', {
type: 'button', type: 'button',
id: 'capture', id: 'capture',
class: disabled: this.state.status !== '📷',
'pure-button' + (this.state.inProgress ? ' pure-button-active' : ''), class: 'pure-button',
onclick: this.handleCapture, onclick: this.handleCapture,
value: `${this.state.inProgress ? '⌛' : '📷'} Capture image` value: `${this.state.status} Capture image`
}) })
); );
} }