-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseOnboardingPersonalDetails.tsx
198 lines (179 loc) · 9.2 KB
/
BaseOnboardingPersonalDetails.tsx
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import React, {useCallback, useState} 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 type {FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
import OfflineIndicator from '@components/OfflineIndicator';
import {useSession} from '@components/OnyxProvider';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useDisableModalDismissOnEscape from '@hooks/useDisableModalDismissOnEscape';
import useLocalize from '@hooks/useLocalize';
import useOnboardingLayout from '@hooks/useOnboardingLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import AccountUtils from '@libs/AccountUtils';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ValidationUtils from '@libs/ValidationUtils';
import variables from '@styles/variables';
import * as PersonalDetails from '@userActions/PersonalDetails';
import * as Report from '@userActions/Report';
import * as Welcome from '@userActions/Welcome';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/DisplayNameForm';
import type {BaseOnboardingPersonalDetailsOnyxProps, BaseOnboardingPersonalDetailsProps} from './types';
function BaseOnboardingPersonalDetails({currentUserPersonalDetails, shouldUseNativeStyles, onboardingPurposeSelected, onboardingAdminsChatReportID}: BaseOnboardingPersonalDetailsProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {isSmallScreenWidth} = useWindowDimensions();
const {shouldUseNarrowLayout} = useOnboardingLayout();
const {inputCallbackRef} = useAutoFocusInput();
const [shouldValidateOnChange, setShouldValidateOnChange] = useState(false);
const {accountID} = useSession();
useDisableModalDismissOnEscape();
const completeEngagement = useCallback(
(values: FormOnyxValues<'onboardingPersonalDetailsForm'>) => {
const firstName = values.firstName.trim();
const lastName = values.lastName.trim();
PersonalDetails.setDisplayName(firstName, lastName);
if (!onboardingPurposeSelected) {
return;
}
Report.completeOnboarding(
onboardingPurposeSelected,
CONST.ONBOARDING_MESSAGES[onboardingPurposeSelected],
{
firstName,
lastName,
},
onboardingAdminsChatReportID ?? undefined,
);
Welcome.setOnboardingAdminsChatReportID();
Welcome.setOnboardingPolicyID();
Navigation.dismissModal();
// Only navigate to concierge chat when central pane is visible
// Otherwise stay on the chats screen.
if (isSmallScreenWidth) {
Navigation.navigate(ROUTES.HOME);
} else if (AccountUtils.isAccountIDOddNumber(accountID ?? 0)) {
Report.navigateToSystemChat();
} else {
Report.navigateToConciergeChat();
}
// Small delay purely due to design considerations,
// no special technical reasons behind that.
setTimeout(() => {
Navigation.navigate(ROUTES.WELCOME_VIDEO_ROOT);
}, variables.welcomeVideoDelay);
},
[isSmallScreenWidth, onboardingPurposeSelected, onboardingAdminsChatReportID, accountID],
);
const validate = (values: FormOnyxValues<'onboardingPersonalDetailsForm'>) => {
if (!shouldValidateOnChange) {
setShouldValidateOnChange(true);
}
const errors = {};
// First we validate the first name field
if (values.firstName.replace(CONST.REGEX.ANY_SPACE, '').length === 0) {
ErrorUtils.addErrorMessage(errors, 'firstName', 'onboarding.error.requiredFirstName');
}
if (!ValidationUtils.isValidDisplayName(values.firstName)) {
ErrorUtils.addErrorMessage(errors, 'firstName', 'personalDetails.error.hasInvalidCharacter');
} else if (values.firstName.length > CONST.DISPLAY_NAME.MAX_LENGTH) {
ErrorUtils.addErrorMessage(errors, 'firstName', ['common.error.characterLimitExceedCounter', {length: values.firstName.length, limit: CONST.DISPLAY_NAME.MAX_LENGTH}]);
}
if (ValidationUtils.doesContainReservedWord(values.firstName, CONST.DISPLAY_NAME.RESERVED_NAMES)) {
ErrorUtils.addErrorMessage(errors, 'firstName', 'personalDetails.error.containsReservedWord');
}
// Then we validate the last name field
if (!ValidationUtils.isValidDisplayName(values.lastName)) {
ErrorUtils.addErrorMessage(errors, 'lastName', 'personalDetails.error.hasInvalidCharacter');
} else if (values.lastName.length > CONST.DISPLAY_NAME.MAX_LENGTH) {
ErrorUtils.addErrorMessage(errors, 'lastName', ['common.error.characterLimitExceedCounter', {length: values.lastName.length, limit: CONST.DISPLAY_NAME.MAX_LENGTH}]);
}
if (ValidationUtils.doesContainReservedWord(values.lastName, CONST.DISPLAY_NAME.RESERVED_NAMES)) {
ErrorUtils.addErrorMessage(errors, 'lastName', 'personalDetails.error.containsReservedWord');
}
return errors;
};
const PersonalDetailsFooterInstance = <OfflineIndicator />;
return (
<View style={[styles.h100, styles.defaultModalContainer, shouldUseNativeStyles && styles.pt8]}>
<HeaderWithBackButton
shouldShowBackButton
progressBarPercentage={75}
onBackButtonPress={Navigation.goBack}
/>
<KeyboardAvoidingView
style={[styles.flex1, styles.dFlex]}
behavior="padding"
>
<FormProvider
style={[styles.flexGrow1, shouldUseNarrowLayout && styles.mt5, styles.mb5, shouldUseNarrowLayout ? styles.mh8 : styles.mh5]}
formID={ONYXKEYS.FORMS.ONBOARDING_PERSONAL_DETAILS_FORM}
footerContent={isSmallScreenWidth && PersonalDetailsFooterInstance}
validate={validate}
onSubmit={completeEngagement}
submitButtonText={translate('common.continue')}
enabledWhenOffline
submitFlexEnabled
shouldValidateOnBlur={false}
shouldValidateOnChange={shouldValidateOnChange}
shouldTrimValues={false}
>
<View style={[shouldUseNarrowLayout ? styles.flexRow : styles.flexColumn, styles.mb5]}>
<Text style={styles.textHeadlineH1}>{translate('onboarding.whatsYourName')}</Text>
</View>
<View style={styles.mb4}>
<InputWrapper
InputComponent={TextInput}
ref={inputCallbackRef}
inputID={INPUT_IDS.FIRST_NAME}
name="fname"
label={translate('common.firstName')}
aria-label={translate('common.firstName')}
role={CONST.ROLE.PRESENTATION}
defaultValue={currentUserPersonalDetails?.firstName}
shouldSaveDraft
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
<View>
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.LAST_NAME}
name="lname"
label={translate('common.lastName')}
aria-label={translate('common.lastName')}
role={CONST.ROLE.PRESENTATION}
defaultValue={currentUserPersonalDetails?.lastName}
shouldSaveDraft
maxLength={CONST.DISPLAY_NAME.MAX_LENGTH}
spellCheck={false}
/>
</View>
</FormProvider>
</KeyboardAvoidingView>
</View>
);
}
BaseOnboardingPersonalDetails.displayName = 'BaseOnboardingPersonalDetails';
export default withCurrentUserPersonalDetails(
withOnyx<BaseOnboardingPersonalDetailsProps, BaseOnboardingPersonalDetailsOnyxProps>({
onboardingPurposeSelected: {
key: ONYXKEYS.ONBOARDING_PURPOSE_SELECTED,
},
onboardingAdminsChatReportID: {
key: ONYXKEYS.ONBOARDING_ADMINS_CHAT_REPORT_ID,
},
})(BaseOnboardingPersonalDetails),
);