UNPKG

@shopgate/engage

Version:
10 lines 3.32 kB
import React,{useMemo,useCallback}from'react';import PropTypes from'prop-types';import throttle from'lodash/throttle';import classNames from'classnames';import{Grid,I18n,Button,Modal}from'@shopgate/engage/components';import{appConfig}from'@shopgate/engage';import pushImage from"./push-opt-in.svg";import styles from"./style";import connect from"./connector";/** * The Push opt-in modal component. * @param {Object} props The component props. * @returns {JSX.Element} */var PushOptInModal=function PushOptInModal(_ref){var isPushOptInModalVisible=_ref.isPushOptInModalVisible,allowPushOptIn=_ref.allowPushOptIn,denyPushOptIn=_ref.denyPushOptIn;var _appConfig$pushOptIn=appConfig.pushOptIn,_appConfig$pushOptIn2=_appConfig$pushOptIn===void 0?{}:_appConfig$pushOptIn,modalMessage=_appConfig$pushOptIn2.modalMessage,modalTitle=_appConfig$pushOptIn2.modalTitle,modalButtonDeny=_appConfig$pushOptIn2.modalButtonDeny,modalButtonAllow=_appConfig$pushOptIn2.modalButtonAllow,modalImageURL=_appConfig$pushOptIn2.modalImageURL,modalImageSVG=_appConfig$pushOptIn2.modalImageSVG;var imageSRC=useMemo(function(){if(!modalImageURL&&!modalImageSVG){return pushImage;}if(modalImageURL){return modalImageURL;}// SVG overwrite configured -> create data url try{// encode SVG string to UTF-8 byte array to handle non-Latin1 characters // (e.g. Unicode characters like emojis) var utf8Encoder=new TextEncoder();var svgBytes=utf8Encoder.encode(modalImageSVG);// Convert the byte array to a Base64 string var base64Svg=btoa(String.fromCharCode.apply(null,svgBytes));return"data:image/svg+xml;base64,".concat(base64Svg);}catch(e){return pushImage;}},[modalImageSVG,modalImageURL]);// Button event handlers are throttled to prevent multiple clicks var handleAllowPushOptIn=useCallback(throttle(allowPushOptIn,1000,{leading:true,trailing:false}),[]);var handleDenyPushOptIn=useCallback(throttle(denyPushOptIn,1000,{leading:true,trailing:false}),[]);if(!isPushOptInModalVisible){return null;}return React.createElement(Modal,{classes:{content:styles.modalContent,layout:styles.modalLayout}},React.createElement(Grid,{className:classNames(styles.container,'push-opt-in-modal__container'),role:"alertdialog","aria-modal":true,"aria-labelledby":"pushOptInDialogTitle","aria-describedby":"pushOptInDialogMessage"},React.createElement(Grid.Item,{className:styles.item},React.createElement("img",{src:imageSRC,className:classNames(styles.image,'push-opt-in-modal__image'),alt:"","aria-hidden":"true"}),React.createElement(I18n.Text,{className:classNames(styles.title,'push-opt-in-modal__title'),string:modalTitle||'pushOptInModal.title',id:"pushOptInDialogTitle"}),React.createElement(I18n.Text,{className:classNames('push-opt-in-modal__message'),string:modalMessage||'pushOptInModal.message',id:"pushOptInDialogMessage"}),React.createElement(Button,{onClick:handleAllowPushOptIn,type:"primary",className:classNames(styles.button,'push-opt-in-modal__button-allow')},React.createElement(I18n.Text,{string:modalButtonAllow||'pushOptInModal.buttonAllow'})),React.createElement(Button,{onClick:handleDenyPushOptIn,type:"plain",className:classNames(styles.button,'push-opt-in-modal__button-deny')},React.createElement(I18n.Text,{string:modalButtonDeny||'pushOptInModal.buttonDeny',className:styles.buttonText})))));};export default connect(PushOptInModal);