Skip to content
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

STRF-4803 - adding helper function to remove empty files from jquery … #1210

Merged
merged 1 commit into from
Apr 27, 2018
Merged

Conversation

tpietsch
Copy link
Contributor

https://stackoverflow.com/questions/49672992/ajax-request-fails-when-sending-formdata-including-empty-file-input-in-safari

our version of jquery has a problem making ajax calls with empty files in safari. Appears the solution is to remove empty files before sending unless anyone has other suggestions.

@bigcommerce/storefront-team @bigcommerce/platform-engineering

@bigbot
Copy link

bigbot commented Apr 24, 2018

Autotagging @bigcommerce/storefront-team @davidchin

for (const pair of form.entries()) {
const key = pair[0];
const val = pair[1];
if (typeof val === 'object' && val instanceof File) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍹 Are there any instances where val instanceof File passes but typeof val === 'object' does not? You can simplify this to just an instanceof check if not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guess not - was trying to be safe for the undef case but realized that "undefined instanceof File" is safe

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better actually to leave the object check and just check if the size/name fields are propertyOf -> and then check the values are not empty/undefined?

Copy link

@davidwrpayne davidwrpayne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

*/
filterEmptyFilesFromForm(form) {
try {
for (const pair of form.entries()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍹 could use destructuring (i.e. const [key, val] of form.entries())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have browser support requirements?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do but don't need to worry about them in this case. we've got webpack and babel to transpile down to the envs we specify in the browserlist file. :)

*/
filterEmptyFilesFromForm(form) {
try {
for (const [key, val] of form.entries()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you should be able to use const [key, val] of form

An object implementing FormData can directly be used in a for...of structure, instead of entries(): for (var p of myFormData) is equivalent to for (var p of myFormData.entries()). (https://developer.mozilla.org/en-US/docs/Web/API/FormData). maybe rename the param to formData to be explicit?

filterEmptyFilesFromForm(form) {
try {
for (const [key, val] of form.entries()) {
if (val instanceof File) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might as well pull the second conditional into the first:
if (val instanceof File && val.name === '' && val.size === 0) {

could also simplify to if (val instanceof File and !val.name && !val.size) {

Copy link
Contributor

@mjschock mjschock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - a couple nitpicks but nothing blocking

@tpietsch tpietsch merged commit 320b035 into bigcommerce:master Apr 27, 2018
Ubersmake added a commit that referenced this pull request May 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants