import { Components, registerComponent, withCurrentUser, withMutation, withMessages, Utils } from 'meteor/nova:core'; import React, { PropTypes, Component } from 'react'; import { FormattedMessage, intlShape } from 'react-intl'; import Formsy from 'formsy-react'; import { Input } from 'formsy-react-components'; import { Button } from 'react-bootstrap'; import Cookie from 'react-cookie'; import Users from 'meteor/nova:users'; class Newsletter extends Component { constructor(props, context) { super(props); this.subscribeEmail = this.subscribeEmail.bind(this); this.successCallbackSubscription = this.successCallbackSubscription.bind(this); this.dismissBanner = this.dismissBanner.bind(this); this.state = { showBanner: showBanner(props.currentUser) }; } componentWillReceiveProps(nextProps, nextContext) { if (nextProps.currentUser) { this.setState({showBanner: showBanner(nextProps.currentUser)}); } } async subscribeEmail(data) { try { const result = await this.props.addEmailNewsletter({email: data.email}); this.successCallbackSubscription(result); } catch(error) { console.error(error); // eslint-disable-line no-console this.props.flash( this.context.intl.formatMessage(Utils.decodeIntlError(error)), "error" ); } } successCallbackSubscription(result) { this.props.flash(this.context.intl.formatMessage({id: "newsletter.success_message"}), "success"); this.dismissBanner(); } dismissBanner(e) { if (e && e.preventDefault) e.preventDefault(); this.setState({showBanner: false}); // set cookie to keep the banner dismissed persistently Cookie.save('showBanner', "no"); } renderButton() { return this.successCallbackSubscription()} user={this.props.currentUser} /> } renderForm() { return ( ) } render() { return this.state.showBanner ? (

{this.props.currentUser ? this.renderButton() : this.renderForm()}
) : null; } } Newsletter.contextTypes = { actions: React.PropTypes.object, intl: intlShape }; const mutationOptions = { name: 'addEmailNewsletter', args: {email: 'String'} } function showBanner (user) { return ( // showBanner cookie either doesn't exist or is not set to "no" Cookie.load('showBanner') !== "no" // and user is not subscribed to the newsletter already (setting either DNE or is not set to false) && !Users.getSetting(user, 'newsletter_subscribeToNewsletter', false) ); } registerComponent('Newsletter', Newsletter, withMutation(mutationOptions), withCurrentUser, withMessages);