From 395850868b51b8c9aa077560bd0b4a4d80478fe7 Mon Sep 17 00:00:00 2001 From: André Oliveira <37463445+SpecialAro@users.noreply.github.com> Date: Wed, 6 Jul 2022 23:57:25 +0100 Subject: Add file size info and error message while setting a custom image in the recipe settings screen (#437) Co-authored-by: Ricardo Cino Co-authored-by: Vijay Aravamudhan --- src/components/ui/ImageUpload.tsx | 64 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) (limited to 'src/components/ui') diff --git a/src/components/ui/ImageUpload.tsx b/src/components/ui/ImageUpload.tsx index 3a396f56c..7732928f8 100644 --- a/src/components/ui/ImageUpload.tsx +++ b/src/components/ui/ImageUpload.tsx @@ -4,6 +4,7 @@ import { Field } from 'mobx-react-form'; import classnames from 'classnames'; import Dropzone from 'react-dropzone'; import { mdiDelete, mdiFileImage } from '@mdi/js'; +import prettyBytes from 'pretty-bytes'; import { isWindows } from '../../environment'; import Icon from './icon'; @@ -13,20 +14,33 @@ type Props = { multiple: boolean; textDelete: string; textUpload: string; + textMaxFileSize: string; + textMaxFileSizeError: string; + maxSize?: number; + maxFiles?: number; + messages: any; }; // Should this file be converted into the coding style similar to './toggle/index.tsx'? class ImageUpload extends Component { static defaultProps = { multiple: false, + maxSize: Number.POSITIVE_INFINITY, + maxFiles: 0, }; state = { path: null, + errorState: false, }; - onDrop(acceptedFiles) { + errorMessage = { + message: '', + }; + + onDropAccepted(acceptedFiles) { const { field } = this.props; + this.setState({ errorState: false }); for (const file of acceptedFiles) { const imgPath = isWindows ? file.path.replace(/\\/g, '/') : file.path; @@ -40,14 +54,43 @@ class ImageUpload extends Component { field.set(''); } + onDropRejected(rejectedFiles): void { + for (const file of rejectedFiles) { + for (const error of file.errors) { + if (error.code === 'file-too-large') { + this.setState({ errorState: true }); + this.setState( + (this.errorMessage = { + message: this.props.textMaxFileSizeError, + }), + ); + } + } + } + } + render() { - const { field, className, multiple, textDelete, textUpload } = this.props; + const { + field, + className, + multiple, + textDelete, + textUpload, + textMaxFileSize, + maxSize, + maxFiles, + } = this.props; const cssClasses = classnames({ 'image-upload__dropzone': true, [`${className}`]: className, }); + const maxSizeParse: number = + maxSize === undefined || maxSize === Number.POSITIVE_INFINITY + ? 0 + : maxSize; + return (
); } -- cgit v1.2.3-54-g00ecf