import { Components, registerComponent, withCurrentUser, withMutation, withMessages, Utils } from 'meteor/vulcan:core'; import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage, intlShape } from 'meteor/vulcan:i18n'; import Formsy from 'formsy-react'; import { Input } from 'formsy-react-components'; import Button from 'react-bootstrap/lib/Button'; import Cookie from 'react-cookie'; import Users from 'meteor/vulcan:users'; class Newsletter extends PureComponent { 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) { const graphQLError = error.graphQLErrors[0]; console.error(graphQLError); // eslint-disable-line no-console const message = this.context.intl.formatMessage({id: `newsletter.error_${this.state.error.name}`}, {message: this.state.error.message}); this.props.flash(message, '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: 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);