Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate SettingsWallet page to TypeScript #34716

Merged
merged 42 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a7fbd97
TS migration SettingsWallet
tienifr Jan 18, 2024
47ea3ac
use StackScreenProps type
tienifr Jan 18, 2024
9506d82
add commen
tienifr Jan 18, 2024
a15c596
fix lint
tienifr Jan 18, 2024
b848510
TS migrate ActivatePhysicalCardPage
tienifr Jan 18, 2024
a532fc8
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Jan 18, 2024
14951ed
TS migrate AddDebitCardPage
tienifr Jan 18, 2024
44853e7
fix lint
tienifr Jan 18, 2024
40b2601
TS migrate ReportCardLostPage
tienifr Jan 18, 2024
73095a2
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Jan 18, 2024
ff51244
TS migrate TransferBalancePage
tienifr Jan 18, 2024
137ff59
fix lint
tienifr Jan 18, 2024
48d470a
TS migrate ExpensifyCardPage
tienifr Jan 18, 2024
65446ab
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Jan 19, 2024
7b7906c
Fix Form TS error
tienifr Jan 19, 2024
3b5d9ee
Merge branch 'main' into ts-migration/32003
tienifr Jan 19, 2024
9cfedfe
Fix lint and type
tienifr Jan 19, 2024
021be80
remove redundant check
tienifr Jan 19, 2024
47d7f81
Merge branch 'main' into ts-migration/32003
tienifr Jan 23, 2024
9365b3d
add comment and fix minor errors
tienifr Jan 23, 2024
67001c6
Merge branch 'main' into ts-migration/32003
tienifr Jan 25, 2024
d3a9997
Merge branch 'main' into ts-migration/32003
tienifr Jan 26, 2024
5ff1569
Merge branch 'main' into ts-migration/32003
tienifr Feb 16, 2024
6368b04
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Feb 16, 2024
3c59cfe
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Feb 17, 2024
7acd0af
Resolve conflicts
tienifr Feb 17, 2024
6439290
fix type error
tienifr Feb 17, 2024
23bc723
fix lint
tienifr Feb 17, 2024
48d5b09
remove unnecessary changes
tienifr Feb 17, 2024
6e01c60
fix type error
tienifr Feb 17, 2024
c15a756
fix lint
tienifr Feb 17, 2024
923e1a0
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Feb 19, 2024
779a231
use or instead of nullish coalescing
tienifr Feb 19, 2024
aced7a7
Merge branch 'main' into ts-migration/32003
tienifr Feb 20, 2024
8ffbbe1
Fix typecheck
tienifr Feb 20, 2024
79b29cc
Merge branch 'main' into ts-migration/32003
tienifr Feb 22, 2024
d6c3570
fix crash in activate physical card page
tienifr Feb 22, 2024
9d27a52
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Feb 22, 2024
b606e21
Merge branch 'main' into ts-migration/32003
tienifr Feb 23, 2024
1ddf781
Merge branch 'main' of https://github.com/tienifr/App into ts-migrati…
tienifr Feb 23, 2024
befbda6
fix typecheck
tienifr Feb 23, 2024
b8caf36
fix: selected icon not show for selected account
tienifr Feb 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,8 @@ type OnyxValues = {
[ONYXKEYS.FORMS.REPORT_VIRTUAL_CARD_FRAUD_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.REPORT_PHYSICAL_CARD_FORM]: OnyxTypes.Form;
[ONYXKEYS.FORMS.REPORT_PHYSICAL_CARD_FORM_DRAFT]: OnyxTypes.Form;
[ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM]: OnyxTypes.Form;
[ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM_DRAFT]: OnyxTypes.Form | undefined;
[ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM]: OnyxTypes.GetPhysicalCardForm;
[ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM_DRAFT]: OnyxTypes.GetPhysicalCardForm | undefined;
};

type OnyxKeyValue<TOnyxKey extends (OnyxKey | OnyxCollectionKey) & keyof OnyxValues> = OnyxEntry<OnyxValues[TOnyxKey]>;
Expand Down
2 changes: 1 addition & 1 deletion src/components/BigNumberPad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type BigNumberPadProps = {
id?: string;

/** Whether long press is disabled */
isLongPressDisabled: boolean;
isLongPressDisabled?: boolean;
};

const padNumbers = [
Expand Down
2 changes: 1 addition & 1 deletion src/languages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type EnterMagicCodeParams = {contactMethod: string};

type TransferParams = {amount: string};

type InstantSummaryParams = {rate: number; minAmount: number};
type InstantSummaryParams = {rate: string; minAmount: string};

type NotYouParams = {user: string};

Expand Down
60 changes: 25 additions & 35 deletions src/libs/GetPhysicalCardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import * as PersonalDetailsUtils from './PersonalDetailsUtils';
import * as UserUtils from './UserUtils';

type DraftValues = {
addressLine1: string;
addressLine2: string;
city: string;
country: string;
legalFirstName: string;
legalLastName: string;
phoneNumber: string;
state: string;
zipPostCode: string;
addressLine1?: string;
addressLine2?: string;
city?: string;
country?: string;
legalFirstName?: string;
legalLastName?: string;
phoneNumber?: string;
state?: string;
zipPostCode?: string;
};

type PrivatePersonalDetails = {
address: {street: string; city: string; state: string; country: string; zip: string};
legalFirstName: string;
legalLastName: string;
phoneNumber: string;
address?: {street: string; city: string; state: string; country: string; zip: string};
legalFirstName?: string;
legalLastName?: string;
phoneNumber?: string;
};

type LoginList = Record<string, Login>;
Expand All @@ -33,12 +33,7 @@ type LoginList = Record<string, Login>;
* @returns
*/
function getCurrentRoute(domain: string, privatePersonalDetails: PrivatePersonalDetails, loginList: LoginList) {
const {
address: {street, city, state, country, zip},
legalFirstName,
legalLastName,
phoneNumber,
} = privatePersonalDetails;
const {address: {city, country, state, street, zip} = {}, legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails;

if (!legalFirstName && !legalLastName) {
return ROUTES.SETTINGS_WALLET_CARD_GET_PHYSICAL_NAME.getRoute(domain);
Expand Down Expand Up @@ -91,23 +86,18 @@ function setCurrentRoute(currentRoute: string, domain: string, privatePersonalDe
* @returns
*/
function getUpdatedDraftValues(draftValues: DraftValues, privatePersonalDetails: PrivatePersonalDetails, loginList: LoginList) {
const {
address: {city, country, state, street = '', zip},
legalFirstName,
legalLastName,
phoneNumber,
} = privatePersonalDetails;
const {address: {city, country, state, street, zip} = {}, legalFirstName, legalLastName, phoneNumber} = privatePersonalDetails;

return {
legalFirstName: draftValues.legalFirstName || legalFirstName,
legalLastName: draftValues.legalLastName || legalLastName,
addressLine1: draftValues.addressLine1 || street.split('\n')[0],
addressLine2: draftValues.addressLine2 || street.split('\n')[1] || '',
city: draftValues.city || city,
country: draftValues.country || country,
phoneNumber: draftValues.phoneNumber || (phoneNumber ?? UserUtils.getSecondaryPhoneLogin(loginList) ?? ''),
state: draftValues.state || state,
zipPostCode: draftValues.zipPostCode || zip,
legalFirstName: draftValues.legalFirstName ?? legalFirstName,
legalLastName: draftValues.legalLastName ?? legalLastName,
addressLine1: draftValues.addressLine1 ?? street?.split('\n')[0] ?? '',
addressLine2: draftValues.addressLine2 ?? street?.split('\n')[1] ?? '',
city: draftValues.city ?? city,
country: draftValues.country ?? country,
phoneNumber: draftValues.phoneNumber ?? phoneNumber ?? UserUtils.getSecondaryPhoneLogin(loginList) ?? '',
state: draftValues.state ?? state,
zipPostCode: draftValues.zipPostCode ?? zip,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause regression. draftValues can be empty string.

Lets hold this PR for #33641

};
}

Expand All @@ -117,7 +107,7 @@ function getUpdatedDraftValues(draftValues: DraftValues, privatePersonalDetails:
* @returns
*/
function getUpdatedPrivatePersonalDetails(draftValues: DraftValues): PrivatePersonalDetails {
const {addressLine1, addressLine2, city, country, legalFirstName, legalLastName, phoneNumber, state, zipPostCode} = draftValues;
const {addressLine1, addressLine2, city = '', country = '', legalFirstName, legalLastName, phoneNumber, state = '', zipPostCode = ''} = draftValues;
return {
legalFirstName,
legalLastName,
Expand Down
1 change: 1 addition & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ type PublicScreensParamList = {
shortLivedAuthToken?: string;
shortLivedToken?: string;
exitTo?: Routes;
domain?: Routes;
};
[SCREENS.VALIDATE_LOGIN]: {
accountID: string;
Expand Down
1 change: 1 addition & 0 deletions src/libs/actions/Card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,4 @@ function revealVirtualCardDetails(cardID: number): Promise<Response> {
}

export {requestReplacementExpensifyCard, activatePhysicalExpensifyCard, clearCardListErrors, reportVirtualExpensifyCardFraud, revealVirtualCardDetails};
export type {ReplacementReason};
7 changes: 1 addition & 6 deletions src/libs/actions/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,7 @@ function answerQuestionsForWallet(answers: WalletQuestionAnswer[], idNumber: str
}

function requestPhysicalExpensifyCard(cardID: number, authToken: string, privatePersonalDetails: PrivatePersonalDetails) {
const {
legalFirstName,
legalLastName,
phoneNumber,
address: {city, country, state, street, zip},
} = privatePersonalDetails;
const {legalFirstName = '', legalLastName = '', phoneNumber = '', address: {city = '', country = '', state = '', street = '', zip = ''} = {}} = privatePersonalDetails;

type RequestPhysicalExpensifyCardParams = {
authToken: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import type {StackScreenProps} from '@react-navigation/stack';
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 _ from 'underscore';
import BigNumberPad from '@components/BigNumberPad';
import Button from '@components/Button';
import IllustratedHeaderPageLayout from '@components/IllustratedHeaderPageLayout';
Expand All @@ -19,42 +18,32 @@ 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 type {PublicScreensParamList} from '@libs/Navigation/types';
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';
import type {Card} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

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,
type ActivatePhysicalCardPageOnyxProps = {
/** Card list propTypes */
cardList: OnyxEntry<Record<string, Card>>;
};

const defaultProps = {
cardList: {},
};
type ActivatePhysicalCardPageProps = ActivatePhysicalCardPageOnyxProps & StackScreenProps<PublicScreensParamList, typeof SCREENS.TRANSITION_BETWEEN_APPS>;

const LAST_FOUR_DIGITS_LENGTH = 4;
const MAGIC_INPUT_MIN_HEIGHT = 86;

function ActivatePhysicalCardPage({
cardList,
cardList = {},
route: {
params: {domain},
params: {domain = ''},
},
}) {
}: ActivatePhysicalCardPageProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {isExtraSmallScreenHeight} = useWindowDimensions();
Expand All @@ -65,23 +54,24 @@ function ActivatePhysicalCardPage({
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 domainCards = CardUtils.getDomainCards(cardList ?? {})[domain];
const physicalCard = domainCards.find((card) => card.isVirtual);
const cardID = physicalCard?.cardID ?? 0;
tienifr marked this conversation as resolved.
Show resolved Hide resolved
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) {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (physicalCard?.isLoading || (cardList && cardList[cardID]?.state !== CONST.EXPENSIFY_CARD.STATE.OPEN)) {
return;
}

Navigation.navigate(ROUTES.SETTINGS_WALLET_DOMAINCARD.getRoute(domain));
}, [cardID, cardList, domain, physicalCard.isLoading]);
}, [cardID, cardList, domain, physicalCard?.isLoading]);

useEffect(
() => () => {
Expand All @@ -95,17 +85,13 @@ function ActivatePhysicalCardPage({
*
* 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]);
const updateLastPressedDigit = useCallback((key: string) => setLastPressedDigit(lastPressedDigit === key ? lastPressedDigit + key : key), [lastPressedDigit]);

/**
* Handle card activation code input
*
* @param {String} text
*/
const onCodeInput = (text) => {
const onCodeInput = (text: string) => {
setFormError('');

if (cardError) {
Expand All @@ -116,7 +102,8 @@ function ActivatePhysicalCardPage({
};

const submitAndNavigateToNextPage = useCallback(() => {
activateCardCodeInputRef.current.blur();
// @ts-expect-error TODO: Remove this once MagicCodeInput (https://github.com/Expensify/App/issues/25078) is migrated to TypeScript.
activateCardCodeInputRef.current?.blur();

if (lastFourDigits.replace(CONST.MAGIC_CODE_EMPTY_CHAR, '').length !== LAST_FOUR_DIGITS_LENGTH) {
setFormError(translate('activateCardPage.error.thatDidntMatch'));
Expand All @@ -126,7 +113,7 @@ function ActivatePhysicalCardPage({
CardSettings.activatePhysicalExpensifyCard(lastFourDigits, cardID);
}, [lastFourDigits, cardID, translate]);

if (_.isEmpty(physicalCard)) {
if (isEmptyObject(physicalCard)) {
return <NotFoundPage />;
}

Expand All @@ -142,6 +129,7 @@ function ActivatePhysicalCardPage({
<Text style={[styles.mh5, styles.textHeadline]}>{translate('activateCardPage.pleaseEnterLastFour')}</Text>
<View style={[styles.mh5, {minHeight: MAGIC_INPUT_MIN_HEIGHT}]}>
<MagicCodeInput
// @ts-expect-error TODO: Remove this once MagicCodeInput (https://github.com/Expensify/App/issues/25078) is migrated to TypeScript.
isDisableKeyboard
autoComplete="off"
maxLength={LAST_FOUR_DIGITS_LENGTH}
Expand All @@ -160,7 +148,7 @@ function ActivatePhysicalCardPage({
<Button
success
isDisabled={isOffline}
isLoading={physicalCard.isLoading}
isLoading={physicalCard?.isLoading}
medium={isExtraSmallScreenHeight}
style={[styles.w100, styles.p5, styles.mtAuto]}
onPress={submitAndNavigateToNextPage}
Expand All @@ -171,11 +159,9 @@ function ActivatePhysicalCardPage({
);
}

ActivatePhysicalCardPage.propTypes = propTypes;
ActivatePhysicalCardPage.defaultProps = defaultProps;
ActivatePhysicalCardPage.displayName = 'ActivatePhysicalCardPage';

export default withOnyx({
export default withOnyx<ActivatePhysicalCardPageProps, ActivatePhysicalCardPageOnyxProps>({
cardList: {
key: ONYXKEYS.CARD_LIST,
},
Expand Down
Loading
Loading