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) 2.01 kB
"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 LoyaltyDiscount = function (_a) { var discount = _a.discount, callback = _a.callback, isLast = _a.isLast, children = _a.children, AppliedView = _a.AppliedView; var dispatch = (0, hooks_1.useAppDispatch)(); var apiUrl = (0, hooks_1.useAppSelector)(slices_1.selectKioskApi); var _b = (0, hooks_1.useAppSelector)(slices_1.selectKioskConfig), config = _b.reward, cardConfig = _b.card; var check = (0, hooks_1.useAppSelector)(slices_1.selectPosCheckout).check; var discountIds = check === null || check === void 0 ? void 0 : check.discounts.map(function (i) { return i.id; }); var id = discount.discount_id; var isApplied = !!(discountIds === null || discountIds === void 0 ? void 0 : discountIds.includes(id)); var apply = function () { var discount_id = discount.discount_id, rest = tslib_1.__rest(discount, ["discount_id"]); dispatch((0, slices_1.addDiscount)(tslib_1.__assign({ id: discount_id }, rest))); if (callback) callback(); }; var remove = function () { dispatch((0, slices_1.removeDiscount)({ id: id })); 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, discount: discount, isApplied: isApplied, isLast: isLast, cardConfig: cardConfig, renderApplied: renderApplied, apiUrl: apiUrl }); }; exports.default = LoyaltyDiscount;