-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ContactMethodsPage.tsx
147 lines (134 loc) · 6.29 KB
/
ContactMethodsPage.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
import type {StackScreenProps} from '@react-navigation/stack';
import {Str} from 'expensify-common';
import React, {useCallback} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import CopyTextToClipboard from '@components/CopyTextToClipboard';
import FixedFooter from '@components/FixedFooter';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import MenuItem from '@components/MenuItem';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {LoginList, Session} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type ContactMethodsPageOnyxProps = {
/** Login list for the user that is signed in */
loginList: OnyxEntry<LoginList>;
/** Current user session */
session: OnyxEntry<Session>;
};
type ContactMethodsPageProps = ContactMethodsPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.PROFILE.CONTACT_METHODS>;
function ContactMethodsPage({loginList, session, route}: ContactMethodsPageProps) {
const styles = useThemeStyles();
const {formatPhoneNumber, translate} = useLocalize();
const loginNames = Object.keys(loginList ?? {});
const navigateBackTo = route?.params?.backTo;
// Sort the login names by placing the one corresponding to the default contact method as the first item before displaying the contact methods.
// The default contact method is determined by checking against the session email (the current login).
const sortedLoginNames = loginNames.sort((loginName) => (loginList?.[loginName].partnerUserID === session?.email ? -1 : 1));
const loginMenuItems = sortedLoginNames.map((loginName) => {
const login = loginList?.[loginName];
const pendingAction = login?.pendingFields?.deletedLogin ?? login?.pendingFields?.addedLogin ?? undefined;
if (!login?.partnerUserID && !pendingAction) {
return null;
}
let description = '';
if (session?.email === login?.partnerUserID) {
description = translate('contacts.getInTouch');
} else if (login?.errorFields?.addedLogin) {
description = translate('contacts.failedNewContact');
} else if (!login?.validatedDate) {
description = translate('contacts.pleaseVerify');
}
let indicator;
if (Object.values(login?.errorFields ?? {}).some((errorField) => !isEmptyObject(errorField))) {
indicator = CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR;
} else if (!login?.validatedDate) {
indicator = CONST.BRICK_ROAD_INDICATOR_STATUS.INFO;
}
// Default to using login key if we deleted login.partnerUserID optimistically
// but still need to show the pending login being deleted while offline.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const partnerUserID = login?.partnerUserID || loginName;
const menuItemTitle = Str.isSMSLogin(partnerUserID) ? formatPhoneNumber(partnerUserID) : partnerUserID;
return (
<OfflineWithFeedback
pendingAction={pendingAction}
key={partnerUserID}
>
<MenuItem
title={menuItemTitle}
description={description}
onPress={() => {
if (!login?.validatedDate && !login?.validateCodeSent) {
User.requestContactMethodValidateCode(loginName);
}
Navigation.navigate(ROUTES.SETTINGS_CONTACT_METHOD_DETAILS.getRoute(partnerUserID, navigateBackTo));
}}
brickRoadIndicator={indicator}
shouldShowBasicTitle
shouldShowRightIcon
disabled={!!pendingAction}
/>
</OfflineWithFeedback>
);
});
const onNewContactMethodButtonPress = useCallback(() => {
Navigation.navigate(ROUTES.SETTINGS_NEW_CONTACT_METHOD.getRoute(navigateBackTo));
}, [navigateBackTo]);
return (
<ScreenWrapper
shouldEnableKeyboardAvoidingView={false}
testID={ContactMethodsPage.displayName}
>
<HeaderWithBackButton
title={translate('contacts.contactMethods')}
onBackButtonPress={() => Navigation.goBack()}
/>
<ScrollView contentContainerStyle={styles.flexGrow1}>
<View style={[styles.ph5, styles.mv3, styles.flexRow, styles.flexWrap]}>
<Text>
{translate('contacts.helpTextBeforeEmail')}
<CopyTextToClipboard
text={CONST.EMAIL.RECEIPTS}
textStyles={[styles.textBlue]}
/>
<Text>{translate('contacts.helpTextAfterEmail')}</Text>
</Text>
</View>
{loginMenuItems}
<FixedFooter style={[styles.mtAuto, styles.pt5]}>
<Button
large
success
text={translate('contacts.newContactMethod')}
onPress={onNewContactMethodButtonPress}
pressOnEnter
/>
</FixedFooter>
</ScrollView>
</ScreenWrapper>
);
}
ContactMethodsPage.displayName = 'ContactMethodsPage';
export default withOnyx<ContactMethodsPageProps, ContactMethodsPageOnyxProps>({
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
session: {
key: ONYXKEYS.SESSION,
},
})(ContactMethodsPage);