import { List } from 'immutable'; import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Button, Picker, ScrollView, Text, TextInput, View } from 'react-native'; import { Avatar } from 'react-native-elements'; import { getEmailAvailability, getNomAvailability } from '../../api/profile.js'; import EditNomDeBid from './EditNomDeBid.js'; import PhoneListInput from './PhoneInput/PhoneListInput.js'; import styles from './Profile.styles.js'; const STRINGS = { CANCEL: 'Cancel', NOM_HEADING: 'Nom de Bid', PASSWORD_HEADING: 'Password', PERSONAL_HEADING: 'A bit about you...', SAVE_PROFILE: 'Save profile', }; export default class EditProfile extends Component { static get propTypes() { return { addresses: PropTypes.oneOfType([PropTypes.array, PropTypes.instanceOf(List)]), avatar: PropTypes.string, cancelEditAction: PropTypes.func.isRequired, email: PropTypes.string, firstName: PropTypes.string, initials: PropTypes.string, isGeneratedNomDeBid: PropTypes.bool, isRegsiteredAccount: PropTypes.bool, lastName: PropTypes.string, nomDeBid: PropTypes.string, phones: PropTypes.oneOfType([PropTypes.array, PropTypes.instanceOf(List)]), saveProfileAction: PropTypes.func.isRequired, saveProfileLabel: PropTypes.string, showPasswordEntry: PropTypes.bool, }; } static get defaultProps() { return { addresses: new List(), avatar: null, email: null, firstName: null, initials: null, isGeneratedNomDeBid: false, isRegsiteredAccount: false, lastName: null, nomDeBid: null, phones: new List(), saveProfileLabel: STRINGS.SAVE_PROFILE, showPasswordEntry: false, }; } constructor(props) { super(props); this.state = { addresses: this.props.addresses, avatar: this.props.avatar, email: this.props.email, firstName: this.props.firstName, lastName: this.props.lastName, invalidEmail: false, invalidNomDeBid: false, nomDeBid: this.props.nomDeBid, password: this.props.password, passwordsMismatch: false, phones: this.props.phones, }; this.handleCancel = this.handleCancel.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } _validateEmail() { getEmailAvailability(this.state.email) .then(({ available = false }) => this.setState('invalidEmail', !available), ); } _validateNomDeBid() { getNomAvailability(this.state.nomDeBid) .then(({ available = false }) => this.setState('invalidNomDeBid', !available), ); } _validatePasswordMatch(password) { this.setState({ passwordsMismatch: this.state.password !== password }); } getProfileFromState() { return { addresses: this.state.addresses, avatar: this.state.avatar, email: this.state.email, firstName: this.state.firstName, lastName: this.state.lastName, nomDeBid: this.state.nomDeBid, password: this.state.password, phones: this.state.phones, }; } handleCancel() { this.props.cancelEditAction(); } handleSubmit() { if (!this.isFormComplete()) { console.error('Incomplete form... how did the button become enabled?'); alert('Please complete all of the required fields. They have bold labels.'); return; } this.props.saveProfileAction(this.getProfileFromState()); } isFormComplete() { const { showPasswordEntry } = this.props; const { password } = this.state; return ( !this.state.invalidEmail && !this.state.invalidNomDeBid && !!this.state.firstName && !!this.state.lastName && !!this.state.email && !!this.state.nomDeBid && !!this.state.phones.size && ((showPasswordEntry && !!password) || !showPasswordEntry) ); } render() { const { isGeneratedNomDeBid, isRegsiteredAccount, showPasswordEntry } = this.props; const { addresses, avatar, firstName, lastName, phones } = this.state; const addressesTitle = 'Addresses'; const numbersTitle = 'Numbers'; return ( {avatar !== null ? ( ) : ( )} {STRINGS.PERSONAL_HEADING} this.setState({ firstName: text })} placeholder="first name" style={[styles.textInput, styles.requiredInput]} value={this.state.firstName} /> this.setState({ lastName: text })} placeholder="last name" style={[styles.textInput, styles.requiredInput]} value={this.state.lastName} /> this.setState({ email: text })} onEndEditing={(text) => this._validateEmail(text)} placeholder="email address" style={[styles.textInput, styles.requiredInput]} value={this.state.email} /> {showPasswordEntry && ( {STRINGS.PASSWORD_HEADING} this.setState({ password: text })} placeholder="password" secureTextEntry style={[styles.textInput, styles.requiredInput]} /> this._validatePasswordMatch(text)} placeholder="re-enter password" secureTextEntry style={[styles.textInput, styles.requiredInput]} /> )} {(isGeneratedNomDeBid || !isRegsiteredAccount) && ( {STRINGS.NOM_HEADING} this.setState({ nomDeBid })} /> )} this.setState({ phones })} handleDelete={(phones) => this.setState({ phones })} handleEdit={(phones) => this.setState({ phones })} phones={phones} /> {addressesTitle} {addresses !== null && addresses.size > 0 && ( /* LIST ADDRESSES */ )}