Created
May 9, 2018 20:12
-
-
Save rgoytacaz/9795178bbee0d056da26623cc992568a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example input | |
var item = { id: 35, quantity: 3, seller: 1 } | |
var attachments = [ | |
{ | |
name: "Ingredientes Hamburguesa con Queso", | |
values: [ | |
{ | |
"Sin Carne 50gr": "true", | |
"Sin Cebolla": "false", | |
"Sin Mostaza": "false", | |
"Sin Pan Regular": "false", | |
"Sin Pepinillos": "false", | |
"Sin Queso Tajada": "false", | |
"Sin Salsa de Tomate": "false" | |
}, | |
{ | |
"Sin Carne 50gr": "false", | |
"Sin Cebolla": "true", | |
"Sin Mostaza": "false", | |
"Sin Pan Regular": "false", | |
"Sin Pepinillos": "false", | |
"Sin Queso Tajada": "false", | |
"Sin Salsa de Tomate": "false" | |
}, | |
{ | |
"Sin Carne 50gr": "false", | |
"Sin Cebolla": "false", | |
"Sin Mostaza": "true", | |
"Sin Pan Regular": "false", | |
"Sin Pepinillos": "false", | |
"Sin Queso Tajada": "false", | |
"Sin Salsa de Tomate": "false" | |
} | |
] | |
}, | |
{ | |
name: "Open to Extras", | |
values: [ | |
{ | |
"Open to Extras": "Extra Queijo" | |
}, | |
{ | |
"Open to Extras": "Extra Bacon" | |
}, | |
{ | |
"Open to Extras": "Extra Cebola" | |
}, | |
] | |
} | |
] | |
//Implementation | |
function addItemWithAttachment(item, attachments) { | |
var items = [item] | |
return vtexjs.checkout.addToCart(items) | |
.then((orderForm) => { | |
// Find the recently added item index in the orderForm | |
var indexToAddAttachment = orderForm.items.findIndex(function(orderItem) { | |
return ( | |
orderItem.id.toString() === item.id.toString() && | |
orderItem.attachments.length === 0 && | |
orderItem.bundleItems.length === 0 | |
) | |
}) | |
// Some error happened. The item was not added to the cart for some reason. | |
if (indexToAddAttachment === -1) { | |
return | |
} | |
var allRequests = attachments.reduce(function(acc, attachment, attachmentIndex) { | |
var requests = attachment.values.map(function(attachmentValue, attachmentValueIndex) { | |
return function(orderForm) { | |
if (!attachmentValue) return $.Deferred().resolve(orderForm) | |
if (attachmentIndex !== 0) { | |
indexToAddAttachment = findItemIndexWithPreviousAttachments( | |
orderForm.items, attachments, attachmentIndex, attachmentValueIndex | |
) | |
// Some error happened. The item was not added to the cart for some reason. | |
if (indexToAddAttachment === -1) { | |
return $.Deferred().resolve(orderForm) | |
} | |
} | |
return vtexjs.checkout.addItemAttachment( | |
indexToAddAttachment, attachment.name, attachmentValue, undefined, true | |
) | |
} | |
}) | |
return acc.concat(requests) | |
}, []) | |
return serializeRequests(allRequests) | |
}) | |
} | |
function findItemIndexWithPreviousAttachments( | |
items, attachments, attachmentIndex, attachmentValueIndex | |
) { | |
return items.findIndex(function(orderItem) { | |
return ( | |
orderItem.id.toString() === item.id.toString() && | |
orderItem.bundleItems.length === 0 && | |
orderItem.attachments.filter(function (a) { | |
return hasPreviousAttachment( | |
a, attachments, attachmentIndex, attachmentValueIndex | |
) | |
}).length === orderItem.attachments.length | |
) | |
}) | |
} | |
function hasPreviousAttachment( | |
attachment, attachments, attachmentIndex, attachmentValueIndex | |
) { | |
var previousAttachments = attachments.slice(0, attachmentIndex) | |
var attachmentToCompare = previousAttachments.find(function (a) { | |
return a.name === attachment.name | |
}) | |
if (!attachmentToCompare) { | |
return false | |
} | |
var sameAttachmentValue = shallowEqual( | |
attachment.content, | |
attachmentToCompare.values[attachmentValueIndex] | |
) | |
return sameAttachmentValue | |
} | |
/* Helper functions */ | |
function serializeRequests(requests) { | |
return requests.reduce(function (acc, request) { | |
if (!acc) { | |
return request() | |
} | |
return acc.then(request) | |
}, null) | |
} | |
function is(x, y) { | |
if (x === y) { | |
return x !== 0 || 1 / x === 1 / y; | |
} else { | |
return x !== x && y !== y; | |
} | |
} | |
function shallowEqual(objA, objB) { | |
if (is(objA, objB)) { | |
return true | |
} | |
if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) { | |
return false | |
} | |
var keysA = Object.keys(objA) | |
var keysB = Object.keys(objB) | |
if (keysA.length !== keysB.length) { | |
return false | |
} | |
// Test for A's keys different from B. | |
for (var i = 0; i < keysA.length; i++) { | |
if (!hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) { | |
return false | |
} | |
} | |
return true | |
} | |
// Execute code | |
addItemWithAttachment(item, attachments) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment