/* eslint-disable no-underscore-dangle */ import React from 'react'; import PropTypes from 'prop-types'; import { Row, Col, FormGroup, ControlLabel, Button } from 'react-bootstrap'; import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import { Bert } from 'meteor/themeteorchef:bert'; import { createContainer } from 'meteor/react-meteor-data'; import InputHint from '../../components/InputHint/InputHint'; import validate from '../../../modules/validate'; class Profile extends React.Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); } componentDidMount() { const component = this; validate(component.form, { rules: { firstName: { required: true, }, lastName: { required: true, }, emailAddress: { required: true, email: true, }, currentPassword: { required() { // Only required if newPassword field has a value. return component.newPassword.value.length > 0; }, }, newPassword: { required() { // Only required if currentPassword field has a value. return component.currentPassword.value.length > 0; }, }, }, messages: { firstName: { required: 'What\'s your first name?', }, lastName: { required: 'What\'s your last name?', }, emailAddress: { required: 'Need an email address here.', email: 'Is this email address correct?', }, currentPassword: { required: 'Need your current password if changing.', }, newPassword: { required: 'Need your new password if changing.', }, }, submitHandler() { component.handleSubmit(); }, }); } handleSubmit() { const profile = { emailAddress: this.emailAddress.value, profile: { name: { first: this.firstName.value, last: this.lastName.value, }, }, }; if (this.newPassword.value) profile.password = Accounts._hashPassword(this.newPassword.value); Meteor.call('users.editProfile', profile, (error) => { if (error) { Bert.alert(error.reason, 'danger'); } else { Bert.alert('Profile updated!', 'success'); } }); } render() { const { loading, user } = this.props; return (