/* eslint-disable no-underscore-dangle */ /* eslint-disable import/no-absolute-path */ import React from 'react'; import PropTypes from 'prop-types'; import { Row, FormGroup, ControlLabel, Button } from 'react-bootstrap'; import _ from 'lodash'; import { Meteor } from 'meteor/meteor'; import { Accounts } from 'meteor/accounts-base'; import { Bert } from 'meteor/themeteorchef:bert'; import { testId } from '/imports/ui/components/Utils/TestUtils'; import Col from '../../components/Col/Col'; import { withTracker } from 'meteor/react-meteor-data'; import InputHint from '../../components/InputHint/InputHint'; import validate from '../../../modules/validate'; import { translate } from 'react-i18next'; import { T9n } from 'meteor-accounts-t9n'; import './Profile.scss'; class Profile extends React.Component { constructor(props) { super(props); this.t = this.props.t; this.getUserType = this.getUserType.bind(this); this.handleSubmit = this.handleSubmit.bind(this); this.renderOAuthUser = this.renderOAuthUser.bind(this); this.renderPasswordUser = this.renderPasswordUser.bind(this); this.renderProfileForm = this.renderProfileForm.bind(this); this.onLangSelect = this.onLangSelect.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: this.t('¿Cuál es tu nombre?') }, lastName: { required: this.t('¿Cuál es tu apellido?') }, emailAddress: { required: this.t('Necesitamos una contraseña aquí.'), email: this.t('¿Es correcto este correo?') }, currentPassword: { required: this.t('Necesito tu contraseña si la quieres cambiar.') }, newPassword: { required: this.t('Necesito tu nueva contraseña si la quieres cambiar.') } }, submitHandler() { component.handleSubmit(); } }); } onLangSelect(lang) { // console.log(lang); Meteor.call('users.setLang', lang, (error) => { if (error) { console.error(error); } else { this.props.i18n.changeLanguage(lang); // Bert.alert(this.t("¡Perfíl actualizado!"), 'success'); } }); } getUserType(user) { const userToCheck = user; delete userToCheck.services.resume; const service = Object.keys(userToCheck.services)[0]; return service === 'password' ? 'password' : 'oauth'; } handleSubmit() { const profile = { emailAddress: this.emailAddress.value, profile: { name: { first: this.firstName.value, last: this.lastName.value } } }; Meteor.call('users.editProfile', profile, (error) => { if (error) { Bert.alert(T9n.get(`error.accounts.${error.reason}`), 'danger'); } else { Bert.alert(this.t('¡Perfíl actualizado!'), 'success'); } }); if (this.newPassword.value) { Accounts.changePassword(this.currentPassword.value, this.newPassword.value, (error) => { if (error) { Bert.alert(T9n.get(`error.accounts.${error.reason}`), 'danger'); } else { this.currentPassword.value = ''; this.newPassword.value = ''; } }); } } renderOAuthUser(loading, user) { return !loading ? (
{Object.keys(user.services).map(service => (
{service}

{this.props.t('Has iniciado sesión con {{service}} usando la dirección de correo {{email}}.', { service: _.capitalize(service), email: user.services[service].email })}

))}
) :
; } renderPasswordUser(loading, user) { const { t, i18n } = this.props; const langName = { en: 'English', es: 'Español', gl: 'Galego', ast: 'Asturianu', ca: 'Català' }; return !loading ? (
{this.t('Nombre')} (this.firstName = firstName)} className="form-control" /> {this.t('Apellidos')} (this.lastName = lastName)} className="form-control" /> {this.t('Correo electrónico')} (this.emailAddress = emailAddress)} className="form-control" /> {this.t('Idioma')}
{i18n.languages.map(lang => ( )) }
{this.t('Contraseña actual')} (this.currentPassword = currentPassword)} className="form-control" /> {this.t('Nueva contraseña')} (this.newPassword = newPassword)} className="form-control" /> {this.t('Usa al menos seis caracteres.')}
) :
; } renderProfileForm(loading, user) { return !loading ? ({ password: this.renderPasswordUser, oauth: this.renderOAuthUser }[this.getUserType(user)])(loading, user) :
; } render() { const { loading, user } = this.props; return (

{this.t('Editar perfíl')}

(this.form = form)} onSubmit={event => event.preventDefault()}> {this.renderProfileForm(loading, user)}
); } } Profile.propTypes = { loading: PropTypes.bool.isRequired, user: PropTypes.object.isRequired, t: PropTypes.func.isRequired, i18n: PropTypes.object.isRequired }; export default translate([], { wait: true })(withTracker(() => { const subscription = Meteor.subscribe('users.editProfile'); return { loading: !subscription.ready(), user: Meteor.user() }; })(Profile));