Refactor Notice component to Typescript

[MAILPOET-2658]
This commit is contained in:
Amine Ben hammou
2020-02-26 14:26:09 +01:00
committed by Jack Kitterhing
parent cbe2fc64bb
commit 25aec60bc9
5 changed files with 24 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Notice from 'notices/notice.jsx'; import Notice from 'notices/notice.tsx';
const APIErrorsNotice = ({ errors }) => { const APIErrorsNotice = ({ errors }) => {
if (errors.length < 1) return null; if (errors.length < 1) return null;

View File

@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Notice from 'notices/notice.jsx'; import Notice from 'notices/notice.tsx';
const MailerStatusNotice = ({ error }) => { const MailerStatusNotice = ({ error }) => {
if (!error || error.operation !== 'authorization') return null; if (!error || error.operation !== 'authorization') return null;

View File

@@ -1,9 +1,24 @@
import React from 'react'; import React, { FC } from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import PropTypes from 'prop-types'; import PropTypes, { InferProps } from 'prop-types';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
const Notice = ({ const propTypes = {
type: PropTypes.oneOf(['success', 'info', 'warning', 'error']).isRequired,
scroll: PropTypes.bool,
closable: PropTypes.bool,
renderInPlace: PropTypes.bool,
onDisplay: PropTypes.func,
onClose: PropTypes.func,
timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]),
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
PropTypes.arrayOf(PropTypes.element),
]).isRequired,
};
const Notice: FC<InferProps<typeof propTypes>> = ({
onClose, onClose,
onDisplay, onDisplay,
renderInPlace, renderInPlace,
@@ -24,7 +39,7 @@ const Notice = ({
React.useEffect(() => { React.useEffect(() => {
if (timeout) { if (timeout) {
timeoutRef.current = setTimeout(close, timeout); timeoutRef.current = setTimeout(close, timeout as number);
} }
return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null); return () => (timeoutRef.current ? clearTimeout(timeoutRef.current) : null);
}, [close, timeout]); }, [close, timeout]);
@@ -61,20 +76,7 @@ const Notice = ({
document.getElementById('mailpoet_notices') document.getElementById('mailpoet_notices')
); );
}; };
Notice.propTypes = { Notice.propTypes = propTypes;
type: PropTypes.oneOf(['success', 'info', 'warning', 'error']).isRequired,
scroll: PropTypes.bool,
closable: PropTypes.bool,
renderInPlace: PropTypes.bool,
onDisplay: PropTypes.func,
onClose: PropTypes.func,
timeout: PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf([false])]),
children: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
PropTypes.arrayOf(PropTypes.element),
]).isRequired,
};
Notice.defaultProps = { Notice.defaultProps = {
timeout: 10000, timeout: 10000,
scroll: false, scroll: false,

View File

@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { GlobalContext } from 'context/index.jsx'; import { GlobalContext } from 'context/index.jsx';
import Notice from './notice.jsx'; import Notice from './notice.tsx';
export default () => { export default () => {
const { notices } = React.useContext(GlobalContext); const { notices } = React.useContext(GlobalContext);

View File

@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import MailPoet from 'mailpoet'; import MailPoet from 'mailpoet';
import Notice from 'notices/notice.jsx'; import Notice from 'notices/notice.tsx';
const SubscribersLimitNotice = () => { const SubscribersLimitNotice = () => {
if (!window.mailpoet_subscribers_limit_reached) return null; if (!window.mailpoet_subscribers_limit_reached) return null;