@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
63 lines (62 loc) • 3.14 kB
JavaScript
import { __spreadArray } from "tslib";
import React from 'react';
import { useAppDispatch, useAppSelector } from '../app/hooks';
import { useCustomerRewards } from '../hooks';
import { selectKioskApi, selectKioskConfig, selectPosCheckout, setPoints } from '../slices';
import { default as QuantityContainer } from './Quantity';
var PointsReward = function (_a) {
var _b;
var item = _a.item, callback = _a.callback, children = _a.children, QuantityView = _a.QuantityView;
var dispatch = useAppDispatch();
var apiUrl = useAppSelector(selectKioskApi);
var _c = useAppSelector(selectKioskConfig), config = _c.pointsReward, successMessageConfig = _c.successMessage;
var checkPoints = useAppSelector(selectPosCheckout).points;
var _d = 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 = __spreadArray(__spreadArray([], otherItems, true), [updatedItem], false);
dispatch(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 ? __spreadArray(__spreadArray([], otherItems, true), [updatedItem], false) : otherItems;
dispatch(setPoints(updatedPoints));
// dispatch(validatePosOrder())
if (callback)
callback();
};
var renderQuantity = function (quantity, incrementDisabled) { return (React.createElement(QuantityContainer, { 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
});
};
export default PointsReward;