Vulcan/packages/vulcan-newsletter/lib/components/NewsletterSubscribe.jsx

73 lines
2.7 KiB
React
Raw Normal View History

2017-03-23 16:27:59 +09:00
import { Components, withMessages } from 'meteor/vulcan:core';
2017-05-19 14:42:43 -06:00
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
2017-06-01 11:42:30 +09:00
import { intlShape } from 'meteor/vulcan:i18n';
// this component is used as a custom controller in user's account edit (cf. ./custom_fields.js)
2017-05-19 14:42:43 -06:00
class NewsletterSubscribe extends PureComponent {
constructor(props) {
super(props);
this.handleSuccessCallback = this.handleSuccessCallback.bind(this);
// note: we double bang (!!) the value to force a boolean (undefined/"" transformed to false)
this.state = {
label: !!props.value ? 'newsletter.unsubscribe' : 'newsletter.subscribe',
mutationName: !!props.value ? 'removeUserNewsletter' : 'addUserNewsletter',
currentValue: !!props.value,
};
}
2016-12-20 09:27:16 +09:00
// initiate SmartForm with the newsletter setting value
componentDidMount() {
// note: forced boolean value because SmartForm's falsy value are empty double quotes.
this.context.addToAutofilledValues({[this.props.name]: !!this.props.value});
}
handleSuccessCallback(result) {
try {
this.setState(
// update the state of this component
(prevState, props) => ({
label: !prevState.currentValue ? 'newsletter.unsubscribe' : 'newsletter.subscribe',
mutationName: !prevState.currentValue ? 'removeUserNewsletter' : 'addUserNewsletter',
currentValue: !prevState.currentValue,
}),
// the asynchronous setState has finished, update the form state related to this field
() => this.context.addToAutofilledValues({[this.props.name]: this.state.currentValue})
);
// display a nice message to the client
2017-05-19 14:42:43 -06:00
this.props.flash(this.context.intl.formatMessage({ id: 'newsletter.subscription_updated' }), 'success');
} catch(e) {
// note: the result didn't have the shape expected, this shouldn't happen unless we change the code of our mailchimp wrapper
2017-05-19 14:42:43 -06:00
this.props.flash('Something went wrong... Please contact the administrator to check the state of your newsletter subscription.');
}
}
render() {
return (
<div className="form-group row">
<label className="control-label col-sm-3"></label>
<div className="col-sm-9">
<Components.NewsletterButton
label={this.state.label}
mutationName={this.state.mutationName}
user={this.props.document}
successCallback={this.handleSuccessCallback}
/>
</div>
</div>
)
}
}
NewsletterSubscribe.contextTypes = {
2017-05-19 14:42:43 -06:00
addToAutofilledValues: PropTypes.func,
intl: intlShape
};
module.exports = withMessages(NewsletterSubscribe);
export default withMessages(NewsletterSubscribe);