Skip to content

Commit

Permalink
STRF-4803 - adding helper function to remove empty files for safari j…
Browse files Browse the repository at this point in the history
…query ajax bug
  • Loading branch information
tpietsch committed Apr 27, 2018
1 parent 0504129 commit 811437c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion assets/js/theme/common/product-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ export default class ProductDetails {
this.previewModal = modalFactory('#previewModal')[0];
}

/**
* https://stackoverflow.com/questions/49672992/ajax-request-fails-when-sending-formdata-including-empty-file-input-in-safari
* Safari browser with jquery 3.3.1 has an issue uploading empty file parameters. This function removes any empty files from the form params
* @param formData: FormData object
* @returns FormData object
*/
filterEmptyFilesFromForm(formData) {
try {
for (const [key, val] of formData) {
if (val instanceof File && !val.name && !val.size) {
formData.delete(key);
}
}
} catch (e) {
console.error(e);
}
return formData;
}

/**
* Since $productView can be dynamically inserted using render_with,
* We have to retrieve the respective elements
Expand Down Expand Up @@ -234,7 +253,7 @@ export default class ProductDetails {
this.$overlay.show();

// Add item to cart
utils.api.cart.itemAdd(new FormData(form), (err, response) => {
utils.api.cart.itemAdd(this.filterEmptyFilesFromForm(new FormData(form)), (err, response) => {
const errorMessage = err || response.data.error;

$addToCartBtn
Expand Down

0 comments on commit 811437c

Please sign in to comment.