@open-tender/store
Version:
A library of hooks, reducers, utility functions, and types for use with Open Tender applications that utilize our in-store POS API
65 lines (64 loc) • 3.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importDefault(require("react"));
var hooks_1 = require("../app/hooks");
var hooks_2 = require("../hooks");
var slices_1 = require("../slices");
var Quantity_1 = tslib_1.__importDefault(require("./Quantity"));
var PointsReward = function (_a) {
var _b;
var item = _a.item, callback = _a.callback, children = _a.children, QuantityView = _a.QuantityView;
var dispatch = (0, hooks_1.useAppDispatch)();
var apiUrl = (0, hooks_1.useAppSelector)(slices_1.selectKioskApi);
var _c = (0, hooks_1.useAppSelector)(slices_1.selectKioskConfig), config = _c.pointsReward, successMessageConfig = _c.successMessage;
var checkPoints = (0, hooks_1.useAppSelector)(slices_1.selectPosCheckout).points;
var _d = (0, hooks_2.useCustomerRewards)(), points = _d.points, pointsRemaining = _d.pointsRemaining;
if (!item.points || item.points.index === undefined || !points)
return null;
var _e = item.points, index = _e.index, per = _e.per;
// const appliedIndices = checkPoints.map(i => i.index)
// const isApplied = appliedIndices.includes(index)
var itemPointsApplied = ((_b = checkPoints.find(function (i) { return i.index === index; })) === null || _b === void 0 ? void 0 : _b.points) || 0;
var apply = function () {
var currentItem = checkPoints.find(function (i) { return i.index === index; });
var currentPoints = currentItem ? currentItem.points : 0;
var updatedItem = {
index: index,
points: currentPoints + per
};
var otherItems = checkPoints.filter(function (i) { return i.index !== index; });
var updatedPoints = tslib_1.__spreadArray(tslib_1.__spreadArray([], otherItems, true), [updatedItem], false);
dispatch((0, slices_1.setPoints)(updatedPoints));
// dispatch(validatePosOrder())
if (callback)
callback();
};
var remove = function () {
var currentItem = checkPoints.find(function (i) { return i.index === index; });
var currentPoints = currentItem ? currentItem.points : 0;
var updatedItem = {
index: index,
points: currentPoints - per
};
var otherItems = checkPoints.filter(function (i) { return i.index !== index; });
var updatedPoints = updatedItem.points > 0 ? tslib_1.__spreadArray(tslib_1.__spreadArray([], otherItems, true), [updatedItem], false) : otherItems;
dispatch((0, slices_1.setPoints)(updatedPoints));
// dispatch(validatePosOrder())
if (callback)
callback();
};
var renderQuantity = function (quantity, incrementDisabled) { return (react_1.default.createElement(Quantity_1.default, { quantity: quantity, increment: apply, incrementDisabled: incrementDisabled, decrement: remove, fontSize: 24, iconSize: 28, children: QuantityView })); };
if (!config)
return null;
return children({
config: config,
item: item,
itemPointsApplied: itemPointsApplied,
pointsRemaining: pointsRemaining,
successMessageConfig: successMessageConfig,
renderQuantity: renderQuantity,
apiUrl: apiUrl
});
};
exports.default = PointsReward;