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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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