2016-12-08 09:42:50 +01:00
|
|
|
import { Components, registerComponent } from 'meteor/nova:lib';
|
2016-05-25 08:52:04 +02:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-06-09 20:26:33 +09:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2016-05-25 08:52:04 +02:00
|
|
|
import { Button } from 'react-bootstrap';
|
2016-12-08 09:42:50 +01:00
|
|
|
import { withMutation, withCurrentUser, withMessages } from 'meteor/nova:core';
|
2016-05-25 08:52:04 +02:00
|
|
|
|
|
|
|
class NewsletterButton extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-05-25 08:57:12 +02:00
|
|
|
this.subscriptionAction = this.subscriptionAction.bind(this);
|
2016-05-25 08:52:04 +02:00
|
|
|
}
|
2017-01-19 10:37:42 +01:00
|
|
|
|
|
|
|
// use async/await + try/catch <=> promise.then(res => ..).catch(e => ...)
|
|
|
|
async subscriptionAction() {
|
|
|
|
|
|
|
|
const {
|
|
|
|
flash,
|
|
|
|
mutationName,
|
|
|
|
successCallback,
|
|
|
|
user,
|
|
|
|
[mutationName]: mutationToTrigger, // dynamic 'mutationToTrigger' variable based on the mutationName (addUserNewsletter or removeUserNewsletter)
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const mutationResult = await mutationToTrigger({userId: user._id});
|
|
|
|
|
|
|
|
successCallback(mutationResult);
|
|
|
|
} catch(error) {
|
|
|
|
console.error(error); // eslint-disable-line no-console
|
|
|
|
|
|
|
|
flash(error.message, "error");
|
|
|
|
}
|
2016-05-25 08:52:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-12-27 11:44:35 +01:00
|
|
|
|
2016-05-25 08:52:04 +02:00
|
|
|
return (
|
2016-05-25 08:57:12 +02:00
|
|
|
<Button
|
|
|
|
className="newsletter-button"
|
2016-05-26 10:46:30 +02:00
|
|
|
onClick={this.subscriptionAction}
|
2016-05-25 08:57:12 +02:00
|
|
|
bsStyle="primary"
|
|
|
|
>
|
2017-01-19 10:37:42 +01:00
|
|
|
<FormattedMessage id={this.props.label}/>
|
2016-05-25 08:52:04 +02:00
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NewsletterButton.propTypes = {
|
2017-01-19 10:37:42 +01:00
|
|
|
mutationName: PropTypes.string.isRequired, // mutation to fire
|
|
|
|
label: PropTypes.string.isRequired, // label of the button
|
|
|
|
user: PropTypes.object.isRequired, // user to operate on
|
|
|
|
successCallback: PropTypes.func.isRequired, // what do to after the mutationName
|
|
|
|
addUserNewsletter: PropTypes.func.isRequired, // prop given by withMutation HOC
|
|
|
|
removeUserNewsletter: PropTypes.func.isRequired, // prop given by withMutation HOC
|
2016-11-15 18:33:16 +01:00
|
|
|
};
|
2016-06-03 11:03:36 +09:00
|
|
|
|
2016-12-07 15:30:40 +09:00
|
|
|
const addOptions = {name: 'addUserNewsletter', args: {userId: 'String'}};
|
|
|
|
const removeOptions = {name: 'removeUserNewsletter', args: {userId: 'String'}};
|
|
|
|
|
2016-12-08 23:48:16 +01:00
|
|
|
registerComponent('NewsletterButton', NewsletterButton, withCurrentUser, withMutation(addOptions), withMutation(removeOptions), withMessages);
|