Render banner near send button

[MAILPOET-2597]
This commit is contained in:
Pavel Dohnal
2020-01-17 08:13:55 +01:00
committed by Jack Kitterhing
parent 89a215a9bf
commit f3f879a11a
3 changed files with 33 additions and 15 deletions

View File

@@ -473,7 +473,6 @@ class NewsletterSend extends React.Component {
return ( return (
<div> <div>
<SubscribersLimitNotice />
<h1>{MailPoet.I18n.t('finalNewsletterStep')}</h1> <h1>{MailPoet.I18n.t('finalNewsletterStep')}</h1>
{breadcrumb} {breadcrumb}
@@ -487,6 +486,7 @@ class NewsletterSend extends React.Component {
onChange={this.handleFormChange} onChange={this.handleFormChange}
onSubmit={this.handleSave} onSubmit={this.handleSave}
> >
<SubscribersLimitNotice />
<p className="submit"> <p className="submit">
{ {
isPaused isPaused

View File

@@ -3,45 +3,61 @@ import ReactDOM from 'react-dom';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
const Notice = (props) => { const Notice = ({
onClose,
onDisplay,
renderInPlace,
timeout,
scroll,
children,
closable,
type,
}) => {
const [hidden, setHidden] = React.useState(false); const [hidden, setHidden] = React.useState(false);
const elementRef = React.useRef(null); const elementRef = React.useRef(null);
const timeoutRef = React.useRef(null); const timeoutRef = React.useRef(null);
const { onClose, onDisplay } = props;
const close = React.useCallback(() => { const close = React.useCallback(() => {
if (onClose) onClose(); if (onClose) onClose();
setHidden(true); setHidden(true);
}, [onClose]); }, [onClose]);
React.useEffect(() => { React.useEffect(() => {
if (props.timeout) { if (timeout) {
timeoutRef.current = setTimeout(close, props.timeout); timeoutRef.current = setTimeout(close, timeout);
} }
return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null); return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null);
}, [close, props.timeout]); }, [close, timeout]);
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
if (props.scroll && elementRef.current) { if (scroll && elementRef.current) {
elementRef.current.scrollIntoView(false); elementRef.current.scrollIntoView(false);
} }
}, [props.scroll]); }, [scroll]);
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
if (onDisplay) onDisplay(); if (onDisplay) onDisplay();
}, [onDisplay]); }, [onDisplay]);
if (hidden) return null; if (hidden) return null;
return ReactDOM.createPortal(
<div ref={elementRef} className={`mailpoet_base_notice mailpoet_${props.type}_notice`}> const content = (
{props.children} <div ref={elementRef} className={`mailpoet_base_notice mailpoet_${type}_notice`}>
{props.closable && ( {children}
{closable && (
<button type="button" className="notice-dismiss" onClick={close}> <button type="button" className="notice-dismiss" onClick={close}>
<span className="screen-reader-text">{MailPoet.I18n.t('dismissNotice')}</span> <span className="screen-reader-text">{MailPoet.I18n.t('dismissNotice')}</span>
</button> </button>
)} )}
</div>, </div>
);
if (renderInPlace) {
return content;
}
return ReactDOM.createPortal(
content,
document.getElementById('mailpoet_notices') document.getElementById('mailpoet_notices')
); );
}; };
@@ -49,6 +65,7 @@ Notice.propTypes = {
type: PropTypes.oneOf(['success', 'info', 'warning', 'error']).isRequired, type: PropTypes.oneOf(['success', 'info', 'warning', 'error']).isRequired,
scroll: PropTypes.bool, scroll: PropTypes.bool,
closable: PropTypes.bool, closable: PropTypes.bool,
renderInPlace: PropTypes.bool,
onDisplay: PropTypes.func, onDisplay: PropTypes.func,
onClose: PropTypes.func, onClose: PropTypes.func,
timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]), timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]),
@@ -62,6 +79,7 @@ Notice.defaultProps = {
timeout: 10000, timeout: 10000,
scroll: false, scroll: false,
closable: true, closable: true,
renderInPlace: false,
onDisplay: undefined, onDisplay: undefined,
onClose: undefined, onClose: undefined,
}; };

View File

@@ -22,7 +22,7 @@ const SubscribersLimitNotice = () => {
}; };
return ( return (
<Notice type="error" timeout={false} closable={false}> <Notice type="error" timeout={false} closable={false} renderInPlace>
<h3>{title}</h3> <h3>{title}</h3>
<p> <p>
{youReachedTheLimit} {youReachedTheLimit}