Improve the Notice component

[MAILPOET-2390]
This commit is contained in:
Amine Ben hammou
2019-12-12 15:30:55 +01:00
committed by Jack Kitterhing
parent 3fc48006c2
commit 3321ad5d31
5 changed files with 31 additions and 6 deletions

View File

@@ -10,6 +10,7 @@
box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1); box-shadow: 0 1px 1px 0 rgba(0, 0, 0, .1);
margin: 5px 0 15px; margin: 5px 0 15px;
padding: 1px 12px; padding: 1px 12px;
position: relative;
} }
.mailpoet_success_notice { .mailpoet_success_notice {

View File

@@ -4,7 +4,7 @@ import Notice from 'notices/notice.jsx';
const APIErrorsNotice = ({ errors }) => { const APIErrorsNotice = ({ errors }) => {
if (errors.length < 1) return null; if (errors.length < 1) return null;
return <Notice type="error">{errors.map((err) => <p key={err.message}>{err.message}</p>)}</Notice>; return <Notice type="error" closable={false}>{errors.map((err) => <p key={err.message}>{err.message}</p>)}</Notice>;
}; };
APIErrorsNotice.propTypes = { APIErrorsNotice.propTypes = {
errors: PropTypes.arrayOf(PropTypes.shape({ errors: PropTypes.arrayOf(PropTypes.shape({

View File

@@ -4,7 +4,7 @@ import Notice from 'notices/notice.jsx';
const MailerStatusNotice = ({ error }) => { const MailerStatusNotice = ({ error }) => {
if (!error || error.operation !== 'authorization') return null; if (!error || error.operation !== 'authorization') return null;
return <Notice type="error" timeout={false}><p>{error.error_message}</p></Notice>; return <Notice type="error" timeout={false} closable={false}><p>{error.error_message}</p></Notice>;
}; };
MailerStatusNotice.propTypes = { MailerStatusNotice.propTypes = {
error: PropTypes.shape({ error: PropTypes.shape({

View File

@@ -7,12 +7,19 @@ const Notice = (props) => {
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(() => {
if (onClose) onClose();
setHidden(true);
}, [onClose]);
React.useEffect(() => { React.useEffect(() => {
if (props.timeout) { if (props.timeout) {
timeoutRef.current = setTimeout(() => setHidden(true), props.timeout); timeoutRef.current = setTimeout(close, props.timeout);
} }
return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null); return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null);
}, [props.timeout]); }, [close, props.timeout]);
React.useLayoutEffect(() => { React.useLayoutEffect(() => {
if (props.scroll && elementRef.current) { if (props.scroll && elementRef.current) {
@@ -20,15 +27,29 @@ const Notice = (props) => {
} }
}, [props.scroll]); }, [props.scroll]);
React.useLayoutEffect(() => {
if (onDisplay) onDisplay();
}, [onDisplay]);
if (hidden) return null; if (hidden) return null;
return ReactDOM.createPortal( return ReactDOM.createPortal(
<div ref={elementRef} className={`mailpoet_base_notice mailpoet_${props.type}_notice`}>{props.children}</div>, <div ref={elementRef} className={`mailpoet_base_notice mailpoet_${props.type}_notice`}>
{props.children}
{props.closable && (
<button type="button" className="notice-dismiss" onClick={close}>
<span className="screen-reader-text">Dismiss this notice.</span>
</button>
)}
</div>,
document.getElementById('mailpoet_notices') document.getElementById('mailpoet_notices')
); );
}; };
Notice.propTypes = { 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,
onDisplay: PropTypes.func,
onClose: PropTypes.func,
timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]), timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]),
children: PropTypes.oneOfType([ children: PropTypes.oneOfType([
PropTypes.string, PropTypes.string,
@@ -39,6 +60,9 @@ Notice.propTypes = {
Notice.defaultProps = { Notice.defaultProps = {
timeout: 10000, timeout: 10000,
scroll: false, scroll: false,
closable: true,
onDisplay: undefined,
onClose: undefined,
}; };
export default Notice; export default Notice;

View File

@@ -5,7 +5,7 @@ import Notice from 'notices/notice.jsx';
const SubscribersLimitNotice = () => { const SubscribersLimitNotice = () => {
if (!window.mailpoet_subscribers_limit_reached) return null; if (!window.mailpoet_subscribers_limit_reached) return null;
return ( return (
<Notice type="error" timeout={false}> <Notice type="error" timeout={false} closable={false}>
<h3> <h3>
{ {
MailPoet.I18n.t('subscribersLimitNoticeTitle') MailPoet.I18n.t('subscribersLimitNoticeTitle')