import React from 'react'; import MailPoet from 'mailpoet'; import Button from 'common/button/button'; import { useAction, useSelector } from 'settings/store/hooks'; import { GlobalContext } from 'context'; import ReactStringReplace from 'react-string-replace'; const showReEngagementNotice = (action, showError, showSuccess) => { if (action === 'deactivate') { showError(

{MailPoet.I18n.t('re-engagementDisabledNotice')}

, { scroll: true }); return; } if (action === 'reactivate') { const reEngagementReactivatedNotice = ReactStringReplace( MailPoet.I18n.t('re-engagementReactivatedNotice'), /\[link\](.*?)\[\/link\]/g, (match) => ( {match} ) ); showSuccess(

{reEngagementReactivatedNotice}

, { scroll: true }); } }; export default () => { const [clicked, setClicked] = React.useState(false); const isSaving = useSelector('isSaving')(); const hasError = useSelector('hasErrorFlag')(); const error = useSelector('getSavingError')(); const hasReEngagementNotice = useSelector('hasReEngagementNotice')(); const reEngagementAction = useSelector('getReEngagementAction')(); const save = useAction('saveSettings'); // eslint-disable-next-line @typescript-eslint/no-explicit-any const { notices } = React.useContext(GlobalContext); const showError = notices.error; const showSuccess = notices.success; React.useEffect(() => { if (clicked && !isSaving) { if (error) showError(error.map((err) =>

{err}

), { scroll: true }); else { showSuccess(

{MailPoet.I18n.t('settingsSaved')}

, { scroll: true }); if (hasReEngagementNotice) { showReEngagementNotice(reEngagementAction, showError, showSuccess); } } } }, [clicked, error, isSaving, showError, showSuccess, hasReEngagementNotice, reEngagementAction]); const onClick = () => { setClicked(true); save(); }; return (
); };