Skip to content

Commit

Permalink
fix and migrate to useOnyx
Browse files Browse the repository at this point in the history
  • Loading branch information
nyomanjyotisa committed Nov 14, 2024
1 parent 55a077a commit 5b131b5
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/pages/settings/Wallet/ActivatePhysicalCardPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useCallback, useEffect, useLayoutEffect, useRef, useState} from 'react';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
import BigNumberPad from '@components/BigNumberPad';
import Button from '@components/Button';
import IllustratedHeaderPageLayout from '@components/IllustratedHeaderPageLayout';
Expand All @@ -25,21 +24,14 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import type {Card} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type ActivatePhysicalCardPageOnyxProps = {
/** Card list propTypes */
cardList: OnyxEntry<Record<string, Card>>;
};

type ActivatePhysicalCardPageProps = ActivatePhysicalCardPageOnyxProps & StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.WALLET.CARD_ACTIVATE>;
type ActivatePhysicalCardPageProps = StackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.WALLET.CARD_ACTIVATE>;

const LAST_FOUR_DIGITS_LENGTH = 4;
const MAGIC_INPUT_MIN_HEIGHT = 86;

function ActivatePhysicalCardPage({
cardList,
route: {
params: {cardID = ''},
},
Expand All @@ -49,10 +41,12 @@ function ActivatePhysicalCardPage({
const {isExtraSmallScreenHeight} = useResponsiveLayout();
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);

const [formError, setFormError] = useState('');
const [lastFourDigits, setLastFourDigits] = useState('');
const [lastPressedDigit, setLastPressedDigit] = useState('');
const [shouldShowError, setShouldShowError] = useState<boolean>(false);

const inactiveCard = cardList?.[cardID];
const cardError = ErrorUtils.getLatestErrorMessage(inactiveCard ?? {});
Expand All @@ -70,7 +64,7 @@ function ActivatePhysicalCardPage({
Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(cardID));
}, [cardID, cardList, inactiveCard?.isLoading, inactiveCard?.state]);

useLayoutEffect(() => {
useEffect(() => {
if (!inactiveCard?.cardID) {
return;
}
Expand Down Expand Up @@ -106,6 +100,7 @@ function ActivatePhysicalCardPage({
};

const submitAndNavigateToNextPage = useCallback(() => {
setShouldShowError(true);
activateCardCodeInputRef.current?.blur();

if (lastFourDigits.replace(CONST.MAGIC_CODE_EMPTY_CHAR, '').length !== LAST_FOUR_DIGITS_LENGTH) {
Expand Down Expand Up @@ -144,7 +139,7 @@ function ActivatePhysicalCardPage({
lastPressedDigit={lastPressedDigit}
onChangeText={onCodeInput}
onFulfill={submitAndNavigateToNextPage}
errorText={formError || cardError}
errorText={shouldShowError ? formError || cardError : ''}
ref={activateCardCodeInputRef}
/>
</View>
Expand All @@ -168,8 +163,4 @@ function ActivatePhysicalCardPage({

ActivatePhysicalCardPage.displayName = 'ActivatePhysicalCardPage';

export default withOnyx<ActivatePhysicalCardPageProps, ActivatePhysicalCardPageOnyxProps>({
cardList: {
key: ONYXKEYS.CARD_LIST,
},
})(ActivatePhysicalCardPage);
export default ActivatePhysicalCardPage;

0 comments on commit 5b131b5

Please sign in to comment.