-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add max upload size to image uploader #293
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! I notice some good attention to detail here, really appreciate that @PraggyaJ !
Just a few small comments, and then as you can see you've accidentally made the whole admin components directory executable it seems haha, so just undo that ;).
static/admin.css
Outdated
@@ -3,6 +3,10 @@ | |||
color: black; | |||
} | |||
|
|||
body{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this an accident?
@@ -138,6 +148,14 @@ export default class ImageUploader extends BaseComponent { | |||
this.addImagePreviewUrl(file); | |||
}); | |||
|
|||
if (tooLargeFileNames.length !== 0) { | |||
let names = '\n'; | |||
tooLargeFileNames.forEach((file) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, good job! But there's a smoother way we can do it, like this:
const alertMessage = tooLargeFileNames.reduce((currentMessage, filename) => {
return `${currentMessage}\n${file.name} (${sizeCalculation) KB)`;
}, 'The following files exceeded the maximum upload size:\n');
See these docs for an explanation of the reduce function
tooLargeFileNames.forEach((file) => { | ||
names = names.concat(` ${file.name} (${(file.size / 1024).toFixed(2)} KB)\n`); | ||
}); | ||
alert(`The following exceeded the maximum upload size: ${names}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just make this the following files...
(adding in "files").
Also, it would generally be a lot more natural to add the newline in here instead of as the first part of the string above, so just making it size:\n${names}
), but with my above approach you can do it all in one and just do alert(alertMessage)
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of alert
, let's use a Material UI dialog box. This falls in line with the rest of the Admin Interface and is just a little less jarring than an alert
message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah you're right, let's keep things pretty! I'll try not to fall into my old habits ;)
tooLargeFileNames.forEach((file) => { | ||
names = names.concat(` ${file.name} (${(file.size / 1024).toFixed(2)} KB)\n`); | ||
}); | ||
alert(`The following exceeded the maximum upload size: ${names}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of alert
, let's use a Material UI dialog box. This falls in line with the rest of the Admin Interface and is just a little less jarring than an alert
message.
}); | ||
alert(`The following exceeded the maximum upload size: ${names}`); | ||
} | ||
|
||
// Reset to "No Files Chosen" in the input element instead of saying "10 files" or so | ||
e.target.parentNode.parentNode.parentNode.parentNode.parentNode.reset(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There must be a more elegant way to do this. What are all the .parentNode
s referring to here??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly, it was just really hard getting a ref to it or something like that, it is without a doubt ugly though
The image uploader only accepts images with a size less than 300 KB and alerts the user which files were too big.