Skip to content

Commit

Permalink
Update: allowed max size of image preview
Browse files Browse the repository at this point in the history
  • Loading branch information
amasin76 committed Oct 1, 2023
1 parent 50f23f3 commit ad9cf1f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/shared/components/ImagePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const ImagePreview: React.FC<ImagePreviewProps> = ({ src, isSelected, onNewImage

const handleFileChange = (e: ChangeEvent<HTMLInputElement>) => {
const file = e.target.files ? e.target.files[0] : null;
if (file) {
const fileSize = file?.size / 1024 / 1024;
const SIZE_LIMIT = 3;

if (file && fileSize > SIZE_LIMIT) {
alert(`Maximum size is ${SIZE_LIMIT}MB`);
} else if (file) {
const reader = new FileReader();
reader.onload = (e) => {
const contents = e.target.result as string;
Expand Down Expand Up @@ -44,7 +49,13 @@ const ImagePreview: React.FC<ImagePreviewProps> = ({ src, isSelected, onNewImage
<BsPlusCircleFill />
New Image
</label>
<input type="file" id="upload-img" onChange={handleFileChange} className="hidden" />
<input
type="file"
accept="image/png, image/jpeg, image/jpg, image/gif, image/avif, image/webp"
id="upload-img"
onChange={handleFileChange}
className="hidden"
/>
</>
)}
{src && isHovered && (
Expand Down

0 comments on commit ad9cf1f

Please sign in to comment.