Merge pull request #1515 from mailpoet/in-app-welcome-emails

In app announcement for free welcome emails [MALPOET-1525]
This commit is contained in:
Michelle Shull
2018-09-20 11:29:24 -04:00
committed by GitHub
7 changed files with 136 additions and 24 deletions

View File

@@ -1,25 +1,80 @@
import React from 'react';
import MailPoet from 'mailpoet';
import InAppAnnouncementDot from './in_app_announcement_dot.jsx';
const InAppAnnouncement = (props) => {
if (props.newUser !== null &&
window.mailpoet_is_new_user !== props.newUser
) {
return null;
class InAppAnnouncement extends React.Component {
constructor(props) {
super(props);
this.saveDisplayed = this.saveDisplayed.bind(this);
this.state = {
announcementsSettings: window.mailpoet_in_app_announcements || null,
};
}
if (props.validUntil < (new Date().getTime() / 1000)) {
return null;
saveDisplayed() {
const settings = Object.assign({}, this.state.announcementsSettings);
settings.displayed.push(this.props.showOnlyOnceSlug);
return MailPoet.Ajax.post({
api_version: window.mailpoet_api_version,
endpoint: 'settings',
action: 'set',
data: { in_app_announcements: settings },
}).always(() => (this.setState({ announcementsSettings: settings })));
}
return (
<InAppAnnouncementDot
className={props.className}
width={props.width}
height={props.height}
>
{props.children}
</InAppAnnouncementDot>);
render() {
if (this.props.showToNewUser !== null &&
window.mailpoet_is_new_user !== this.props.showToNewUser
) {
return null;
}
if (this.props.validUntil !== null
&& this.props.validUntil < new Date()
) {
return null;
}
if (this.props.showToPremiumUser !== null &&
window.mailpoet_premium_active !== this.props.showToPremiumUser
) {
return null;
}
if (this.props.showOnlyOnceSlug &&
this.state.announcementsSettings.displayed.includes(this.props.showOnlyOnceSlug)
) {
return null;
}
return (
<InAppAnnouncementDot
className={this.props.className}
width={this.props.width}
height={this.props.height}
onUserTrigger={() => {
if (!this.props.showOnlyOnceSlug) { return; }
this.saveDisplayed();
}}
>
{this.props.children}
</InAppAnnouncementDot>
);
}
}
const validateBooleanWithWindowDependency = (props, propName, componentName, windowProperty) => {
const propValue = props[propName];
if (propValue !== null && propValue !== true && propValue !== false) {
return new Error(`Invalid property in ${componentName}. newUser must be of type boolean`);
}
if (propValue !== null && typeof window[windowProperty] === 'undefined') {
return new Error(
`Missing data for evaluation of ${componentName} display condition. ${propName} requires window.${windowProperty}`
);
}
return null;
};
InAppAnnouncement.propTypes = {
@@ -27,14 +82,27 @@ InAppAnnouncement.propTypes = {
height: React.PropTypes.string,
className: React.PropTypes.string,
children: React.PropTypes.element.isRequired,
validUntil: React.PropTypes.number,
newUser: (props, propName, componentName) => {
validUntil: React.PropTypes.instanceOf(Date),
showToNewUser: (props, propName, componentName) => (
validateBooleanWithWindowDependency(props, propName, componentName, 'mailpoet_is_new_user')
),
showToPremiumUser: (props, propName, componentName) => (
validateBooleanWithWindowDependency(props, propName, componentName, 'mailpoet_premium_active')
),
showOnlyOnceSlug: (props, propName, componentName) => {
const propValue = props[propName];
if (propValue !== null && propValue !== true && propValue !== false) {
return new Error(`Invalid property in ${componentName}. newUser must be of type boolean`);
if (propValue !== null && typeof propValue !== 'string') {
return new Error(`Invalid property in ${componentName}. ${propName} must be of type string`);
}
if (typeof window.mailpoet_is_new_user === 'undefined') {
return new Error(`Missing data for evaluation of ${componentName} display condition. ${propName} requires window.mailpoet_is_new_user`);
if (propValue === null) {
return null;
}
if (
typeof window.mailpoet_in_app_announcements === 'undefined'
) {
return new Error(
`Missing data for evaluation of ${componentName} display condition. ${propName} requires window.mailpoet_in_app_announcements`
);
}
return null;
},
@@ -45,7 +113,9 @@ InAppAnnouncement.defaultProps = {
height: '600px',
className: null,
validUntil: null,
newUser: null,
showToNewUser: null,
showToPremiumUser: null,
showOnlyOnceSlug: null,
};
module.exports = InAppAnnouncement;

View File

@@ -14,21 +14,24 @@ const InAppAnnouncementDot = props => (
width: props.width,
height: props.height,
});
if (props.onUserTrigger) props.onUserTrigger();
}}
/>
);
InAppAnnouncementDot.propTypes = {
children: React.PropTypes.element.isRequired,
width: React.PropTypes.string,
height: React.PropTypes.string,
className: React.PropTypes.string,
children: React.PropTypes.element.isRequired,
onUserTrigger: React.PropTypes.func,
};
InAppAnnouncementDot.defaultProps = {
width: 'auto',
height: 'auto',
className: null,
onUserTrigger: null,
};
module.exports = InAppAnnouncementDot;

View File

@@ -1,6 +1,7 @@
import React from 'react';
import { Link } from 'react-router';
import MailPoet from 'mailpoet';
import InAppAnnoucement from 'in_app_announcements/in_app_announcement.jsx';
const ListingHeading = () => (
<h1 className="title">
@@ -17,6 +18,23 @@ const ListingHeading = () => (
>
{MailPoet.I18n.t('new')}
</Link>
<InAppAnnoucement
className="mailpoet_in_app_announcement_free_welcome_emails_dot"
showToNewUser={false}
showToPremiumUser={false}
showOnlyOnceSlug="free_welcome_emails"
height="650px"
validUntil={new Date('2018-10-31')}
>
<div className="mailpoet_in_app_announcement_free_welcome_emails">
<h2>{MailPoet.I18n.t('freeWelcomeEmailsHeading')}</h2>
<img
src={window.mailpoet_free_welcome_emails_image}
alt={MailPoet.I18n.t('freeWelcomeEmailsHeading')}
/>
<p>{MailPoet.I18n.t('freeWelcomeEmailsParagraph')}</p>
</div>
</InAppAnnoucement>
</h1>
);