@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
46 lines (45 loc) • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var hooks_1 = require("../app/hooks");
var slices_1 = require("../slices");
var useCustomerRewards = function () {
var customer = (0, hooks_1.useAppSelector)(slices_1.selectCustomerIdentified);
var _a = (0, hooks_1.useAppSelector)(slices_1.selectCustomer), rewards = _a.rewards, deals = _a.deals;
var _b = (0, hooks_1.useAppSelector)(slices_1.selectPosCheckout) || {}, check = _b.check, _c = _b.points, checkPoints = _c === void 0 ? [] : _c;
var _d = (check || {}).cart, cart = _d === void 0 ? [] : _d;
if (!customer) {
return {
customer: null,
hasDeals: false,
hasRewards: false,
hasPoints: false,
deals: [],
rewards: [],
items: [],
points: 0,
pointsApplied: 0,
pointsRemaining: 0
};
}
// const rewards = customer.rewards
var points = parseInt(customer.points);
var items = cart
.map(function (i, index) { return (tslib_1.__assign(tslib_1.__assign({}, i), { points: i.points ? tslib_1.__assign(tslib_1.__assign({}, i.points), { index: index }) : null })); })
.filter(function (i) { return i.points && i.points.per < points; });
var pointsApplied = checkPoints.reduce(function (t, i) { return (t += i.points); }, 0);
var pointsRemaining = points - pointsApplied;
return {
customer: customer,
hasDeals: deals.length > 0,
hasRewards: rewards.length > 0,
hasPoints: items.length > 0,
deals: deals,
rewards: rewards,
items: items,
points: points,
pointsApplied: pointsApplied,
pointsRemaining: pointsRemaining
};
};
exports.default = useCustomerRewards;