2017-02-11 12:27:32 +01:00
|
|
|
import { Components, registerComponent, withCurrentUser, withMutation, withMessages, Utils } from 'meteor/nova:core';
|
2016-03-18 11:53:46 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-06-10 10:25:38 +09:00
|
|
|
import { FormattedMessage, intlShape } from 'react-intl';
|
2016-03-18 11:53:46 +09:00
|
|
|
import Formsy from 'formsy-react';
|
2016-05-25 08:52:04 +02:00
|
|
|
import { Input } from 'formsy-react-components';
|
2016-03-24 10:56:47 +09:00
|
|
|
import { Button } from 'react-bootstrap';
|
2016-05-26 10:11:39 +02:00
|
|
|
import Cookie from 'react-cookie';
|
2016-06-23 15:00:58 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-03-27 17:30:28 +09:00
|
|
|
|
2016-04-19 15:45:36 +09:00
|
|
|
class Newsletter extends Component {
|
2016-03-18 11:53:46 +09:00
|
|
|
|
2016-03-27 17:30:28 +09:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props);
|
2016-03-18 11:53:46 +09:00
|
|
|
this.subscribeEmail = this.subscribeEmail.bind(this);
|
2016-05-25 08:57:12 +02:00
|
|
|
this.successCallbackSubscription = this.successCallbackSubscription.bind(this);
|
2016-03-27 17:30:28 +09:00
|
|
|
this.dismissBanner = this.dismissBanner.bind(this);
|
|
|
|
|
|
|
|
this.state = {
|
2016-12-08 23:48:16 +01:00
|
|
|
showBanner: showBanner(props.currentUser)
|
2016-03-27 17:30:28 +09:00
|
|
|
};
|
2016-03-18 11:53:46 +09:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:11:39 +02:00
|
|
|
componentWillReceiveProps(nextProps, nextContext) {
|
2016-12-08 23:48:16 +01:00
|
|
|
if (nextProps.currentUser) {
|
|
|
|
this.setState({showBanner: showBanner(nextProps.currentUser)});
|
2016-05-26 10:11:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 16:18:33 +01:00
|
|
|
async subscribeEmail(data) {
|
|
|
|
try {
|
|
|
|
const result = await this.props.addEmailNewsletter({email: data.email});
|
2016-12-07 15:30:40 +09:00
|
|
|
this.successCallbackSubscription(result);
|
2017-02-02 16:18:33 +01:00
|
|
|
} catch(error) {
|
2017-02-11 12:27:32 +01:00
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
this.props.flash(
|
|
|
|
this.context.intl.formatMessage(Utils.decodeIntlError(error.message)),
|
|
|
|
"error"
|
|
|
|
);
|
2017-02-02 16:18:33 +01:00
|
|
|
}
|
2016-03-18 11:53:46 +09:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:57:12 +02:00
|
|
|
successCallbackSubscription(result) {
|
2016-10-31 17:13:14 +01:00
|
|
|
this.props.flash(this.context.intl.formatMessage({id: "newsletter.success_message"}), "success");
|
2016-05-25 08:57:12 +02:00
|
|
|
this.dismissBanner();
|
2016-03-18 11:53:46 +09:00
|
|
|
}
|
|
|
|
|
2016-03-27 17:30:28 +09:00
|
|
|
dismissBanner(e) {
|
|
|
|
if (e && e.preventDefault) e.preventDefault();
|
|
|
|
|
|
|
|
this.setState({showBanner: false});
|
|
|
|
|
2017-01-19 10:37:42 +01:00
|
|
|
// set cookie to keep the banner dismissed persistently
|
2016-05-26 10:11:39 +02:00
|
|
|
Cookie.save('showBanner', "no");
|
2016-03-27 17:30:28 +09:00
|
|
|
}
|
|
|
|
|
2016-06-17 11:14:49 +02:00
|
|
|
renderButton() {
|
2016-12-06 18:06:29 +01:00
|
|
|
return <Components.NewsletterButton
|
2017-01-19 10:37:42 +01:00
|
|
|
label="newsletter.subscribe"
|
|
|
|
mutationName="addUserNewsletter"
|
2016-07-21 00:38:49 +02:00
|
|
|
successCallback={() => this.successCallbackSubscription()}
|
2016-11-15 18:33:16 +01:00
|
|
|
user={this.props.currentUser}
|
2016-06-17 11:14:49 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
|
2016-03-18 11:53:46 +09:00
|
|
|
renderForm() {
|
|
|
|
return (
|
2016-03-25 10:45:28 +09:00
|
|
|
<Formsy.Form className="newsletter-form" onSubmit={this.subscribeEmail}>
|
2016-03-18 11:53:46 +09:00
|
|
|
<Input
|
|
|
|
name="email"
|
|
|
|
value=""
|
2016-06-10 10:25:38 +09:00
|
|
|
placeholder={this.context.intl.formatMessage({id: "newsletter.email"})}
|
2016-03-18 11:53:46 +09:00
|
|
|
type="text"
|
2016-03-25 10:45:28 +09:00
|
|
|
layout="elementOnly"
|
2016-03-18 11:53:46 +09:00
|
|
|
/>
|
2016-06-10 10:25:38 +09:00
|
|
|
<Button className="newsletter-button" type="submit" bsStyle="primary"><FormattedMessage id="newsletter.subscribe"/></Button>
|
2016-03-18 11:53:46 +09:00
|
|
|
</Formsy.Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-05-25 08:52:04 +02:00
|
|
|
return this.state.showBanner
|
|
|
|
? (
|
2016-03-18 11:53:46 +09:00
|
|
|
<div className="newsletter">
|
2016-06-10 10:25:38 +09:00
|
|
|
<h4 className="newsletter-tagline"><FormattedMessage id="newsletter.subscribe_prompt"/></h4>
|
2016-11-15 18:33:16 +01:00
|
|
|
{this.props.currentUser ? this.renderButton() : this.renderForm()}
|
2016-12-06 18:06:29 +01:00
|
|
|
<a onClick={this.dismissBanner} className="newsletter-close"><Components.Icon name="close"/></a>
|
2016-03-18 11:53:46 +09:00
|
|
|
</div>
|
2016-05-25 08:52:04 +02:00
|
|
|
) : null;
|
2016-03-18 11:53:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:45:36 +09:00
|
|
|
Newsletter.contextTypes = {
|
2016-06-14 17:03:35 +09:00
|
|
|
actions: React.PropTypes.object,
|
2016-06-10 10:25:38 +09:00
|
|
|
intl: intlShape
|
2016-03-25 10:45:28 +09:00
|
|
|
};
|
|
|
|
|
2016-12-07 15:30:40 +09:00
|
|
|
const mutationOptions = {
|
|
|
|
name: 'addEmailNewsletter',
|
|
|
|
args: {email: 'String'}
|
|
|
|
}
|
2016-12-08 23:48:16 +01:00
|
|
|
|
2016-11-17 11:37:26 -06:00
|
|
|
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)
|
2017-01-18 10:18:33 +09:00
|
|
|
&& !Users.getSetting(user, 'newsletter_subscribeToNewsletter', false)
|
2016-11-17 11:37:26 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-08 23:48:16 +01:00
|
|
|
registerComponent('Newsletter', Newsletter, withMutation(mutationOptions), withCurrentUser, withMessages);
|