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.85 kB
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 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 = useAppDispatch(); var apiUrl = useAppSelector(selectKioskApi); var _c = useAppSelector(selectKioskConfig), config = _c.reward, cardConfig = _c.card; var check = useAppSelector(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(addDiscount({ id: id, index: index })); if (callback) callback(); }; var remove = function () { dispatch(removeDiscount({ id: id, index: index })); 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, reward: reward, isApplied: isApplied, index: index, isLast: isLast, cardConfig: cardConfig, renderApplied: renderApplied, apiUrl: apiUrl }); }; export default Reward;