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';
|
2016-11-24 08:27:12 -06:00
|
|
|
import { intlShape } from 'react-intl';
|
2016-06-03 11:03:36 +09:00
|
|
|
|
2016-07-31 14:14:51 +02:00
|
|
|
// 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 {
|
2016-07-31 14:14:51 +02:00
|
|
|
|
2017-01-19 10:37:42 +01:00
|
|
|
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
|
2017-01-19 10:37:42 +01:00
|
|
|
componentDidMount() {
|
|
|
|
// note: forced boolean value because SmartForm's falsy value are empty double quotes.
|
2016-07-31 14:14:51 +02:00
|
|
|
this.context.addToAutofilledValues({[this.props.name]: !!this.props.value});
|
|
|
|
}
|
2017-01-19 10:37:42 +01:00
|
|
|
|
|
|
|
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');
|
2017-01-19 10:37:42 +01:00
|
|
|
} 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.');
|
2016-07-31 14:14:51 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-19 10:37:42 +01:00
|
|
|
|
2016-07-31 14:14:51 +02:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="form-group row">
|
2016-11-24 05:56:02 -06:00
|
|
|
<label className="control-label col-sm-3"></label>
|
2016-07-31 14:14:51 +02:00
|
|
|
<div className="col-sm-9">
|
2016-12-08 23:48:16 +01:00
|
|
|
<Components.NewsletterButton
|
2017-01-19 10:37:42 +01:00
|
|
|
label={this.state.label}
|
|
|
|
mutationName={this.state.mutationName}
|
2016-11-18 11:35:48 -06:00
|
|
|
user={this.props.document}
|
2017-01-19 10:37:42 +01:00
|
|
|
successCallback={this.handleSuccessCallback}
|
2016-07-31 14:14:51 +02:00
|
|
|
/>
|
|
|
|
</div>
|
2016-06-03 11:03:36 +09:00
|
|
|
</div>
|
2016-07-31 14:14:51 +02:00
|
|
|
)
|
|
|
|
}
|
2016-06-03 11:03:36 +09:00
|
|
|
}
|
|
|
|
|
2016-07-21 00:38:49 +02:00
|
|
|
NewsletterSubscribe.contextTypes = {
|
2017-05-19 14:42:43 -06:00
|
|
|
addToAutofilledValues: PropTypes.func,
|
2016-11-24 08:27:12 -06:00
|
|
|
intl: intlShape
|
2016-07-21 00:38:49 +02:00
|
|
|
};
|
|
|
|
|
2016-12-08 09:42:50 +01:00
|
|
|
module.exports = withMessages(NewsletterSubscribe);
|
2016-12-08 23:48:16 +01:00
|
|
|
export default withMessages(NewsletterSubscribe);
|