import { Components, registerComponent, withMutation, withMessages } from 'meteor/vulcan:core'; import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from 'meteor/vulcan:i18n'; // this component is used as a custom controller in user's account edit (cf. ./custom_fields.js) class NewsletterSubscribe extends PureComponent { // check if fields is true or falsy (no value = not subscribed) isSubscribed = () => { return !!this.props.value; } subscribeUnsubscribe = async () => { const { path, updateCurrentValues, throwError } = this.props; const user = this.props.document; const mutationName = this.isSubscribed() ? 'removeUserNewsletter' : 'addUserNewsletter'; const mutation = this.props[mutationName]; try { await mutation({userId: user._id}); updateCurrentValues({ [path]: !this.isSubscribed() }); // display a nice message to the client this.props.flash({ id: 'newsletter.subscription_updated', type: 'success'}); } catch(error) { throwError(error); } } render() { return (