2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
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-10-31 17:13:14 +01:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-11-15 18:33:16 +01:00
|
|
|
import { withCurrentUser } from 'meteor/nova:core';
|
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);
|
|
|
|
|
2016-05-25 08:52:04 +02:00
|
|
|
const showBanner =
|
2016-05-26 10:11:39 +02:00
|
|
|
Cookie.load('showBanner') !== "no" &&
|
2016-11-15 18:33:16 +01:00
|
|
|
!Users.getSetting(props.currentUser, 'newsletter_subscribeToNewsletter', false);
|
2016-03-27 17:30:28 +09:00
|
|
|
this.state = {
|
|
|
|
showBanner: showBanner
|
|
|
|
};
|
2016-03-18 11:53:46 +09:00
|
|
|
}
|
|
|
|
|
2016-05-26 10:11:39 +02:00
|
|
|
componentWillReceiveProps(nextProps, nextContext) {
|
|
|
|
if (nextContext.currentUser) {
|
|
|
|
const showBanner =
|
|
|
|
Cookie.load('showBanner') !== "no" &&
|
|
|
|
!Users.getSetting(nextContext.currentUser, 'newsletter_subscribeToNewsletter', false);
|
|
|
|
|
|
|
|
this.setState({showBanner});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 11:53:46 +09:00
|
|
|
subscribeEmail(data) {
|
2016-06-14 17:03:35 +09:00
|
|
|
this.context.actions.call("newsletter.addEmail", data.email, (error, result) => {
|
2016-05-25 08:57:12 +02:00
|
|
|
if (error) {
|
|
|
|
console.log(error);
|
2016-10-31 17:13:14 +01:00
|
|
|
this.props.flash(error.message, "error");
|
2016-05-25 08:57:12 +02:00
|
|
|
} else {
|
|
|
|
this.successCallbackSubscription(result);
|
|
|
|
}
|
|
|
|
});
|
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});
|
|
|
|
|
2016-05-26 10:11:39 +02:00
|
|
|
// set cookie
|
|
|
|
Cookie.save('showBanner', "no");
|
2016-03-27 17:30:28 +09:00
|
|
|
}
|
|
|
|
|
2016-06-17 11:14:49 +02:00
|
|
|
renderButton() {
|
|
|
|
return <Telescope.components.NewsletterButton
|
2016-07-21 00:38:49 +02:00
|
|
|
successCallback={() => this.successCallbackSubscription()}
|
2016-06-17 11:14:49 +02:00
|
|
|
subscribeText={this.context.intl.formatMessage({id: "newsletter.subscribe"})}
|
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-03-27 17:30:28 +09:00
|
|
|
|
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-05-22 16:42:24 +09:00
|
|
|
<a onClick={this.dismissBanner} className="newsletter-close"><Telescope.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-10-31 17:13:14 +01:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages, });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
2016-11-17 20:00:20 +01:00
|
|
|
|
|
|
|
// register the component directly in the file
|
|
|
|
Telescope.registerComponent(
|
|
|
|
// name of the component
|
|
|
|
'Newsletter',
|
|
|
|
// original component
|
|
|
|
Newsletter,
|
|
|
|
// higher order components enhancing the original component
|
|
|
|
withCurrentUser,
|
|
|
|
connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
);
|