-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseOnboardingPurpose.tsx
174 lines (155 loc) · 7.65 KB
/
BaseOnboardingPurpose.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
import {useIsFocused} from '@react-navigation/native';
import React, {useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';
import {ScrollView} from 'react-native-gesture-handler';
import {withOnyx} from 'react-native-onyx';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import * as Illustrations from '@components/Icon/Illustrations';
import type {MenuItemProps} from '@components/MenuItem';
import MenuItemList from '@components/MenuItemList';
import OfflineIndicator from '@components/OfflineIndicator';
import SafeAreaConsumer from '@components/SafeAreaConsumer';
import Text from '@components/Text';
import useDisableModalDismissOnEscape from '@hooks/useDisableModalDismissOnEscape';
import useLocalize from '@hooks/useLocalize';
import useOnboardingLayout from '@hooks/useOnboardingLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Navigation from '@libs/Navigation/Navigation';
import OnboardingRefManager from '@libs/OnboardingRefManager';
import type {TOnboardingRef} from '@libs/OnboardingRefManager';
import variables from '@styles/variables';
import * as Welcome from '@userActions/Welcome';
import type {OnboardingPurposeType} from '@src/CONST';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {BaseOnboardingPurposeOnyxProps, BaseOnboardingPurposeProps} from './types';
const menuIcons = {
[CONST.ONBOARDING_CHOICES.EMPLOYER]: Illustrations.ReceiptUpload,
[CONST.ONBOARDING_CHOICES.MANAGE_TEAM]: Illustrations.Abacus,
[CONST.ONBOARDING_CHOICES.PERSONAL_SPEND]: Illustrations.PiggyBank,
[CONST.ONBOARDING_CHOICES.CHAT_SPLIT]: Illustrations.SplitBill,
[CONST.ONBOARDING_CHOICES.LOOKING_AROUND]: Illustrations.Binoculars,
};
function BaseOnboardingPurpose({shouldUseNativeStyles, shouldEnableMaxHeight, onboardingPurposeSelected}: BaseOnboardingPurposeProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {shouldUseNarrowLayout} = useOnboardingLayout();
const [selectedPurpose, setSelectedPurpose] = useState<OnboardingPurposeType | undefined>(undefined);
const {isSmallScreenWidth, windowHeight} = useWindowDimensions();
const theme = useTheme();
useDisableModalDismissOnEscape();
const PurposeFooterInstance = <OfflineIndicator />;
useEffect(() => {
setSelectedPurpose(onboardingPurposeSelected ?? undefined);
}, [onboardingPurposeSelected]);
const maxHeight = shouldEnableMaxHeight ? windowHeight : undefined;
const paddingHorizontal = shouldUseNarrowLayout ? styles.ph8 : styles.ph5;
const selectedCheckboxIcon = useMemo(
() => (
<View style={[styles.popoverMenuIcon, styles.pointerEventsAuto]}>
<Icon
src={Expensicons.Checkmark}
fill={theme.success}
/>
</View>
),
[styles.pointerEventsAuto, styles.popoverMenuIcon, theme.success],
);
const saveAndNavigate = useCallback(() => {
if (selectedPurpose === undefined) {
return;
}
if (selectedPurpose === CONST.ONBOARDING_CHOICES.MANAGE_TEAM) {
Navigation.navigate(ROUTES.ONBOARDING_WORK);
return;
}
Navigation.navigate(ROUTES.ONBOARDING_PERSONAL_DETAILS);
}, [selectedPurpose]);
const [errorMessage, setErrorMessage] = useState<string>('');
const menuItems: MenuItemProps[] = Object.values(CONST.ONBOARDING_CHOICES).map((choice) => {
const translationKey = `onboarding.purpose.${choice}` as const;
const isSelected = selectedPurpose === choice;
return {
key: translationKey,
title: translate(translationKey),
icon: menuIcons[choice],
displayInDefaultIconColor: true,
iconWidth: variables.menuIconSize,
iconHeight: variables.menuIconSize,
iconStyles: [styles.mh3],
wrapperStyle: [styles.purposeMenuItem, isSelected && styles.purposeMenuItemSelected],
hoverAndPressStyle: [styles.purposeMenuItemSelected],
rightComponent: selectedCheckboxIcon,
shouldShowRightComponent: isSelected,
onPress: () => {
Welcome.setOnboardingPurposeSelected(choice);
setErrorMessage('');
},
};
});
const isFocused = useIsFocused();
const handleOuterClick = useCallback(() => {
if (!selectedPurpose) {
setErrorMessage(translate('onboarding.purpose.errorSelection'));
} else {
setErrorMessage(translate('onboarding.purpose.errorContinue'));
}
}, [selectedPurpose, setErrorMessage, translate]);
const onboardingLocalRef = useRef<TOnboardingRef>(null);
useImperativeHandle(isFocused ? OnboardingRefManager.ref : onboardingLocalRef, () => ({handleOuterClick}), [handleOuterClick]);
return (
<SafeAreaConsumer>
{({safeAreaPaddingBottomStyle}) => (
<View style={[{maxHeight}, styles.h100, styles.defaultModalContainer, shouldUseNativeStyles && styles.pt8, safeAreaPaddingBottomStyle]}>
<View style={shouldUseNarrowLayout && styles.mh3}>
<HeaderWithBackButton
shouldShowBackButton={false}
iconFill={theme.iconColorfulBackground}
progressBarPercentage={25}
/>
</View>
<ScrollView style={[styles.flex1, styles.flexGrow1, shouldUseNarrowLayout && styles.mt5, paddingHorizontal]}>
<View style={styles.flex1}>
<View style={[shouldUseNarrowLayout ? styles.flexRow : styles.flexColumn, styles.mb5]}>
<Text style={styles.textHeadlineH1}>{translate('onboarding.purpose.title')} </Text>
</View>
<MenuItemList
menuItems={menuItems}
shouldUseSingleExecution
/>
</View>
</ScrollView>
<FormAlertWithSubmitButton
enabledWhenOffline
footerContent={isSmallScreenWidth && PurposeFooterInstance}
buttonText={translate('common.continue')}
onSubmit={() => {
if (!selectedPurpose) {
setErrorMessage(translate('onboarding.purpose.errorSelection'));
return;
}
setErrorMessage('');
saveAndNavigate();
}}
message={errorMessage}
isAlertVisible={!!errorMessage}
containerStyles={[styles.w100, styles.mb5, styles.mh0, paddingHorizontal]}
/>
</View>
)}
</SafeAreaConsumer>
);
}
BaseOnboardingPurpose.displayName = 'BaseOnboardingPurpose';
export default withOnyx<BaseOnboardingPurposeProps, BaseOnboardingPurposeOnyxProps>({
onboardingPurposeSelected: {
key: ONYXKEYS.ONBOARDING_PURPOSE_SELECTED,
},
})(BaseOnboardingPurpose);
export type {BaseOnboardingPurposeProps};