Vulcan/packages/nova-base-components/lib/common/NewsletterButton.jsx

71 lines
2.2 KiB
React
Raw Normal View History

import { Components, registerComponent } from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
2016-06-09 20:26:33 +09:00
import { FormattedMessage } from 'react-intl';
import { Button } from 'react-bootstrap';
import { withMutation, withCurrentUser, withMessages } from 'meteor/nova:core';
class NewsletterButton extends Component {
constructor(props) {
super(props);
this.subscriptionAction = this.subscriptionAction.bind(this);
const isSubscribed = props.user.newsletter_subscribeToNewsletter;
this.state = {
labelId: isSubscribed ? 'newsletter.unsubscribe' : 'newsletter.subscribe',
action: isSubscribed ? 'removeUserNewsletter' : 'addUserNewsletter'
};
}
2016-05-26 10:46:30 +02:00
subscriptionAction() {
const action = this.state.action;
this.props[action]({userId: this.props.user._id}).then(result => {
this.props.successCallback(result);
// note: cannot update state, the component is unmounted when we try to update it
// console.log(result);
// if (result.data[action].actionResult === 'subscribed') {
// this.setState({
// labelId: 'newsletter.unsubscribe',
// action: 'removeUserNewsletter',
// });
// } else {
// this.setState({
// labelId: 'newsletter.subscribe',
// action: 'addUserNewsletter',
// });
// }
}).catch(error => {
console.log(error); // eslint-disable-line no-console
this.props.flash(error.message, "error");
2016-05-26 10:46:30 +02:00
});
}
render() {
return (
<Button
className="newsletter-button"
2016-05-26 10:46:30 +02:00
onClick={this.subscriptionAction}
bsStyle="primary"
>
<FormattedMessage id={this.state.labelId}/>
</Button>
)
}
}
NewsletterButton.propTypes = {
user: React.PropTypes.object.isRequired,
successCallback: React.PropTypes.func.isRequired,
};
NewsletterButton.contextTypes = {
actions: React.PropTypes.object,
2016-11-15 18:33:16 +01:00
};
const addOptions = {name: 'addUserNewsletter', args: {userId: 'String'}};
const removeOptions = {name: 'removeUserNewsletter', args: {userId: 'String'}};
registerComponent('NewsletterButton', NewsletterButton, withCurrentUser, withMutation(addOptions), withMutation(removeOptions), withMessages);