import React from 'react'; import MailPoet from 'mailpoet'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { Link } from 'react-router-dom'; import Listing from 'listing/listing.jsx'; import { CronMixin, MailerMixin } from 'newsletters/listings/mixins.jsx'; const columns = [ { name: 'subscriber_id', label: MailPoet.I18n.t('subscriber'), sortable: true, }, { name: 'status', label: MailPoet.I18n.t('sendingStatus'), }, { name: 'failureReason', label: MailPoet.I18n.t('failureReason'), }, ]; const messages = { onNoItemsFound: () => MailPoet.I18n.t('noSendingTaskFound'), }; const SendingStatus = (props) => { const newsletterId = props.match.params.id; const [newsletterSubject, setNewsletterSubject] = React.useState(''); React.useEffect(() => { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'newsletters', action: 'get', data: { id: newsletterId, }, }) .done(res => setNewsletterSubject(res.data.subject)) .fail(res => MailPoet.Notice.showApiErrorNotice(res)); }, [newsletterId]); return ( <>

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

); }; SendingStatus.propTypes = { location: PropTypes.shape({ pathname: PropTypes.string, }).isRequired, match: PropTypes.shape({ params: PropTypes.shape({ id: PropTypes.string.isRequired, }).isRequired, }).isRequired, }; const compareProps = (prev, next) => ( prev.location.pathname === next.location.pathname && prev.params.id === next.params.id ); const SendingStatusListing = React.memo(({ location, params }) => (
} getListingItemKey={item => `${item.taskId}-${item.subscriberId}`} columns={columns} messages={messages} auto_refresh sort_by="failed" sort_order="desc" afterGetItems={(state) => { MailerMixin.checkMailerStatus(state); CronMixin.checkCronStatus(state); }} /> ), compareProps); SendingStatusListing.propTypes = { location: PropTypes.shape({ pathname: PropTypes.string, }).isRequired, params: PropTypes.shape({ id: PropTypes.string.isRequired, }).isRequired, }; const StatsLink = ({ newsletterId, newsletterSubject }) => { if (!newsletterId || !newsletterSubject) return null; if (window.mailpoet_premium_active) { return

{ newsletterSubject }

; } return

{newsletterSubject}

; }; StatsLink.propTypes = { newsletterId: PropTypes.string, newsletterSubject: PropTypes.string, }; StatsLink.defaultProps = { newsletterId: null, newsletterSubject: null, }; const ListingItem = ({ error, failed, taskId, processed, email, subscriberId, lastName, firstName, }) => { const resend = () => { MailPoet.Ajax.post({ api_version: window.mailpoet_api_version, endpoint: 'sending_task_subscribers', action: 'resend', data: { taskId, subscriberId }, }) .done(() => window.mailpoet_listing.forceUpdate()) .fail(res => MailPoet.Notice.showApiErrorNotice(res)); }; const rowClasses = classNames( 'manage-column', 'column-primary', 'has-row-actions' ); let status = MailPoet.I18n.t('unprocessed'); if (processed === '1') { if (failed === '1') { status = ( {MailPoet.I18n.t('failed')}
{MailPoet.I18n.t('resend')}
); } else { status = MailPoet.I18n.t('sent'); } } return ( <> { email }

{ `${firstName} ${lastName}` }

{ status } { error } ); }; ListingItem.propTypes = { error: PropTypes.string, email: PropTypes.string.isRequired, failed: PropTypes.string.isRequired, taskId: PropTypes.string.isRequired, lastName: PropTypes.string.isRequired, firstName: PropTypes.string.isRequired, processed: PropTypes.string.isRequired, subscriberId: PropTypes.string.isRequired, }; ListingItem.defaultProps = { error: '', }; export default SendingStatus;