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 25, 2018
1 parent 0504129 commit c90e4df
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 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,29 @@ 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 form: FormData object
* @returns FormData object
*/
filterEmptyFilesFromForm(form) {
try {
for (const pair of form.entries()) {
const key = pair[0];
const val = pair[1];
if (val instanceof File) {
if (val.name === '' && val.size === 0) {
form.delete(key);
}
}
}
} catch (e) {
console.error(e);
}
return form;
}

/**
* Since $productView can be dynamically inserted using render_with,
* We have to retrieve the respective elements
Expand Down Expand Up @@ -234,7 +257,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 c90e4df

Please sign in to comment.