-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
InitialSettingsPage.js
executable file
·369 lines (332 loc) · 15 KB
/
InitialSettingsPage.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import lodashGet from 'lodash/get';
import React from 'react';
import {View, ScrollView, Pressable} from 'react-native';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import {withNetwork} from '../../components/OnyxProvider';
import styles from '../../styles/styles';
import Text from '../../components/Text';
import * as Session from '../../libs/actions/Session';
import ONYXKEYS from '../../ONYXKEYS';
import Tooltip from '../../components/Tooltip';
import Avatar from '../../components/Avatar';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import Navigation from '../../libs/Navigation/Navigation';
import * as Expensicons from '../../components/Icon/Expensicons';
import ScreenWrapper from '../../components/ScreenWrapper';
import MenuItem from '../../components/MenuItem';
import ROUTES from '../../ROUTES';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import CONST from '../../CONST';
import Permissions from '../../libs/Permissions';
import * as App from '../../libs/actions/App';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../components/withCurrentUserPersonalDetails';
import * as PaymentMethods from '../../libs/actions/PaymentMethods';
import bankAccountPropTypes from '../../components/bankAccountPropTypes';
import cardPropTypes from '../../components/cardPropTypes';
import * as Wallet from '../../libs/actions/Wallet';
import walletTermsPropTypes from '../EnablePayments/walletTermsPropTypes';
import * as PolicyUtils from '../../libs/PolicyUtils';
import ConfirmModal from '../../components/ConfirmModal';
import * as ReportUtils from '../../libs/ReportUtils';
import * as Link from '../../libs/actions/Link';
import OfflineWithFeedback from '../../components/OfflineWithFeedback';
import * as UserUtils from '../../libs/UserUtils';
import policyMemberPropType from '../policyMemberPropType';
const propTypes = {
/* Onyx Props */
/** The session of the logged in person */
session: PropTypes.shape({
/** Email of the logged in person */
email: PropTypes.string,
}),
/** The list of this user's policies */
policies: PropTypes.objectOf(PropTypes.shape({
/** The ID of the policy */
ID: PropTypes.string,
/** The name of the policy */
name: PropTypes.string,
/** The type of the policy */
type: PropTypes.string,
/** The user's role in the policy */
role: PropTypes.string,
/** The current action that is waiting to happen on the policy */
pendingAction: PropTypes.oneOf(_.values(CONST.RED_BRICK_ROAD_PENDING_ACTION)),
})),
/** The user's wallet account */
userWallet: PropTypes.shape({
/** The user's current wallet balance */
currentBalance: PropTypes.number,
}),
/** List of bank accounts */
bankAccountList: PropTypes.objectOf(bankAccountPropTypes),
/** List of cards */
cardList: PropTypes.objectOf(cardPropTypes),
/** List of betas available to current user */
betas: PropTypes.arrayOf(PropTypes.string),
/** Information about the user accepting the terms for payments */
walletTerms: walletTermsPropTypes,
/** Login list for the user that is signed in */
loginList: PropTypes.shape({
/** Date login was validated, used to show brickroad info status */
validatedDate: PropTypes.string,
/** Field-specific server side errors keyed by microtime */
errorFields: PropTypes.objectOf(PropTypes.objectOf(PropTypes.string)),
}),
/** List of policy members */
policyMembers: PropTypes.objectOf(policyMemberPropType),
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
session: {},
policies: {},
userWallet: {
currentBalance: 0,
},
betas: [],
walletTerms: {},
bankAccountList: {},
cardList: {},
loginList: {},
policyMembers: {},
...withCurrentUserPersonalDetailsDefaultProps,
};
class InitialSettingsPage extends React.Component {
constructor(props) {
super(props);
this.getWalletBalance = this.getWalletBalance.bind(this);
this.getDefaultMenuItems = this.getDefaultMenuItems.bind(this);
this.getMenuItem = this.getMenuItem.bind(this);
this.toggleSignoutConfirmModal = this.toggleSignoutConfirmModal.bind(this);
this.signout = this.signOut.bind(this);
this.state = {
shouldShowSignoutConfirmModal: false,
};
}
componentDidMount() {
Wallet.openInitialSettingsPage();
}
/**
* @param {Boolean} isPaymentItem whether the item being rendered is the payments menu item
* @returns {Number} the user wallet balance
*/
getWalletBalance(isPaymentItem) {
return (isPaymentItem && Permissions.canUseWallet(this.props.betas))
? this.props.numberFormat(
this.props.userWallet.currentBalance / 100, // Divide by 100 because balance is in cents
{style: 'currency', currency: 'USD'},
) : undefined;
}
/**
* Retuns a list of default menu items
* @returns {Array} the default menu items
*/
getDefaultMenuItems() {
const policiesAvatars = _.chain(this.props.policies)
.filter(policy => PolicyUtils.shouldShowPolicy(policy, this.props.network.isOffline))
.sortBy(policy => policy.name)
.map(policy => ({
source: policy.avatar || ReportUtils.getDefaultWorkspaceAvatar(policy.name),
name: policy.name,
type: CONST.ICON_TYPE_WORKSPACE,
}))
.value();
const policyBrickRoadIndicator = _.chain(this.props.policies)
.filter(policy => policy && policy.type === CONST.POLICY.TYPE.FREE && policy.role === CONST.POLICY.ROLE.ADMIN)
.find(policy => PolicyUtils.hasPolicyError(policy) || PolicyUtils.getPolicyBrickRoadIndicatorStatus(policy, this.props.policyMembers))
.value() ? 'error' : null;
const profileBrickRoadIndicator = UserUtils.getLoginListBrickRoadIndicator(this.props.loginList);
return ([
{
translationKey: 'common.workspaces',
icon: Expensicons.Building,
action: () => { Navigation.navigate(ROUTES.SETTINGS_WORKSPACES); },
floatRightAvatars: policiesAvatars,
shouldStackHorizontally: true,
brickRoadIndicator: policyBrickRoadIndicator,
},
{
translationKey: 'common.profile',
icon: Expensicons.Profile,
action: () => { App.openProfile(); },
brickRoadIndicator: profileBrickRoadIndicator,
},
{
translationKey: 'common.preferences',
icon: Expensicons.Gear,
action: () => { Navigation.navigate(ROUTES.SETTINGS_PREFERENCES); },
},
{
translationKey: 'initialSettingsPage.security',
icon: Expensicons.Lock,
action: () => { Navigation.navigate(ROUTES.SETTINGS_SECURITY); },
},
{
translationKey: 'common.payments',
icon: Expensicons.Wallet,
action: () => { Navigation.navigate(ROUTES.SETTINGS_PAYMENTS); },
brickRoadIndicator: PaymentMethods.hasPaymentMethodError(this.props.bankAccountList, this.props.cardList) || !_.isEmpty(this.props.userWallet.errors)
|| !_.isEmpty(this.props.walletTerms.errors) ? 'error' : null,
},
{
translationKey: 'initialSettingsPage.help',
icon: Expensicons.QuestionMark,
action: () => { Link.openExternalLink(CONST.NEWHELP_URL); },
shouldShowRightIcon: true,
iconRight: Expensicons.NewWindow,
},
{
translationKey: 'initialSettingsPage.about',
icon: Expensicons.Info,
action: () => { Navigation.navigate(ROUTES.SETTINGS_ABOUT); },
},
{
translationKey: 'initialSettingsPage.signOut',
icon: Expensicons.Exit,
action: () => { this.signout(false); },
},
]);
}
getMenuItem(item, index) {
const keyTitle = item.translationKey ? this.props.translate(item.translationKey) : item.title;
const isPaymentItem = item.translationKey === 'common.payments';
return (
<MenuItem
key={`${keyTitle}_${index}`}
title={keyTitle}
icon={item.icon}
iconType={item.iconType}
onPress={item.action}
iconStyles={item.iconStyles}
shouldShowRightIcon
iconRight={item.iconRight}
badgeText={this.getWalletBalance(isPaymentItem)}
fallbackIcon={item.fallbackIcon}
brickRoadIndicator={item.brickRoadIndicator}
floatRightAvatars={item.floatRightAvatars}
shouldStackHorizontally={item.shouldStackHorizontally}
/>
);
}
toggleSignoutConfirmModal(value) {
this.setState({shouldShowSignoutConfirmModal: value});
}
signOut(shouldForceSignout = false) {
if (!this.props.network.isOffline || shouldForceSignout) {
Session.signOutAndRedirectToSignIn();
return;
}
// When offline, warn the user that any actions they took while offline will be lost if they sign out
this.toggleSignoutConfirmModal(true);
}
openProfileSettings() {
Navigation.navigate(ROUTES.SETTINGS_PROFILE);
}
render() {
// On the very first sign in or after clearing storage these
// details will not be present on the first render so we'll just
// return nothing for now.
if (_.isEmpty(this.props.currentUserPersonalDetails)) {
return null;
}
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
{({safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithCloseButton
title={this.props.translate('common.settings')}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<ScrollView contentContainerStyle={safeAreaPaddingBottomStyle} style={[styles.settingsPageBackground]}>
<View style={styles.w100}>
<View style={styles.avatarSectionWrapper}>
<Pressable style={[styles.mb3]} onPress={this.openProfileSettings}>
<Tooltip text={this.props.translate('common.profile')}>
<OfflineWithFeedback
pendingAction={lodashGet(this.props.currentUserPersonalDetails, 'pendingFields.avatar', null)}
>
<Avatar
imageStyles={[styles.avatarLarge]}
source={ReportUtils.getAvatar(this.props.currentUserPersonalDetails.avatar, this.props.session.email)}
size={CONST.AVATAR_SIZE.LARGE}
/>
</OfflineWithFeedback>
</Tooltip>
</Pressable>
<Pressable style={[styles.mt1, styles.mw100]} onPress={this.openProfileSettings}>
<Tooltip text={this.props.translate('common.profile')}>
<Text style={[styles.textHeadline, styles.pre]} numberOfLines={1}>
{this.props.currentUserPersonalDetails.displayName
? this.props.currentUserPersonalDetails.displayName
: Str.removeSMSDomain(this.props.session.email)}
</Text>
</Tooltip>
</Pressable>
{this.props.currentUserPersonalDetails.displayName && (
<Text
style={[styles.textLabelSupporting, styles.mt1]}
numberOfLines={1}
>
{Str.removeSMSDomain(this.props.session.email)}
</Text>
)}
</View>
{_.map(this.getDefaultMenuItems(), (item, index) => this.getMenuItem(item, index))}
<ConfirmModal
danger
title={this.props.translate('common.areYouSure')}
prompt={this.props.translate('initialSettingsPage.signOutConfirmationText')}
confirmText={this.props.translate('initialSettingsPage.signOut')}
cancelText={this.props.translate('common.cancel')}
isVisible={this.state.shouldShowSignoutConfirmModal}
onConfirm={() => this.signOut(true)}
onCancel={() => this.toggleSignoutConfirmModal(false)}
/>
</View>
</ScrollView>
</>
)}
</ScreenWrapper>
);
}
}
InitialSettingsPage.propTypes = propTypes;
InitialSettingsPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withCurrentUserPersonalDetails,
withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
policyMembers: {
key: ONYXKEYS.COLLECTION.POLICY_MEMBER_LIST,
},
userWallet: {
key: ONYXKEYS.USER_WALLET,
},
betas: {
key: ONYXKEYS.BETAS,
},
bankAccountList: {
key: ONYXKEYS.BANK_ACCOUNT_LIST,
},
cardList: {
key: ONYXKEYS.CARD_LIST,
},
walletTerms: {
key: ONYXKEYS.WALLET_TERMS,
},
loginList: {
key: ONYXKEYS.LOGIN_LIST,
},
}),
withNetwork(),
)(InitialSettingsPage);