@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
42 lines (41 loc) • 1.88 kB
JavaScript
import { __assign, __rest } from "tslib";
import React, { useCallback } from 'react';
import { useAppDispatch, useAppSelector } from '../app/hooks';
import { addDiscount, removeDiscount, selectKioskApi, selectKioskConfig, selectPosCheckout } from '../slices';
import { default as AppliedContainer } from './Applied';
var LoyaltyDiscount = function (_a) {
var discount = _a.discount, callback = _a.callback, isLast = _a.isLast, children = _a.children, AppliedView = _a.AppliedView;
var dispatch = useAppDispatch();
var apiUrl = useAppSelector(selectKioskApi);
var _b = useAppSelector(selectKioskConfig), config = _b.reward, cardConfig = _b.card;
var check = useAppSelector(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 = __rest(discount, ["discount_id"]);
dispatch(addDiscount(__assign({ id: discount_id }, rest)));
if (callback)
callback();
};
var remove = function () {
dispatch(removeDiscount({ id: id }));
if (callback)
callback();
};
var handlers = { apply: apply, remove: remove };
var renderApplied = useCallback(function () { return React.createElement(AppliedContainer, { 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
});
};
export default LoyaltyDiscount;