-
Notifications
You must be signed in to change notification settings - Fork 3k
/
LegalNamePage.js
132 lines (119 loc) · 5.22 KB
/
LegalNamePage.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
import _ from 'underscore';
import React, {useCallback} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import HeaderWithBackButton from '../../../../components/HeaderWithBackButton';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import Form from '../../../../components/Form';
import ONYXKEYS from '../../../../ONYXKEYS';
import CONST from '../../../../CONST';
import * as ValidationUtils from '../../../../libs/ValidationUtils';
import TextInput from '../../../../components/TextInput';
import styles from '../../../../styles/styles';
import * as PersonalDetails from '../../../../libs/actions/PersonalDetails';
import compose from '../../../../libs/compose';
import Navigation from '../../../../libs/Navigation/Navigation';
import ROUTES from '../../../../ROUTES';
import usePrivatePersonalDetails from '../../../../hooks/usePrivatePersonalDetails';
import FullscreenLoadingIndicator from '../../../../components/FullscreenLoadingIndicator';
const propTypes = {
/* Onyx Props */
/** User's private personal details */
privatePersonalDetails: PropTypes.shape({
legalFirstName: PropTypes.string,
legalLastName: PropTypes.string,
}),
...withLocalizePropTypes,
};
const defaultProps = {
privatePersonalDetails: {
legalFirstName: '',
legalLastName: '',
},
};
const updateLegalName = (values) => {
PersonalDetails.updateLegalName(values.legalFirstName.trim(), values.legalLastName.trim());
};
function LegalNamePage(props) {
usePrivatePersonalDetails();
const legalFirstName = lodashGet(props.privatePersonalDetails, 'legalFirstName', '');
const legalLastName = lodashGet(props.privatePersonalDetails, 'legalLastName', '');
const isLoadingPersonalDetails = lodashGet(props.privatePersonalDetails, 'isLoading', true);
const validate = useCallback((values) => {
const errors = {};
if (!ValidationUtils.isValidLegalName(values.legalFirstName)) {
errors.legalFirstName = 'privatePersonalDetails.error.hasInvalidCharacter';
} else if (_.isEmpty(values.legalFirstName)) {
errors.legalFirstName = 'common.error.fieldRequired';
}
if (!ValidationUtils.isValidLegalName(values.legalLastName)) {
errors.legalLastName = 'privatePersonalDetails.error.hasInvalidCharacter';
} else if (_.isEmpty(values.legalLastName)) {
errors.legalLastName = 'common.error.fieldRequired';
}
return errors;
}, []);
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={LegalNamePage.displayName}
>
<HeaderWithBackButton
title={props.translate('privatePersonalDetails.legalName')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_PERSONAL_DETAILS)}
/>
{isLoadingPersonalDetails ? (
<FullscreenLoadingIndicator style={[styles.flex1, styles.pRelative]} />
) : (
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.LEGAL_NAME_FORM}
validate={validate}
onSubmit={updateLegalName}
submitButtonText={props.translate('common.save')}
enabledWhenOffline
>
<View style={[styles.mb4]}>
<TextInput
inputID="legalFirstName"
name="lfname"
label={props.translate('privatePersonalDetails.legalFirstName')}
accessibilityLabel={props.translate('privatePersonalDetails.legalFirstName')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
defaultValue={legalFirstName}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
<View>
<TextInput
inputID="legalLastName"
name="llname"
label={props.translate('privatePersonalDetails.legalLastName')}
accessibilityLabel={props.translate('privatePersonalDetails.legalLastName')}
accessibilityRole={CONST.ACCESSIBILITY_ROLE.TEXT}
defaultValue={legalLastName}
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
</Form>
)}
</ScreenWrapper>
);
}
LegalNamePage.propTypes = propTypes;
LegalNamePage.defaultProps = defaultProps;
LegalNamePage.displayName = 'LegalNamePage';
export default compose(
withLocalize,
withOnyx({
privatePersonalDetails: {
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
},
}),
)(LegalNamePage);