-
Notifications
You must be signed in to change notification settings - Fork 3k
/
DisplayNamePage.js
142 lines (132 loc) · 5.64 KB
/
DisplayNamePage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultProps, withCurrentUserPersonalDetailsPropTypes} from '@components/withCurrentUserPersonalDetails';
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import compose from '@libs/compose';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ValidationUtils from '@libs/ValidationUtils';
import * as PersonalDetails from '@userActions/PersonalDetails';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
const propTypes = {
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
isLoadingApp: PropTypes.bool,
};
const defaultProps = {
...withCurrentUserPersonalDetailsDefaultProps,
isLoadingApp: true,
};
/**
* 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());
};
function DisplayNamePage(props) {
const styles = useThemeStyles();
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
*/
const validate = (values) => {
const errors = {};
// First we validate the first name field
if (!ValidationUtils.isValidDisplayName(values.firstName)) {
ErrorUtils.addErrorMessage(errors, 'firstName', 'personalDetails.error.hasInvalidCharacter');
}
if (ValidationUtils.doesContainReservedWord(values.firstName, CONST.DISPLAY_NAME.RESERVED_FIRST_NAMES)) {
ErrorUtils.addErrorMessage(errors, 'firstName', 'personalDetails.error.containsReservedWord');
}
// Then we validate the last name field
if (!ValidationUtils.isValidDisplayName(values.lastName)) {
errors.lastName = 'personalDetails.error.hasInvalidCharacter';
}
return errors;
};
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={DisplayNamePage.displayName}
>
<HeaderWithBackButton
title={props.translate('displayNamePage.headerTitle')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PROFILE)}
/>
{props.isLoadingApp ? (
<FullScreenLoadingIndicator style={[styles.flex1, styles.pRelative]} />
) : (
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.DISPLAY_NAME_FORM}
validate={validate}
onSubmit={updateDisplayName}
submitButtonText={props.translate('common.save')}
enabledWhenOffline
shouldValidateOnBlur
shouldValidateOnChange
>
<Text style={[styles.mb6]}>{props.translate('displayNamePage.isShownOnProfile')}</Text>
<View style={styles.mb4}>
<InputWrapper
InputComponent={TextInput}
inputID="firstName"
name="fname"
label={props.translate('common.firstName')}
aria-label={props.translate('common.firstName')}
role={CONST.ROLE.PRESENTATION}
defaultValue={lodashGet(currentUserDetails, 'firstName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
<View>
<InputWrapper
InputComponent={TextInput}
inputID="lastName"
name="lname"
label={props.translate('common.lastName')}
aria-label={props.translate('common.lastName')}
role={CONST.ROLE.PRESENTATION}
defaultValue={lodashGet(currentUserDetails, 'lastName', '')}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
</FormProvider>
)}
</ScreenWrapper>
);
}
DisplayNamePage.propTypes = propTypes;
DisplayNamePage.defaultProps = defaultProps;
DisplayNamePage.displayName = 'DisplayNamePage';
export default compose(
withLocalize,
withCurrentUserPersonalDetails,
withOnyx({
isLoadingApp: {
key: ONYXKEYS.IS_LOADING_APP,
},
}),
)(DisplayNamePage);