194 lines
6.8 KiB
JavaScript
194 lines
6.8 KiB
JavaScript
import React, { Component } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import { Button, 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 styles from './Profile.styles.js';
|
|
|
|
const STRINGS = {
|
|
CANCEL: 'Cancel',
|
|
SAVE_PROFILE: 'Save profile',
|
|
};
|
|
|
|
export default class EditProfile extends Component {
|
|
static get propTypes() {
|
|
return {
|
|
addresses: PropTypes.array,
|
|
avatar: PropTypes.string,
|
|
cancelEditAction: PropTypes.func.isRequired,
|
|
email: PropTypes.string,
|
|
firstName: PropTypes.string,
|
|
initials: PropTypes.string,
|
|
isGeneratedNomDeBid: PropTypes.bool,
|
|
lastName: PropTypes.string,
|
|
nomDeBid: PropTypes.string,
|
|
phones: PropTypes.array,
|
|
saveProfileAction: PropTypes.func.isRequired,
|
|
saveProfileLabel: PropTypes.string,
|
|
};
|
|
}
|
|
|
|
static get defaultProps() {
|
|
return {
|
|
addresses: null,
|
|
avatar: null,
|
|
email: null,
|
|
firstName: null,
|
|
initials: null,
|
|
isGeneratedNomDeBid: false,
|
|
lastName: null,
|
|
nomDeBid: null,
|
|
phones: null,
|
|
saveProfileLabel: STRINGS.SAVE_PROFILE,
|
|
};
|
|
}
|
|
|
|
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,
|
|
phones: this.props.phones,
|
|
};
|
|
|
|
this.handleCancel = this.handleCancel.bind(this);
|
|
this.handleSubmit = this.handleSubmit.bind(this);
|
|
}
|
|
|
|
_validateEmail() {
|
|
getEmailAvailability(this.state.email, (result) =>
|
|
this.setState('invalidEmail', !result.available),
|
|
);
|
|
}
|
|
|
|
_validateNomDeBid() {
|
|
getNomAvailability(this.state.nomDeBid, (result) =>
|
|
this.setState('invalidNomDeBid', !result.available),
|
|
);
|
|
}
|
|
|
|
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() {
|
|
return (
|
|
!this.state.invalidEmail &&
|
|
!this.state.invalidNomDeBid &&
|
|
!!this.state.firstName &&
|
|
!!this.state.lastName &&
|
|
!!this.state.email &&
|
|
!!this.state.nomDeBid &&
|
|
!!this.state.phones.length &&
|
|
!!this.state.password
|
|
);
|
|
}
|
|
|
|
render() {
|
|
const { isGeneratedNomDeBid } = this.props;
|
|
const { avatar, firstName, lastName } = this.state;
|
|
const addressesTitle = 'Addresses';
|
|
const numbersTitle = 'Numbers';
|
|
|
|
return (
|
|
<View style={styles.profileFormWrap}>
|
|
<View style={styles.avatarWrap}>
|
|
{avatar !== null ? (
|
|
<Avatar source={{ uri: this.state.avatar }} showEditButton />
|
|
) : (
|
|
<Avatar title={this.props.initials} showEditButton />
|
|
)}
|
|
</View>
|
|
<View style={styles.nameWrap}>
|
|
<TextInput
|
|
onChange={(text) => this.setState({ firstName: text })}
|
|
placeholder="first name"
|
|
style={[styles.textInput, styles.requiredInput]}
|
|
value={this.state.firstName}
|
|
/>
|
|
<TextInput
|
|
onChange={(text) => this.setState({ lastName: text })}
|
|
placeholder="last name"
|
|
style={[styles.textInput, styles.requiredInput]}
|
|
value={this.state.lastName}
|
|
/>
|
|
</View>
|
|
<View style={[styles.emailWrap, styles.requiredWrap]}>
|
|
<TextInput
|
|
keyboardType="email-address"
|
|
onChangeText={(text) => this.setState({ email: text })}
|
|
onEndEditing={(text) => this._validateEmail(text)}
|
|
placeholder="email address"
|
|
style={[styles.textInput, styles.requiredInput]}
|
|
value={this.state.email}
|
|
/>
|
|
</View>
|
|
{isGeneratedNomDeBid && (
|
|
<View style={[styles.nomWrap, styles.requiredWrap]}>
|
|
<EditNomDeBid
|
|
isGeneratedNomDeBid={isGeneratedNomDeBid}
|
|
isStandalone={false}
|
|
nomDeBid={this.state.nomDeBid}
|
|
updateNomDeBid={(nomDeBid) => this.setState({ nomDeBid })}
|
|
/>
|
|
</View>
|
|
)}
|
|
<View style={styles.phonesWrap}>
|
|
<Text style={[styles.groupLabel, styles.requiredLabel]}>{numbersTitle}</Text>
|
|
{this.props.phones.length > 0 && (
|
|
/* LIST PHONES */
|
|
<View />
|
|
)}
|
|
<Button title="Add number" onPress={() => false} />
|
|
</View>
|
|
<View style={styles.addressesWrap}>
|
|
<Text style={styles.groupLabel}>{addressesTitle}</Text>
|
|
{this.props.addresses.length > 0 && (
|
|
/* LIST ADDRESSES */
|
|
<View />
|
|
)}
|
|
<Button title="Add address" onPress={() => false} />
|
|
</View>
|
|
<View style={styles.register}>
|
|
<Button title={this.props.saveProfileLabel} onPress={this.handleSubmit} />
|
|
<Button title={STRINGS.CANCEL} onPress={this.handleCancel} />
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
}
|