Skip to content

Commit

Permalink
Merge pull request #3777 from Expensify/jasper-requestCallPageMiddleN…
Browse files Browse the repository at this point in the history
…ames

Update logic to account for middle names
  • Loading branch information
sketchydroide authored Jul 1, 2021
2 parents 832a28f + 5475302 commit de44278
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions src/pages/RequestCallPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,17 @@ const propTypes = {
class RequestCallPage extends Component {
constructor(props) {
super(props);

// The displayName defaults to the user's login if they haven't set a first and last name,
// which we can't use to prefill the input fields
const [firstName, lastName] = props.myPersonalDetails.displayName !== props.myPersonalDetails.login
? props.myPersonalDetails.displayName.split(' ')
: [];
const {firstName, lastName} = this.getFirstAndLastName(props.myPersonalDetails);
this.state = {
firstName: firstName ?? '',
lastName: lastName ?? '',
firstName,
lastName,
phoneNumber: this.getPhoneNumber(props.user.loginList) ?? '',
isLoading: false,
};

this.onSubmit = this.onSubmit.bind(this);
this.getPhoneNumber = this.getPhoneNumber.bind(this);
this.getFirstAndLastName = this.getFirstAndLastName.bind(this);
}

onSubmit() {
Expand Down Expand Up @@ -96,6 +92,38 @@ class RequestCallPage extends Component {
return secondaryLogin ? Str.removeSMSDomain(secondaryLogin.partnerUserID) : null;
}

/**
* Gets the first and last name from the user's personal details.
* If the login is the same as the displayName, then they don't exist,
* so we return empty strings instead.
* @param {String} login
* @param {String} displayName
*
* @returns {Object}
*/
getFirstAndLastName({login, displayName}) {
let firstName;
let lastName;

if (login === displayName) {
firstName = '';
lastName = '';
} else {
const firstSpaceIndex = displayName.indexOf(' ');
const lastSpaceIndex = displayName.lastIndexOf(' ');

if (firstSpaceIndex === -1) {
firstName = displayName;
lastName = '';
} else {
firstName = displayName.substring(0, firstSpaceIndex);
lastName = displayName.substring(lastSpaceIndex);
}
}

return {firstName, lastName};
}

render() {
const isButtonDisabled = false;
return (
Expand Down

0 comments on commit de44278

Please sign in to comment.