@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
47 lines (46 loc) • 2.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var react_1 = tslib_1.__importStar(require("react"));
var hooks_1 = require("../app/hooks");
var slices_1 = require("../slices");
var Applied_1 = tslib_1.__importDefault(require("./Applied"));
var Reward = function (_a) {
var _b;
var reward = _a.reward, isLast = _a.isLast, index = _a.index, callback = _a.callback, children = _a.children, AppliedView = _a.AppliedView;
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.reward, cardConfig = _c.card;
var check = (0, hooks_1.useAppSelector)(slices_1.selectPosCheckout).check;
var discounts = check === null || check === void 0 ? void 0 : check.discounts;
var id = reward.discount_id;
var isApplied = (_b = discounts === null || discounts === void 0 ? void 0 : discounts.some(function (discount) {
return discount.id === id && discount.index === index;
})) !== null && _b !== void 0 ? _b : false;
var apply = function () {
dispatch((0, slices_1.addDiscount)({ id: id, index: index }));
if (callback)
callback();
};
var remove = function () {
dispatch((0, slices_1.removeDiscount)({ id: id, index: index }));
if (callback)
callback();
};
var handlers = { apply: apply, remove: remove };
var renderApplied = (0, react_1.useCallback)(function () { return react_1.default.createElement(Applied_1.default, { children: AppliedView }); }, [AppliedView]);
if (!config)
return null;
return children({
config: config,
handlers: handlers,
reward: reward,
isApplied: isApplied,
index: index,
isLast: isLast,
cardConfig: cardConfig,
renderApplied: renderApplied,
apiUrl: apiUrl
});
};
exports.default = Reward;