Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate DisplayNamePage.js to function #20334

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 56 additions & 64 deletions src/pages/settings/Profile/DisplayNamePage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import React from 'react';
import {View} from 'react-native';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../components/withCurrentUserPersonalDetails';
import ScreenWrapper from '../../../components/ScreenWrapper';
Expand Down Expand Up @@ -27,94 +27,86 @@ const defaultProps = {
...withCurrentUserPersonalDetailsDefaultProps,
};

class DisplayNamePage extends Component {
constructor(props) {
super(props);

this.validate = this.validate.bind(this);
this.updateDisplayName = this.updateDisplayName.bind(this);
}
/**
* Submit form to update user's first and last name (and display name)
* @param {Object} values
* @param {String} values.firstName
* @param {String} values.lastName
*/
const updateDisplayName = (values) => {
PersonalDetails.updateDisplayName(values.firstName.trim(), values.lastName.trim());
};

/**
* Submit form to update user's first and last name (and display name)
* @param {Object} values
* @param {String} values.firstName
* @param {String} values.lastName
*/
updateDisplayName(values) {
PersonalDetails.updateDisplayName(values.firstName.trim(), values.lastName.trim());
}
function DisplayNamePage(props) {
const currentUserDetails = props.currentUserPersonalDetails || {};

/**
* @param {Object} values
* @param {String} values.firstName
* @param {String} values.lastName
* @returns {Object} - An object containing the errors for each inputID
*/
validate(values) {
const validate = (values) => {
const errors = {};

// First we validate the first name field
if (!ValidationUtils.isValidDisplayName(values.firstName)) {
ErrorUtils.addErrorMessage(errors, 'firstName', this.props.translate('personalDetails.error.hasInvalidCharacter'));
ErrorUtils.addErrorMessage(errors, 'firstName', props.translate('personalDetails.error.hasInvalidCharacter'));
}
if (ValidationUtils.doesContainReservedWord(values.firstName, CONST.DISPLAY_NAME.RESERVED_FIRST_NAMES)) {
ErrorUtils.addErrorMessage(errors, 'firstName', this.props.translate('personalDetails.error.containsReservedWord'));
ErrorUtils.addErrorMessage(errors, 'firstName', props.translate('personalDetails.error.containsReservedWord'));
}

// Then we validate the last name field
if (!ValidationUtils.isValidDisplayName(values.lastName)) {
errors.lastName = this.props.translate('personalDetails.error.hasInvalidCharacter');
errors.lastName = props.translate('personalDetails.error.hasInvalidCharacter');
}

return errors;
}

render() {
const currentUserDetails = this.props.currentUserPersonalDetails || {};
};

return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
title={this.props.translate('displayNamePage.headerTitle')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.DISPLAY_NAME_FORM}
validate={this.validate}
onSubmit={this.updateDisplayName}
submitButtonText={this.props.translate('common.save')}
enabledWhenOffline
>
<Text style={[styles.mb6]}>{this.props.translate('displayNamePage.isShownOnProfile')}</Text>
<View style={styles.mb4}>
<TextInput
inputID="firstName"
name="fname"
label={this.props.translate('common.firstName')}
defaultValue={lodashGet(currentUserDetails, 'firstName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
autoCapitalize="words"
/>
</View>
<View>
<TextInput
inputID="lastName"
name="lname"
label={this.props.translate('common.lastName')}
defaultValue={lodashGet(currentUserDetails, 'lastName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
autoCapitalize="words"
/>
</View>
</Form>
</ScreenWrapper>
);
}
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
title={props.translate('displayNamePage.headerTitle')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.DISPLAY_NAME_FORM}
validate={validate}
onSubmit={updateDisplayName}
submitButtonText={props.translate('common.save')}
enabledWhenOffline
>
<Text style={[styles.mb6]}>{props.translate('displayNamePage.isShownOnProfile')}</Text>
<View style={styles.mb4}>
<TextInput
inputID="firstName"
name="fname"
label={props.translate('common.firstName')}
defaultValue={lodashGet(currentUserDetails, 'firstName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
autoCapitalize="words"
/>
</View>
<View>
<TextInput
inputID="lastName"
name="lname"
label={props.translate('common.lastName')}
defaultValue={lodashGet(currentUserDetails, 'lastName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
autoCapitalize="words"
/>
</View>
</Form>
</ScreenWrapper>
);
}

DisplayNamePage.propTypes = propTypes;
DisplayNamePage.defaultProps = defaultProps;
DisplayNamePage.displayName = 'DisplayNamePage';

export default compose(withLocalize, withCurrentUserPersonalDetails)(DisplayNamePage);