UNPKG

@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

44 lines (43 loc) 1.73 kB
import { __assign } from "tslib"; import { useAppSelector } from '../app/hooks'; import { selectCustomer, selectCustomerIdentified, selectPosCheckout } from '../slices'; var useCustomerRewards = function () { var customer = useAppSelector(selectCustomerIdentified); var _a = useAppSelector(selectCustomer), rewards = _a.rewards, deals = _a.deals; var _b = useAppSelector(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 (__assign(__assign({}, i), { points: i.points ? __assign(__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 }; }; export default useCustomerRewards;