Socket
Socket
Sign inDemoInstall

@vtex/order-items

Package Overview
Dependencies
7
Maintainers
74
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1-0 to 0.0.1-1

types/__fixtures__/mockOrderForm.d.ts

744

dist/index.esm.js
/*!
* @vtex/order-items v0.0.1-0
* @vtex/order-items v0.0.1-1
* (c) VTEX

@@ -7,5 +7,743 @@ * Released under the MIT License.

var HelloWorld = 'hello world';
import React, { createContext, useContext, useCallback, useRef, useEffect, useMemo } from 'react';
import * as uuid from 'uuid';
import { useOrderQueue, useQueueStatus, TASK_CANCELLED_CODE } from '@vtex/order-manager';
export { HelloWorld };
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
var DEFAULT_LOCAL_ORDER_QUEUE = {
queue: [],
};
var getLocalOrderQueue = function () {
var _a;
var queue = null;
try {
queue = JSON.parse((_a = localStorage.getItem('orderQueue')) !== null && _a !== void 0 ? _a : 'null');
}
catch (_b) {
// ignore
}
if (!queue) {
localStorage.setItem('orderQueue', JSON.stringify(DEFAULT_LOCAL_ORDER_QUEUE));
}
return queue !== null && queue !== void 0 ? queue : DEFAULT_LOCAL_ORDER_QUEUE;
};
var saveLocalOrderQueue = function (orderQueue) {
localStorage.setItem('orderQueue', JSON.stringify(orderQueue));
};
var pushLocalOrderQueue = function (task) {
var orderQueue = getLocalOrderQueue();
var index = orderQueue.queue.push(task);
saveLocalOrderQueue(orderQueue);
return index;
};
var popLocalOrderQueue = function (index) {
if (index === void 0) { index = 0; }
var orderQueue = getLocalOrderQueue();
var task = orderQueue.queue[index];
if (!task) {
return undefined;
}
orderQueue.queue.splice(index, 1);
saveLocalOrderQueue(orderQueue);
return task;
};
var updateLocalQueueItemIds = function (_a) {
var fakeUniqueId = _a.fakeUniqueId, uniqueId = _a.uniqueId;
var orderQueue = getLocalOrderQueue();
orderQueue.queue = orderQueue.queue.map(function (task) {
if (task.type !== 'update_mutation') {
return task;
}
var itemIndex = task.variables.orderItems.findIndex(function (input) { return 'uniqueId' in input && input.uniqueId === fakeUniqueId; });
if (itemIndex > -1) {
task.variables.orderItems[itemIndex] = __assign(__assign({}, task.variables.orderItems[itemIndex]), { uniqueId: uniqueId });
}
return task;
});
saveLocalOrderQueue(orderQueue);
};
/* eslint-disable @typescript-eslint/no-non-null-assertion */
var AVAILABLE = 'available';
var isSameItem = function (input, item, items) {
var _a, _b;
var isSameId = ((_a = input.id) === null || _a === void 0 ? void 0 : _a.toString()) === item.id;
var isSameSeller = input.seller === item.seller;
// input has no options
if (input.options == null) {
// and the comparing item has, not the same item
if ((_b = item.options) === null || _b === void 0 ? void 0 : _b.length) {
return false;
}
// neither have options, just compare id and seller
return isSameId && isSameSeller;
}
// does every option (assuming assembly option) existing in the cart as separate products?
var optionsExistInCart = input.options.every(function (opItem) { return items.find(function (i) { return i.id === opItem.id; }); });
return isSameId && isSameSeller && optionsExistInCart;
};
var adjustForItemInput = function (item) {
var _a;
return {
id: +((_a = item.id) !== null && _a !== void 0 ? _a : 0),
index: item.index,
quantity: item.quantity,
seller: item.seller,
options: item.options,
};
};
var mapToOrderFormItem = function (itemInput, cartItem) {
var _a, _b, _c, _d;
return {
id: cartItem.id,
productId: cartItem.productId,
name: cartItem.name,
skuName: cartItem.skuName,
skuSpecifications: cartItem.skuSpecifications,
imageUrls: {
at1x: cartItem.imageUrl,
at2x: cartItem.imageUrl,
at3x: cartItem.imageUrl,
},
price: cartItem.price,
listPrice: cartItem.listPrice,
sellingPrice: cartItem.sellingPrice,
measurementUnit: cartItem.measurementUnit,
quantity: (_a = cartItem.quantity) !== null && _a !== void 0 ? _a : 1,
unitMultiplier: (_b = cartItem.unitMultiplier) !== null && _b !== void 0 ? _b : 1,
uniqueId: (_c = itemInput.uniqueId) !== null && _c !== void 0 ? _c : uuid.v4(),
detailUrl: cartItem.detailUrl,
availability: (_d = cartItem.availability) !== null && _d !== void 0 ? _d : AVAILABLE,
additionalInfo: cartItem.additionalInfo,
options: cartItem.options,
seller: cartItem.seller,
attachmentOfferings: [],
attachments: [],
bundleItems: [],
offerings: [],
priceTags: [],
};
};
var filterUndefined = function (value) {
return value !== undefined;
};
var noop = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); }); };
var OrderItemsContext = createContext({
addItem: noop,
updateQuantity: noop,
removeItem: noop,
});
var useOrderItems = function () {
return useContext(OrderItemsContext);
};
function createUseAddItems(_a) {
var _this = this;
var useMutateAddItems = _a.useMutateAddItems, useOrderForm = _a.useOrderForm;
var useAddItems = function (fakeUniqueIdMapRef) {
var setOrderForm = useOrderForm().setOrderForm;
var mutate = useMutateAddItems();
var addItemTask = useCallback(function (_a) {
var mutationInputItems = _a.mutationInputItems, mutationInputMarketingData = _a.mutationInputMarketingData, orderFormItems = _a.orderFormItems, salesChannel = _a.salesChannel;
return ({
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var _a, updatedOrderForm, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, mutate({
items: mutationInputItems,
marketingData: mutationInputMarketingData,
salesChannel: salesChannel,
})];
case 1:
_a = _c.sent(), updatedOrderForm = _a.data, errors = _a.errors;
if (!updatedOrderForm || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
// update the uniqueId of the items that were
// added locally with the value from the server
orderFormItems.forEach(function (orderFormItem) {
var updatedItem = updatedOrderForm === null || updatedOrderForm === void 0 ? void 0 : updatedOrderForm.items.find(function (updatedOrderFormItem) {
return updatedOrderFormItem.id === orderFormItem.id;
});
if (!updatedItem) {
// the item wasn't added to the cart. the reason for this
// may vary, but could be something like the item doesn't
// have stock left, etc.
return;
}
var fakeUniqueId = orderFormItem.uniqueId;
// update all mutations in the queue that referenced
// this item with it's fake `uniqueId`
updateLocalQueueItemIds({
fakeUniqueId: fakeUniqueId,
uniqueId: updatedItem.uniqueId,
});
fakeUniqueIdMapRef.current[fakeUniqueId] = updatedItem.uniqueId;
});
// update the `uniqueId` in the remaining items on local orderForm
setOrderForm(function (prevOrderForm) {
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items
.map(function (item) {
var inputIndex = mutationInputItems.findIndex(function (inputItem) {
return isSameItem(inputItem, item, prevOrderForm.items);
});
if (inputIndex === -1) {
// this item wasn't part of the initial mutation, skip it
return item;
}
var updatedItem = updatedOrderForm.items.find(function (updatedOrderFormItem) {
return updatedOrderFormItem.id === item.id;
});
if (!updatedItem) {
// item was not added to the cart
return null;
}
return __assign(__assign({}, item), { uniqueId: updatedItem.uniqueId });
})
.filter(function (item) { return item != null; }), marketingData: mutationInputMarketingData !== null && mutationInputMarketingData !== void 0 ? mutationInputMarketingData : prevOrderForm.marketingData });
});
return [2 /*return*/, updatedOrderForm];
}
});
}); },
rollback: function () {
setOrderForm(function (prevOrderForm) {
var itemIds = mutationInputItems.map(function (_a) {
var id = _a.id;
return id.toString();
});
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items.filter(function (orderFormItem) {
return !itemIds.includes(orderFormItem.id);
}) });
});
},
});
}, [fakeUniqueIdMapRef, mutate, setOrderForm]);
return addItemTask;
};
return useAddItems;
}
function createUseSetManualPrice(_a) {
var _this = this;
var useMutateSetManualPrice = _a.useMutateSetManualPrice;
var useSetManualPrice = function () {
var mutate = useMutateSetManualPrice();
var setManualPriceTask = useCallback(function (price, itemIndex) {
if (!mutate)
return;
return {
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var _a, data, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, mutate({ itemIndex: itemIndex, price: price })];
case 1:
_a = _c.sent(), data = _a.data, errors = _a.errors;
if (!data || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
return [2 /*return*/, data];
}
});
}); },
};
}, [mutate]);
return setManualPriceTask;
};
return useSetManualPrice;
}
function createUseUpdateQuantity(_a) {
var _this = this;
var useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useOrderForm = _a.useOrderForm;
var useUpdateItem = function (fakeUniqueIdMapRef) {
var setOrderForm = useOrderForm().setOrderForm;
var mutate = useMutateUpdateQuantity();
var updateItemTask = useCallback(function (_a) {
var items = _a.items, orderFormItems = _a.orderFormItems, id = _a.id;
return {
id: id,
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var mutationVariables, _a, data, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
mutationVariables = {
orderItems: items.map(function (input) {
if ('uniqueId' in input) {
// here we need to update the uniqueId again in the mutation
// because it may have been a "fake" `uniqueId` that were generated
// locally so we could manage the items when offline.
//
// so, we will read the value using the `fakeUniqueIdMapRef` because
// it maps a fake `uniqueId` to a real `uniqueId` that was generated by
// the API. if it doesn't contain the value, we will assume that this uniqueId
// is a real one.
var uniqueId = fakeUniqueIdMapRef.current[input.uniqueId] || input.uniqueId;
return { uniqueId: uniqueId, quantity: input.quantity };
}
return input;
}),
};
return [4 /*yield*/, mutate(mutationVariables)];
case 1:
_a = _c.sent(), data = _a.data, errors = _a.errors;
if (!data || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
return [2 /*return*/, data];
}
});
}); },
rollback: function () {
var deletedItemsInput = items.filter(function (_a) {
var quantity = _a.quantity;
return quantity === 0;
});
var updatedItemsInput = items.filter(function (_a) {
var quantity = _a.quantity;
return quantity !== 0;
});
var deletedItems = deletedItemsInput
.map(function (input) {
return orderFormItems.find(function (orderFormItem, itemIndex) {
return 'uniqueId' in input
? orderFormItem.uniqueId === input.uniqueId
: input.index === itemIndex;
});
})
.filter(filterUndefined);
setOrderForm(function (prevOrderForm) {
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items
.map(function (orderFormItem) {
var updatedIndex = updatedItemsInput.findIndex(function (item, itemIndex) {
return 'uniqueId' in item
? orderFormItem.uniqueId === item.uniqueId
: itemIndex === item.index;
});
if (updatedIndex !== -1) {
var updatedItemInput_1 = updatedItemsInput[updatedIndex];
var previousItem = orderFormItems.find(function (prevOrderFormItem, prevOrderFormItemIndex) {
return 'uniqueId' in updatedItemInput_1
? prevOrderFormItem.uniqueId ===
updatedItemInput_1.uniqueId
: prevOrderFormItemIndex === updatedItemInput_1.index;
});
return __assign(__assign({}, orderFormItem), { quantity: previousItem.quantity });
}
return orderFormItem;
})
.concat(deletedItems) });
});
},
};
}, [fakeUniqueIdMapRef, mutate, setOrderForm]);
return updateItemTask;
};
return useUpdateItem;
}
var updateTotalizersAndValue = function (_a) {
var _b, _c, _d, _e, _f;
var totalizers = _a.totalizers, _g = _a.currentValue, currentValue = _g === void 0 ? 0 : _g, newItem = _a.newItem, oldItem = _a.oldItem;
if ((oldItem === null || oldItem === void 0 ? void 0 : oldItem.availability) !== AVAILABLE) {
return { totalizers: totalizers, value: currentValue };
}
var oldItemPrice = ((_b = oldItem.price) !== null && _b !== void 0 ? _b : 0) * ((_c = oldItem.unitMultiplier) !== null && _c !== void 0 ? _c : 1);
var oldItemQuantity = (_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0;
var oldItemSellingPrice = (_e = oldItem.sellingPrice) !== null && _e !== void 0 ? _e : 0;
var oldPrice = oldItemPrice * oldItemQuantity;
var newItemPrice = newItem.price * ((_f = newItem.unitMultiplier) !== null && _f !== void 0 ? _f : 1);
var newPrice = newItemPrice * newItem.quantity;
var subtotalDifference = newPrice - oldPrice;
var oldDiscount = (oldItemSellingPrice - oldItemPrice) * oldItemQuantity;
var newDiscount = (newItem.sellingPrice - newItemPrice) * newItem.quantity;
var discountDifference = newDiscount - oldDiscount;
var updatedValue = currentValue + subtotalDifference + discountDifference;
if (!totalizers.length) {
return {
totalizers: [
{
id: 'Items',
name: 'Items Total',
value: subtotalDifference,
},
{
id: 'Discounts',
name: 'Discounts Total',
value: discountDifference,
},
],
value: updatedValue,
};
}
var newTotalizers = totalizers.map(function (totalizer) {
switch (totalizer.id) {
case 'Items':
return __assign(__assign({}, totalizer), { value: totalizer.value + subtotalDifference });
case 'Discounts':
return __assign(__assign({}, totalizer), { value: totalizer.value + discountDifference });
default:
return totalizer;
}
});
return {
totalizers: newTotalizers,
value: updatedValue,
};
};
var useEnqueueTask = function (_a) {
var useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
var log = useLogger().log;
var _b = useOrderQueue(), enqueue = _b.enqueue, listen = _b.listen;
var queueStatusRef = useQueueStatus(listen);
var setOrderForm = useOrderForm().setOrderForm;
var enqueueTask = useCallback(function (task) {
return enqueue(task.execute, task.id).then(function (orderForm) {
popLocalOrderQueue();
if (queueStatusRef.current === 'Fulfilled') {
setOrderForm(orderForm);
}
else {
setOrderForm(function (prevOrderForm) { return (__assign(__assign({}, prevOrderForm), { messages: __assign({}, orderForm.messages) })); });
}
}, function (error) {
if (error && error.code === TASK_CANCELLED_CODE) {
popLocalOrderQueue(error.index);
return;
}
popLocalOrderQueue();
log({
type: 'Error',
level: 'Critical',
event: error,
workflowType: 'OrderItems',
workflowInstance: 'enqueue-task-error',
});
throw error;
});
}, [enqueue, queueStatusRef, setOrderForm, log]);
return enqueueTask;
};
var useFakeUniqueIdMap = function () {
var fakeUniqueIdMapRef = useRef({});
var listen = useOrderQueue().listen;
useEffect(function () {
return listen('Fulfilled', function () {
// avoid leaking "fake" `uniqueId`.
// this works because everytime we fulfill the queue, we know
// for sure that we won't have any locally generated uniqueId's
// left to map to a real uniqueId.
fakeUniqueIdMapRef.current = {};
});
}, [listen]);
return fakeUniqueIdMapRef;
};
function createOrderItemsProvider$1(_a) {
var useMutateAddItems = _a.useMutateAddItems, useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useMutateSetManualPrice = _a.useMutateSetManualPrice, useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
var useAddItem = createUseAddItems({ useMutateAddItems: useMutateAddItems, useOrderForm: useOrderForm });
var useSetManualPrice = createUseSetManualPrice({
useMutateSetManualPrice: useMutateSetManualPrice,
});
var useUpdateQuantity = createUseUpdateQuantity({
useMutateUpdateQuantity: useMutateUpdateQuantity,
useOrderForm: useOrderForm,
});
var OrderItemsProvider = function (_a) {
var children = _a.children;
var _b = useOrderForm(), orderForm = _b.orderForm, setOrderForm = _b.setOrderForm;
var fakeUniqueIdMapRef = useFakeUniqueIdMap();
var enqueueTask = useEnqueueTask({ useOrderForm: useOrderForm, useLogger: useLogger });
var addItemsTask = useAddItem(fakeUniqueIdMapRef);
var updateItemsTask = useUpdateQuantity(fakeUniqueIdMapRef);
var setManualPriceTask = useSetManualPrice();
var orderFormItemsRef = useRef(orderForm.items);
useEffect(function () {
orderFormItemsRef.current = orderForm.items;
}, [orderForm.items]);
var updateQuantity = useCallback(function (input) {
var _a, _b;
var index;
var uniqueId = '';
var currentOrderFormItems = orderFormItemsRef.current;
if (input.id) {
index = currentOrderFormItems.findIndex(function (orderItem) {
return isSameItem(input, orderItem, currentOrderFormItems);
});
}
else if ('uniqueId' in input) {
uniqueId = input.uniqueId;
index = currentOrderFormItems.findIndex(function (orderItem) { return orderItem.uniqueId === input.uniqueId; });
}
else {
index = (_a = input === null || input === void 0 ? void 0 : input.index) !== null && _a !== void 0 ? _a : -1;
}
if (index < 0 || index >= currentOrderFormItems.length) {
throw new Error("Item " + (input.id || input.uniqueId) + " not found");
}
if (!uniqueId) {
uniqueId = currentOrderFormItems[index].uniqueId;
}
var quantity = (_b = input.quantity) !== null && _b !== void 0 ? _b : 1;
setOrderForm(function (prevOrderForm) {
var updatedItems = prevOrderForm.items.slice();
var oldItem = updatedItems[index];
var newItem = __assign(__assign({}, oldItem), { quantity: quantity });
if (quantity > 0) {
updatedItems[index] = newItem;
}
else {
updatedItems.splice(index, 1);
}
return __assign(__assign(__assign({}, prevOrderForm), updateTotalizersAndValue({
totalizers: prevOrderForm.totalizers,
currentValue: prevOrderForm.value,
newItem: newItem,
oldItem: oldItem,
})), { items: updatedItems });
});
var mutationVariables;
var id = uuid.v4();
if (quantity > 0) {
var localQueue = getLocalOrderQueue().queue;
var previousTaskIndex = -1;
var originalId = id;
// Skip the first element in the queue (which is currently being executed)
// because we can't cancel an in-progress task.
for (var i = 1; i < localQueue.length; i++) {
var task = localQueue[i];
if (task.type === 'update_mutation' &&
task.variables.orderItems.every(function (itemInput) { return itemInput.quantity > 0; })) {
// If we find an update-only mutation (without removed items)
// we will re-use it's id so we minimize the number of updates
// to send to the API
previousTaskIndex = i;
id = task.id;
}
else {
// If we find any other kind of request we need to reset our
// `previousTaskIndex` and `id` because we can't rely on the indexes
// of the operations done before this task. For example, assume the
// following cart:
//
// [{ id: 1, quantity: 2 }, { id: 2, quantity: 3 }, { id: 3, quantity: 1 }]
//
// If we update the second item's quantity to 1, then remove the first
// item and update the third item quantity to 2, we can't "join" the first
// and last updates, because the indexes will have been shifted due to
// the second item being removed (and the API rely on the index of the items,
// even though we send the unique id). The same could happen if we add one
// item and the cart isn't using the default "add_time" sort algorithm.
previousTaskIndex = -1;
id = originalId;
}
}
var previousTask = previousTaskIndex === -1 ? undefined : localQueue[previousTaskIndex];
var previousTaskItems = (previousTask === null || previousTask === void 0 ? void 0 : previousTask.type) === 'update_mutation'
? previousTask.variables.orderItems
: [];
var itemIndexInPreviousTask_1 = previousTaskItems.findIndex(function (prevInput) {
return 'uniqueId' in prevInput
? prevInput.uniqueId === uniqueId
: prevInput.index === index;
});
mutationVariables = {
orderItems: itemIndexInPreviousTask_1 > -1
? previousTaskItems.map(function (prevInput, prevInputIndex) {
return prevInputIndex === itemIndexInPreviousTask_1
? { uniqueId: uniqueId, quantity: quantity }
: prevInput;
})
: previousTaskItems.concat([{ uniqueId: uniqueId, quantity: quantity }]),
};
}
else {
mutationVariables = { orderItems: [{ uniqueId: uniqueId, quantity: quantity }] };
}
pushLocalOrderQueue({
id: id,
type: 'update_mutation',
variables: mutationVariables,
orderFormItems: currentOrderFormItems,
});
enqueueTask(updateItemsTask({
items: mutationVariables.orderItems,
orderFormItems: currentOrderFormItems,
id: id,
}));
}, [enqueueTask, setOrderForm, updateItemsTask]);
/**
* Add the items to the order form.
* In case of an item already in the cart, it increments its quantity.
*/
var addItem = useCallback(function (items, marketingData, salesChannel) {
var _a = items.reduce(function (acc, item) {
var _a;
var newList = acc.newItems, updateList = acc.updatedItems;
// assembly items are always different
var isAssemblyItem = item.options && item.options.length > 0;
var existingItem = isAssemblyItem
? undefined
: orderFormItemsRef.current.find(function (i) {
return isSameItem(item, i, items);
});
if (existingItem == null) {
newList.push(item);
}
else {
updateList.push(__assign(__assign({}, item), { quantity: ((_a = item.quantity) !== null && _a !== void 0 ? _a : 1) + existingItem.quantity }));
}
return acc;
}, { newItems: [], updatedItems: [] }), newItems = _a.newItems, updatedItems = _a.updatedItems;
if (updatedItems.length) {
updatedItems.forEach(function (item) { return updateQuantity(item); });
}
if (newItems.length === 0) {
return;
}
var mutationInputItems = newItems.map(adjustForItemInput);
var orderFormItems = newItems.map(function (cartItem, index) {
return mapToOrderFormItem(mutationInputItems[index], cartItem);
});
setOrderForm(function (prevOrderForm) {
var _a;
return __assign(__assign({}, prevOrderForm), { items: [].concat(orderFormItemsRef.current, orderFormItems), totalizers: orderFormItems.reduce(function (totalizers, item) {
return updateTotalizersAndValue({ totalizers: totalizers, newItem: item })
.totalizers;
}, (_a = prevOrderForm.totalizers) !== null && _a !== void 0 ? _a : []), marketingData: marketingData !== null && marketingData !== void 0 ? marketingData : prevOrderForm.marketingData, value: prevOrderForm.value +
orderFormItems.reduce(function (total, item) { return total + item.sellingPrice * item.quantity; }, 0) });
});
pushLocalOrderQueue({
type: 'add_mutation',
variables: {
items: mutationInputItems,
marketingData: marketingData,
salesChannel: salesChannel,
},
orderFormItems: orderFormItems,
});
enqueueTask(addItemsTask({
mutationInputItems: mutationInputItems,
mutationInputMarketingData: marketingData,
orderFormItems: orderFormItems,
salesChannel: salesChannel,
}));
}, [addItemsTask, enqueueTask, setOrderForm, updateQuantity]);
var setManualPrice = useCallback(function (price, itemIndex) {
var task = setManualPriceTask(price, itemIndex);
if (!task)
return;
enqueueTask(task);
}, [enqueueTask, setManualPriceTask]);
var removeItem = useCallback(function (props) { return updateQuantity(__assign(__assign({}, props), { quantity: 0 })); }, [updateQuantity]);
var value = useMemo(function () { return ({ addItem: addItem, updateQuantity: updateQuantity, removeItem: removeItem, setManualPrice: setManualPrice }); }, [addItem, updateQuantity, removeItem, setManualPrice]);
useEffect(function () {
var localOrderQueue = getLocalOrderQueue();
localOrderQueue.queue.forEach(function (task) {
if (task.type === 'add_mutation') {
enqueueTask(addItemsTask({
mutationInputItems: task.variables.items,
mutationInputMarketingData: task.variables.marketingData,
orderFormItems: task.orderFormItems,
salesChannel: task.variables.salesChannel,
}));
}
else if (task.type === 'update_mutation') {
enqueueTask(updateItemsTask({
items: task.variables.orderItems,
orderFormItems: task.orderFormItems,
id: task.id,
}));
}
});
}, [addItemsTask, enqueueTask, updateItemsTask]);
return (React.createElement(OrderItemsContext.Provider, { value: value }, children));
};
return { OrderItemsProvider: OrderItemsProvider, useOrderItems: useOrderItems };
}
function createOrderItemsProvider(_a) {
var useMutateAddItems = _a.useMutateAddItems, useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useMutateSetManualPrice = _a.useMutateSetManualPrice, useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
return createOrderItemsProvider$1({
useMutateAddItems: useMutateAddItems,
useMutateUpdateQuantity: useMutateUpdateQuantity,
useMutateSetManualPrice: useMutateSetManualPrice,
useOrderForm: useOrderForm,
useLogger: useLogger,
});
}
export { createOrderItemsProvider };
//# sourceMappingURL=index.esm.js.map
/*!
* @vtex/order-items v0.0.1-0
* @vtex/order-items v0.0.1-1
* (c) VTEX

@@ -11,5 +11,768 @@ * Released under the MIT License.

var HelloWorld = 'hello world';
var React = require('react');
var uuid = require('uuid');
var orderManager = require('@vtex/order-manager');
exports.HelloWorld = HelloWorld;
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
var __assign = function() {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __awaiter(thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
}
function __generator(thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
}
var DEFAULT_LOCAL_ORDER_QUEUE = {
queue: [],
};
var getLocalOrderQueue = function () {
var _a;
var queue = null;
try {
queue = JSON.parse((_a = localStorage.getItem('orderQueue')) !== null && _a !== void 0 ? _a : 'null');
}
catch (_b) {
// ignore
}
if (!queue) {
localStorage.setItem('orderQueue', JSON.stringify(DEFAULT_LOCAL_ORDER_QUEUE));
}
return queue !== null && queue !== void 0 ? queue : DEFAULT_LOCAL_ORDER_QUEUE;
};
var saveLocalOrderQueue = function (orderQueue) {
localStorage.setItem('orderQueue', JSON.stringify(orderQueue));
};
var pushLocalOrderQueue = function (task) {
var orderQueue = getLocalOrderQueue();
var index = orderQueue.queue.push(task);
saveLocalOrderQueue(orderQueue);
return index;
};
var popLocalOrderQueue = function (index) {
if (index === void 0) { index = 0; }
var orderQueue = getLocalOrderQueue();
var task = orderQueue.queue[index];
if (!task) {
return undefined;
}
orderQueue.queue.splice(index, 1);
saveLocalOrderQueue(orderQueue);
return task;
};
var updateLocalQueueItemIds = function (_a) {
var fakeUniqueId = _a.fakeUniqueId, uniqueId = _a.uniqueId;
var orderQueue = getLocalOrderQueue();
orderQueue.queue = orderQueue.queue.map(function (task) {
if (task.type !== 'update_mutation') {
return task;
}
var itemIndex = task.variables.orderItems.findIndex(function (input) { return 'uniqueId' in input && input.uniqueId === fakeUniqueId; });
if (itemIndex > -1) {
task.variables.orderItems[itemIndex] = __assign(__assign({}, task.variables.orderItems[itemIndex]), { uniqueId: uniqueId });
}
return task;
});
saveLocalOrderQueue(orderQueue);
};
/* eslint-disable @typescript-eslint/no-non-null-assertion */
var AVAILABLE = 'available';
var isSameItem = function (input, item, items) {
var _a, _b;
var isSameId = ((_a = input.id) === null || _a === void 0 ? void 0 : _a.toString()) === item.id;
var isSameSeller = input.seller === item.seller;
// input has no options
if (input.options == null) {
// and the comparing item has, not the same item
if ((_b = item.options) === null || _b === void 0 ? void 0 : _b.length) {
return false;
}
// neither have options, just compare id and seller
return isSameId && isSameSeller;
}
// does every option (assuming assembly option) existing in the cart as separate products?
var optionsExistInCart = input.options.every(function (opItem) { return items.find(function (i) { return i.id === opItem.id; }); });
return isSameId && isSameSeller && optionsExistInCart;
};
var adjustForItemInput = function (item) {
var _a;
return {
id: +((_a = item.id) !== null && _a !== void 0 ? _a : 0),
index: item.index,
quantity: item.quantity,
seller: item.seller,
options: item.options,
};
};
var mapToOrderFormItem = function (itemInput, cartItem) {
var _a, _b, _c, _d;
return {
id: cartItem.id,
productId: cartItem.productId,
name: cartItem.name,
skuName: cartItem.skuName,
skuSpecifications: cartItem.skuSpecifications,
imageUrls: {
at1x: cartItem.imageUrl,
at2x: cartItem.imageUrl,
at3x: cartItem.imageUrl,
},
price: cartItem.price,
listPrice: cartItem.listPrice,
sellingPrice: cartItem.sellingPrice,
measurementUnit: cartItem.measurementUnit,
quantity: (_a = cartItem.quantity) !== null && _a !== void 0 ? _a : 1,
unitMultiplier: (_b = cartItem.unitMultiplier) !== null && _b !== void 0 ? _b : 1,
uniqueId: (_c = itemInput.uniqueId) !== null && _c !== void 0 ? _c : uuid__namespace.v4(),
detailUrl: cartItem.detailUrl,
availability: (_d = cartItem.availability) !== null && _d !== void 0 ? _d : AVAILABLE,
additionalInfo: cartItem.additionalInfo,
options: cartItem.options,
seller: cartItem.seller,
attachmentOfferings: [],
attachments: [],
bundleItems: [],
offerings: [],
priceTags: [],
};
};
var filterUndefined = function (value) {
return value !== undefined;
};
var noop = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); }); };
var OrderItemsContext = React.createContext({
addItem: noop,
updateQuantity: noop,
removeItem: noop,
});
var useOrderItems = function () {
return React.useContext(OrderItemsContext);
};
function createUseAddItems(_a) {
var _this = this;
var useMutateAddItems = _a.useMutateAddItems, useOrderForm = _a.useOrderForm;
var useAddItems = function (fakeUniqueIdMapRef) {
var setOrderForm = useOrderForm().setOrderForm;
var mutate = useMutateAddItems();
var addItemTask = React.useCallback(function (_a) {
var mutationInputItems = _a.mutationInputItems, mutationInputMarketingData = _a.mutationInputMarketingData, orderFormItems = _a.orderFormItems, salesChannel = _a.salesChannel;
return ({
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var _a, updatedOrderForm, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, mutate({
items: mutationInputItems,
marketingData: mutationInputMarketingData,
salesChannel: salesChannel,
})];
case 1:
_a = _c.sent(), updatedOrderForm = _a.data, errors = _a.errors;
if (!updatedOrderForm || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
// update the uniqueId of the items that were
// added locally with the value from the server
orderFormItems.forEach(function (orderFormItem) {
var updatedItem = updatedOrderForm === null || updatedOrderForm === void 0 ? void 0 : updatedOrderForm.items.find(function (updatedOrderFormItem) {
return updatedOrderFormItem.id === orderFormItem.id;
});
if (!updatedItem) {
// the item wasn't added to the cart. the reason for this
// may vary, but could be something like the item doesn't
// have stock left, etc.
return;
}
var fakeUniqueId = orderFormItem.uniqueId;
// update all mutations in the queue that referenced
// this item with it's fake `uniqueId`
updateLocalQueueItemIds({
fakeUniqueId: fakeUniqueId,
uniqueId: updatedItem.uniqueId,
});
fakeUniqueIdMapRef.current[fakeUniqueId] = updatedItem.uniqueId;
});
// update the `uniqueId` in the remaining items on local orderForm
setOrderForm(function (prevOrderForm) {
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items
.map(function (item) {
var inputIndex = mutationInputItems.findIndex(function (inputItem) {
return isSameItem(inputItem, item, prevOrderForm.items);
});
if (inputIndex === -1) {
// this item wasn't part of the initial mutation, skip it
return item;
}
var updatedItem = updatedOrderForm.items.find(function (updatedOrderFormItem) {
return updatedOrderFormItem.id === item.id;
});
if (!updatedItem) {
// item was not added to the cart
return null;
}
return __assign(__assign({}, item), { uniqueId: updatedItem.uniqueId });
})
.filter(function (item) { return item != null; }), marketingData: mutationInputMarketingData !== null && mutationInputMarketingData !== void 0 ? mutationInputMarketingData : prevOrderForm.marketingData });
});
return [2 /*return*/, updatedOrderForm];
}
});
}); },
rollback: function () {
setOrderForm(function (prevOrderForm) {
var itemIds = mutationInputItems.map(function (_a) {
var id = _a.id;
return id.toString();
});
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items.filter(function (orderFormItem) {
return !itemIds.includes(orderFormItem.id);
}) });
});
},
});
}, [fakeUniqueIdMapRef, mutate, setOrderForm]);
return addItemTask;
};
return useAddItems;
}
function createUseSetManualPrice(_a) {
var _this = this;
var useMutateSetManualPrice = _a.useMutateSetManualPrice;
var useSetManualPrice = function () {
var mutate = useMutateSetManualPrice();
var setManualPriceTask = React.useCallback(function (price, itemIndex) {
if (!mutate)
return;
return {
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var _a, data, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, mutate({ itemIndex: itemIndex, price: price })];
case 1:
_a = _c.sent(), data = _a.data, errors = _a.errors;
if (!data || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
return [2 /*return*/, data];
}
});
}); },
};
}, [mutate]);
return setManualPriceTask;
};
return useSetManualPrice;
}
function createUseUpdateQuantity(_a) {
var _this = this;
var useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useOrderForm = _a.useOrderForm;
var useUpdateItem = function (fakeUniqueIdMapRef) {
var setOrderForm = useOrderForm().setOrderForm;
var mutate = useMutateUpdateQuantity();
var updateItemTask = React.useCallback(function (_a) {
var items = _a.items, orderFormItems = _a.orderFormItems, id = _a.id;
return {
id: id,
execute: function () { return __awaiter(_this, void 0, void 0, function () {
var mutationVariables, _a, data, errors;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
mutationVariables = {
orderItems: items.map(function (input) {
if ('uniqueId' in input) {
// here we need to update the uniqueId again in the mutation
// because it may have been a "fake" `uniqueId` that were generated
// locally so we could manage the items when offline.
//
// so, we will read the value using the `fakeUniqueIdMapRef` because
// it maps a fake `uniqueId` to a real `uniqueId` that was generated by
// the API. if it doesn't contain the value, we will assume that this uniqueId
// is a real one.
var uniqueId = fakeUniqueIdMapRef.current[input.uniqueId] || input.uniqueId;
return { uniqueId: uniqueId, quantity: input.quantity };
}
return input;
}),
};
return [4 /*yield*/, mutate(mutationVariables)];
case 1:
_a = _c.sent(), data = _a.data, errors = _a.errors;
if (!data || ((_b = errors === null || errors === void 0 ? void 0 : errors.length) !== null && _b !== void 0 ? _b : 0) > 0) {
throw errors === null || errors === void 0 ? void 0 : errors[0];
}
return [2 /*return*/, data];
}
});
}); },
rollback: function () {
var deletedItemsInput = items.filter(function (_a) {
var quantity = _a.quantity;
return quantity === 0;
});
var updatedItemsInput = items.filter(function (_a) {
var quantity = _a.quantity;
return quantity !== 0;
});
var deletedItems = deletedItemsInput
.map(function (input) {
return orderFormItems.find(function (orderFormItem, itemIndex) {
return 'uniqueId' in input
? orderFormItem.uniqueId === input.uniqueId
: input.index === itemIndex;
});
})
.filter(filterUndefined);
setOrderForm(function (prevOrderForm) {
return __assign(__assign({}, prevOrderForm), { items: prevOrderForm.items
.map(function (orderFormItem) {
var updatedIndex = updatedItemsInput.findIndex(function (item, itemIndex) {
return 'uniqueId' in item
? orderFormItem.uniqueId === item.uniqueId
: itemIndex === item.index;
});
if (updatedIndex !== -1) {
var updatedItemInput_1 = updatedItemsInput[updatedIndex];
var previousItem = orderFormItems.find(function (prevOrderFormItem, prevOrderFormItemIndex) {
return 'uniqueId' in updatedItemInput_1
? prevOrderFormItem.uniqueId ===
updatedItemInput_1.uniqueId
: prevOrderFormItemIndex === updatedItemInput_1.index;
});
return __assign(__assign({}, orderFormItem), { quantity: previousItem.quantity });
}
return orderFormItem;
})
.concat(deletedItems) });
});
},
};
}, [fakeUniqueIdMapRef, mutate, setOrderForm]);
return updateItemTask;
};
return useUpdateItem;
}
var updateTotalizersAndValue = function (_a) {
var _b, _c, _d, _e, _f;
var totalizers = _a.totalizers, _g = _a.currentValue, currentValue = _g === void 0 ? 0 : _g, newItem = _a.newItem, oldItem = _a.oldItem;
if ((oldItem === null || oldItem === void 0 ? void 0 : oldItem.availability) !== AVAILABLE) {
return { totalizers: totalizers, value: currentValue };
}
var oldItemPrice = ((_b = oldItem.price) !== null && _b !== void 0 ? _b : 0) * ((_c = oldItem.unitMultiplier) !== null && _c !== void 0 ? _c : 1);
var oldItemQuantity = (_d = oldItem.quantity) !== null && _d !== void 0 ? _d : 0;
var oldItemSellingPrice = (_e = oldItem.sellingPrice) !== null && _e !== void 0 ? _e : 0;
var oldPrice = oldItemPrice * oldItemQuantity;
var newItemPrice = newItem.price * ((_f = newItem.unitMultiplier) !== null && _f !== void 0 ? _f : 1);
var newPrice = newItemPrice * newItem.quantity;
var subtotalDifference = newPrice - oldPrice;
var oldDiscount = (oldItemSellingPrice - oldItemPrice) * oldItemQuantity;
var newDiscount = (newItem.sellingPrice - newItemPrice) * newItem.quantity;
var discountDifference = newDiscount - oldDiscount;
var updatedValue = currentValue + subtotalDifference + discountDifference;
if (!totalizers.length) {
return {
totalizers: [
{
id: 'Items',
name: 'Items Total',
value: subtotalDifference,
},
{
id: 'Discounts',
name: 'Discounts Total',
value: discountDifference,
},
],
value: updatedValue,
};
}
var newTotalizers = totalizers.map(function (totalizer) {
switch (totalizer.id) {
case 'Items':
return __assign(__assign({}, totalizer), { value: totalizer.value + subtotalDifference });
case 'Discounts':
return __assign(__assign({}, totalizer), { value: totalizer.value + discountDifference });
default:
return totalizer;
}
});
return {
totalizers: newTotalizers,
value: updatedValue,
};
};
var useEnqueueTask = function (_a) {
var useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
var log = useLogger().log;
var _b = orderManager.useOrderQueue(), enqueue = _b.enqueue, listen = _b.listen;
var queueStatusRef = orderManager.useQueueStatus(listen);
var setOrderForm = useOrderForm().setOrderForm;
var enqueueTask = React.useCallback(function (task) {
return enqueue(task.execute, task.id).then(function (orderForm) {
popLocalOrderQueue();
if (queueStatusRef.current === 'Fulfilled') {
setOrderForm(orderForm);
}
else {
setOrderForm(function (prevOrderForm) { return (__assign(__assign({}, prevOrderForm), { messages: __assign({}, orderForm.messages) })); });
}
}, function (error) {
if (error && error.code === orderManager.TASK_CANCELLED_CODE) {
popLocalOrderQueue(error.index);
return;
}
popLocalOrderQueue();
log({
type: 'Error',
level: 'Critical',
event: error,
workflowType: 'OrderItems',
workflowInstance: 'enqueue-task-error',
});
throw error;
});
}, [enqueue, queueStatusRef, setOrderForm, log]);
return enqueueTask;
};
var useFakeUniqueIdMap = function () {
var fakeUniqueIdMapRef = React.useRef({});
var listen = orderManager.useOrderQueue().listen;
React.useEffect(function () {
return listen('Fulfilled', function () {
// avoid leaking "fake" `uniqueId`.
// this works because everytime we fulfill the queue, we know
// for sure that we won't have any locally generated uniqueId's
// left to map to a real uniqueId.
fakeUniqueIdMapRef.current = {};
});
}, [listen]);
return fakeUniqueIdMapRef;
};
function createOrderItemsProvider$1(_a) {
var useMutateAddItems = _a.useMutateAddItems, useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useMutateSetManualPrice = _a.useMutateSetManualPrice, useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
var useAddItem = createUseAddItems({ useMutateAddItems: useMutateAddItems, useOrderForm: useOrderForm });
var useSetManualPrice = createUseSetManualPrice({
useMutateSetManualPrice: useMutateSetManualPrice,
});
var useUpdateQuantity = createUseUpdateQuantity({
useMutateUpdateQuantity: useMutateUpdateQuantity,
useOrderForm: useOrderForm,
});
var OrderItemsProvider = function (_a) {
var children = _a.children;
var _b = useOrderForm(), orderForm = _b.orderForm, setOrderForm = _b.setOrderForm;
var fakeUniqueIdMapRef = useFakeUniqueIdMap();
var enqueueTask = useEnqueueTask({ useOrderForm: useOrderForm, useLogger: useLogger });
var addItemsTask = useAddItem(fakeUniqueIdMapRef);
var updateItemsTask = useUpdateQuantity(fakeUniqueIdMapRef);
var setManualPriceTask = useSetManualPrice();
var orderFormItemsRef = React.useRef(orderForm.items);
React.useEffect(function () {
orderFormItemsRef.current = orderForm.items;
}, [orderForm.items]);
var updateQuantity = React.useCallback(function (input) {
var _a, _b;
var index;
var uniqueId = '';
var currentOrderFormItems = orderFormItemsRef.current;
if (input.id) {
index = currentOrderFormItems.findIndex(function (orderItem) {
return isSameItem(input, orderItem, currentOrderFormItems);
});
}
else if ('uniqueId' in input) {
uniqueId = input.uniqueId;
index = currentOrderFormItems.findIndex(function (orderItem) { return orderItem.uniqueId === input.uniqueId; });
}
else {
index = (_a = input === null || input === void 0 ? void 0 : input.index) !== null && _a !== void 0 ? _a : -1;
}
if (index < 0 || index >= currentOrderFormItems.length) {
throw new Error("Item " + (input.id || input.uniqueId) + " not found");
}
if (!uniqueId) {
uniqueId = currentOrderFormItems[index].uniqueId;
}
var quantity = (_b = input.quantity) !== null && _b !== void 0 ? _b : 1;
setOrderForm(function (prevOrderForm) {
var updatedItems = prevOrderForm.items.slice();
var oldItem = updatedItems[index];
var newItem = __assign(__assign({}, oldItem), { quantity: quantity });
if (quantity > 0) {
updatedItems[index] = newItem;
}
else {
updatedItems.splice(index, 1);
}
return __assign(__assign(__assign({}, prevOrderForm), updateTotalizersAndValue({
totalizers: prevOrderForm.totalizers,
currentValue: prevOrderForm.value,
newItem: newItem,
oldItem: oldItem,
})), { items: updatedItems });
});
var mutationVariables;
var id = uuid__namespace.v4();
if (quantity > 0) {
var localQueue = getLocalOrderQueue().queue;
var previousTaskIndex = -1;
var originalId = id;
// Skip the first element in the queue (which is currently being executed)
// because we can't cancel an in-progress task.
for (var i = 1; i < localQueue.length; i++) {
var task = localQueue[i];
if (task.type === 'update_mutation' &&
task.variables.orderItems.every(function (itemInput) { return itemInput.quantity > 0; })) {
// If we find an update-only mutation (without removed items)
// we will re-use it's id so we minimize the number of updates
// to send to the API
previousTaskIndex = i;
id = task.id;
}
else {
// If we find any other kind of request we need to reset our
// `previousTaskIndex` and `id` because we can't rely on the indexes
// of the operations done before this task. For example, assume the
// following cart:
//
// [{ id: 1, quantity: 2 }, { id: 2, quantity: 3 }, { id: 3, quantity: 1 }]
//
// If we update the second item's quantity to 1, then remove the first
// item and update the third item quantity to 2, we can't "join" the first
// and last updates, because the indexes will have been shifted due to
// the second item being removed (and the API rely on the index of the items,
// even though we send the unique id). The same could happen if we add one
// item and the cart isn't using the default "add_time" sort algorithm.
previousTaskIndex = -1;
id = originalId;
}
}
var previousTask = previousTaskIndex === -1 ? undefined : localQueue[previousTaskIndex];
var previousTaskItems = (previousTask === null || previousTask === void 0 ? void 0 : previousTask.type) === 'update_mutation'
? previousTask.variables.orderItems
: [];
var itemIndexInPreviousTask_1 = previousTaskItems.findIndex(function (prevInput) {
return 'uniqueId' in prevInput
? prevInput.uniqueId === uniqueId
: prevInput.index === index;
});
mutationVariables = {
orderItems: itemIndexInPreviousTask_1 > -1
? previousTaskItems.map(function (prevInput, prevInputIndex) {
return prevInputIndex === itemIndexInPreviousTask_1
? { uniqueId: uniqueId, quantity: quantity }
: prevInput;
})
: previousTaskItems.concat([{ uniqueId: uniqueId, quantity: quantity }]),
};
}
else {
mutationVariables = { orderItems: [{ uniqueId: uniqueId, quantity: quantity }] };
}
pushLocalOrderQueue({
id: id,
type: 'update_mutation',
variables: mutationVariables,
orderFormItems: currentOrderFormItems,
});
enqueueTask(updateItemsTask({
items: mutationVariables.orderItems,
orderFormItems: currentOrderFormItems,
id: id,
}));
}, [enqueueTask, setOrderForm, updateItemsTask]);
/**
* Add the items to the order form.
* In case of an item already in the cart, it increments its quantity.
*/
var addItem = React.useCallback(function (items, marketingData, salesChannel) {
var _a = items.reduce(function (acc, item) {
var _a;
var newList = acc.newItems, updateList = acc.updatedItems;
// assembly items are always different
var isAssemblyItem = item.options && item.options.length > 0;
var existingItem = isAssemblyItem
? undefined
: orderFormItemsRef.current.find(function (i) {
return isSameItem(item, i, items);
});
if (existingItem == null) {
newList.push(item);
}
else {
updateList.push(__assign(__assign({}, item), { quantity: ((_a = item.quantity) !== null && _a !== void 0 ? _a : 1) + existingItem.quantity }));
}
return acc;
}, { newItems: [], updatedItems: [] }), newItems = _a.newItems, updatedItems = _a.updatedItems;
if (updatedItems.length) {
updatedItems.forEach(function (item) { return updateQuantity(item); });
}
if (newItems.length === 0) {
return;
}
var mutationInputItems = newItems.map(adjustForItemInput);
var orderFormItems = newItems.map(function (cartItem, index) {
return mapToOrderFormItem(mutationInputItems[index], cartItem);
});
setOrderForm(function (prevOrderForm) {
var _a;
return __assign(__assign({}, prevOrderForm), { items: [].concat(orderFormItemsRef.current, orderFormItems), totalizers: orderFormItems.reduce(function (totalizers, item) {
return updateTotalizersAndValue({ totalizers: totalizers, newItem: item })
.totalizers;
}, (_a = prevOrderForm.totalizers) !== null && _a !== void 0 ? _a : []), marketingData: marketingData !== null && marketingData !== void 0 ? marketingData : prevOrderForm.marketingData, value: prevOrderForm.value +
orderFormItems.reduce(function (total, item) { return total + item.sellingPrice * item.quantity; }, 0) });
});
pushLocalOrderQueue({
type: 'add_mutation',
variables: {
items: mutationInputItems,
marketingData: marketingData,
salesChannel: salesChannel,
},
orderFormItems: orderFormItems,
});
enqueueTask(addItemsTask({
mutationInputItems: mutationInputItems,
mutationInputMarketingData: marketingData,
orderFormItems: orderFormItems,
salesChannel: salesChannel,
}));
}, [addItemsTask, enqueueTask, setOrderForm, updateQuantity]);
var setManualPrice = React.useCallback(function (price, itemIndex) {
var task = setManualPriceTask(price, itemIndex);
if (!task)
return;
enqueueTask(task);
}, [enqueueTask, setManualPriceTask]);
var removeItem = React.useCallback(function (props) { return updateQuantity(__assign(__assign({}, props), { quantity: 0 })); }, [updateQuantity]);
var value = React.useMemo(function () { return ({ addItem: addItem, updateQuantity: updateQuantity, removeItem: removeItem, setManualPrice: setManualPrice }); }, [addItem, updateQuantity, removeItem, setManualPrice]);
React.useEffect(function () {
var localOrderQueue = getLocalOrderQueue();
localOrderQueue.queue.forEach(function (task) {
if (task.type === 'add_mutation') {
enqueueTask(addItemsTask({
mutationInputItems: task.variables.items,
mutationInputMarketingData: task.variables.marketingData,
orderFormItems: task.orderFormItems,
salesChannel: task.variables.salesChannel,
}));
}
else if (task.type === 'update_mutation') {
enqueueTask(updateItemsTask({
items: task.variables.orderItems,
orderFormItems: task.orderFormItems,
id: task.id,
}));
}
});
}, [addItemsTask, enqueueTask, updateItemsTask]);
return (React__default['default'].createElement(OrderItemsContext.Provider, { value: value }, children));
};
return { OrderItemsProvider: OrderItemsProvider, useOrderItems: useOrderItems };
}
function createOrderItemsProvider(_a) {
var useMutateAddItems = _a.useMutateAddItems, useMutateUpdateQuantity = _a.useMutateUpdateQuantity, useMutateSetManualPrice = _a.useMutateSetManualPrice, useOrderForm = _a.useOrderForm, useLogger = _a.useLogger;
return createOrderItemsProvider$1({
useMutateAddItems: useMutateAddItems,
useMutateUpdateQuantity: useMutateUpdateQuantity,
useMutateSetManualPrice: useMutateSetManualPrice,
useOrderForm: useOrderForm,
useLogger: useLogger,
});
}
exports.createOrderItemsProvider = createOrderItemsProvider;
//# sourceMappingURL=index.js.map

14

package.json
{
"name": "@vtex/order-items",
"version": "0.0.1-0",
"version": "0.0.1-1",
"description": "",

@@ -8,6 +8,3 @@ "cdn": "dist/index.umd.js",

"types": "types/index.d.ts",
"unpkg": "dist/index.umd.js",
"module": "dist/index.esm.js",
"jsdelivr": "dist/index.umd.js",
"umd:main": "dist/index.umd.js",
"scripts": {

@@ -17,6 +14,9 @@ "test": "vtex-test-tools test --passWithNoTests",

"build": "rollup --config ./rollup.config.js",
"prepublishOnly": "yarn build && yarn test"
"prepublishOnly": "yarn build"
},
"dependencies": {
"react": "17.0.1"
"@types/uuid": "^8.3.0",
"@vtex/order-manager": "0.0.3-12",
"react": "17.0.1",
"uuid": "^8.3.2"
},

@@ -30,2 +30,3 @@ "peerDependencies": {

"@types/prettier": "^2.1.2",
"@types/ramda": "^0.27.39",
"@types/react": "^17.0.3",

@@ -38,2 +39,3 @@ "@vtex/prettier-config": "^0.3.6",

"prettier": "^2.1.2",
"ramda": "^0.27.1",
"react-apollo": "3",

@@ -40,0 +42,0 @@ "react-intl": "3",

@@ -1,2 +0,2 @@

export declare const HelloWorld = "hello world";
export { createOrderItemsProvider } from './createOrderItems';
//# sourceMappingURL=index.d.ts.map

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc