-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ActivatePhysicalCardPage.js
182 lines (159 loc) · 6.63 KB
/
ActivatePhysicalCardPage.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
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import BigNumberPad from '@components/BigNumberPad';
import Button from '@components/Button';
import IllustratedHeaderPageLayout from '@components/IllustratedHeaderPageLayout';
import LottieAnimations from '@components/LottieAnimations';
import MagicCodeInput from '@components/MagicCodeInput';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as CardUtils from '@libs/CardUtils';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import NotFoundPage from '@pages/ErrorPage/NotFoundPage';
import * as CardSettings from '@userActions/Card';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import assignedCardPropTypes from './assignedCardPropTypes';
const propTypes = {
/* Onyx Props */
/** The details about the Expensify cards */
cardList: PropTypes.objectOf(assignedCardPropTypes),
/** Navigation route context info provided by react navigation */
route: PropTypes.shape({
params: PropTypes.shape({
/** domain passed via route /settings/wallet/card/:domain */
domain: PropTypes.string,
}),
}).isRequired,
};
const defaultProps = {
cardList: {},
};
const LAST_FOUR_DIGITS_LENGTH = 4;
const MAGIC_INPUT_MIN_HEIGHT = 86;
function ActivatePhysicalCardPage({
cardList,
route: {
params: {domain},
},
}) {
const theme = useTheme();
const styles = useThemeStyles();
const {isExtraSmallScreenHeight} = useWindowDimensions();
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const [formError, setFormError] = useState('');
const [lastFourDigits, setLastFourDigits] = useState('');
const [lastPressedDigit, setLastPressedDigit] = useState('');
const domainCards = CardUtils.getDomainCards(cardList)[domain];
const physicalCard = _.find(domainCards, (card) => !card.isVirtual) || {};
const cardID = lodashGet(physicalCard, 'cardID', 0);
const cardError = ErrorUtils.getLatestErrorMessage(physicalCard);
const activateCardCodeInputRef = useRef(null);
/**
* If state of the card is CONST.EXPENSIFY_CARD.STATE.OPEN, navigate to card details screen.
*/
useEffect(() => {
if (physicalCard.isLoading || lodashGet(cardList, `${cardID}.state`, 0) !== CONST.EXPENSIFY_CARD.STATE.OPEN) {
return;
}
Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(domain));
}, [cardID, cardList, domain, physicalCard.isLoading]);
useEffect(
() => () => {
CardSettings.clearCardListErrors(cardID);
},
[cardID],
);
/**
* Update lastPressedDigit with value that was pressed on BigNumberPad.
*
* NOTE: If the same digit is pressed twice in a row, append it to the end of the string
* so that useEffect inside MagicCodeInput will be triggered by artificial change of the value.
*
* @param {String} key
*/
const updateLastPressedDigit = useCallback((key) => setLastPressedDigit(lastPressedDigit === key ? lastPressedDigit + key : key), [lastPressedDigit]);
/**
* Handle card activation code input
*
* @param {String} text
*/
const onCodeInput = (text) => {
setFormError('');
if (cardError) {
CardSettings.clearCardListErrors(cardID);
}
setLastFourDigits(text);
};
const submitAndNavigateToNextPage = useCallback(() => {
activateCardCodeInputRef.current.blur();
if (lastFourDigits.replace(CONST.MAGIC_CODE_EMPTY_CHAR, '').length !== LAST_FOUR_DIGITS_LENGTH) {
setFormError(translate('activateCardPage.error.thatDidntMatch'));
return;
}
CardSettings.activatePhysicalExpensifyCard(lastFourDigits, cardID);
}, [lastFourDigits, cardID, translate]);
if (_.isEmpty(physicalCard)) {
return <NotFoundPage />;
}
return (
<IllustratedHeaderPageLayout
title={translate('activateCardPage.activateCard')}
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(domain))}
backgroundColor={theme.PAGE_THEMES[SCREENS.SETTINGS.PREFERENCES.ROOT].backgroundColor}
illustration={LottieAnimations.Magician}
scrollViewContainerStyles={[styles.mnh100]}
childrenContainerStyles={[styles.flex1]}
>
<Text style={[styles.mh5, styles.textHeadline]}>{translate('activateCardPage.pleaseEnterLastFour')}</Text>
<View style={[styles.mh5, {minHeight: MAGIC_INPUT_MIN_HEIGHT}]}>
<MagicCodeInput
isDisableKeyboard
autoComplete="off"
maxLength={LAST_FOUR_DIGITS_LENGTH}
name="activateCardCode"
value={lastFourDigits}
lastPressedDigit={lastPressedDigit}
onChangeText={onCodeInput}
onFulfill={submitAndNavigateToNextPage}
errorText={formError || cardError}
ref={activateCardCodeInputRef}
/>
</View>
<View style={[styles.w100, styles.justifyContentEnd, styles.pageWrapper, styles.pv0]}>
{DeviceCapabilities.canUseTouchScreen() && <BigNumberPad numberPressed={updateLastPressedDigit} />}
</View>
<Button
success
isDisabled={isOffline}
isLoading={physicalCard.isLoading}
medium={isExtraSmallScreenHeight}
style={[styles.w100, styles.p5, styles.mtAuto]}
onPress={submitAndNavigateToNextPage}
pressOnEnter
text={translate('activateCardPage.activatePhysicalCard')}
/>
</IllustratedHeaderPageLayout>
);
}
ActivatePhysicalCardPage.propTypes = propTypes;
ActivatePhysicalCardPage.defaultProps = defaultProps;
ActivatePhysicalCardPage.displayName = 'ActivatePhysicalCardPage';
export default withOnyx({
cardList: {
key: ONYXKEYS.CARD_LIST,
},
})(ActivatePhysicalCardPage);