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

Migrate IOURequestStepCurrency to TypeScript #40100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions src/libs/Navigation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ type MoneyRequestNavigatorParamList = {
pageIndex: number;
backTo: Routes;
};
[SCREENS.MONEY_REQUEST.STEP_CURRENCY]: {
action: ValueOf<typeof CONST.IOU.ACTION>;
iouType: ValueOf<typeof CONST.IOU.TYPE>;
transactionID: string;
reportID: string;
pageIndex?: string;
backTo?: Routes;
};
};

type NewTaskNavigatorParamList = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
import Str from 'expensify-common/lib/str';
import lodashGet from 'lodash/get';
import PropTypes from 'prop-types';
import React, {useMemo, useRef, useState} from 'react';
import React, {useMemo, useState} from 'react';
import {Keyboard} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import type {OnyxEntry} from 'react-native-onyx';
import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/RadioListItem';
import transactionPropTypes from '@components/transactionPropTypes';
import type {ListItem} from '@components/SelectionList/types';
import useLocalize from '@hooks/useLocalize';
import compose from '@libs/compose';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as ReportUtils from '@libs/ReportUtils';
import * as IOU from '@userActions/IOU';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES, {getUrlWithBackToParam} from '@src/ROUTES';
import IOURequestStepRoutePropTypes from './IOURequestStepRoutePropTypes';
import type {Route} from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {CurrencyList, Transaction} from '@src/types/onyx';
import StepScreenWrapper from './StepScreenWrapper';
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound';

/**
* IOU Currency selection for selecting currency
*/
const propTypes = {
/** Navigation route context info provided by react navigation */
route: IOURequestStepRoutePropTypes.isRequired,
type IOURequestStepCurrencyOnyxProps = {
/** Constant, list of available currencies */
currencyList: OnyxEntry<CurrencyList>;

/** The currency list constant object from Onyx */
currencyList: PropTypes.objectOf(
PropTypes.shape({
/** Symbol for the currency */
symbol: PropTypes.string,

/** Name of the currency */
name: PropTypes.string,

/** ISO4217 Code for the currency */
ISO4217: PropTypes.string,
}),
),

/* Onyx Props */
/** The draft transaction object being modified in Onyx */
draftTransaction: transactionPropTypes,
draftTransaction: OnyxEntry<Transaction>;
};

const defaultProps = {
currencyList: {},
draftTransaction: {},
type IOURequestStepCurrencyProps = IOURequestStepCurrencyOnyxProps & WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_CURRENCY>;

type CurrencyListItem = ListItem & {
currencyName: string;
currencyCode: string;
};

function IOURequestStepCurrency({
Expand All @@ -58,11 +42,10 @@ function IOURequestStepCurrency({
params: {backTo, iouType, pageIndex, reportID, transactionID, action},
},
draftTransaction,
}) {
}: IOURequestStepCurrencyProps) {
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');
const optionsSelectorRef = useRef();
const {currency} = ReportUtils.getTransactionDetails(draftTransaction);
const {currency = ''} = ReportUtils.getTransactionDetails(draftTransaction) ?? {};

const navigateBack = () => {
// If the currency selection was done from the confirmation step (eg. + > request money > manual > confirm > amount > currency)
Expand All @@ -71,30 +54,26 @@ function IOURequestStepCurrency({
// to the confirmation page
if (pageIndex === 'confirm') {
const routeToAmountPageWithConfirmationAsBackTo = getUrlWithBackToParam(
backTo,
backTo as string,
`/${ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, iouType, transactionID, reportID)}`,
);
Navigation.goBack(routeToAmountPageWithConfirmationAsBackTo);
Navigation.goBack(routeToAmountPageWithConfirmationAsBackTo as Route);
return;
}
Navigation.goBack(backTo);
};

/**
* @param {Object} option
* @param {String} options.currencyCode
*/
const confirmCurrencySelection = (option) => {
const confirmCurrencySelection = (option: CurrencyListItem) => {
Keyboard.dismiss();
IOU.setMoneyRequestCurrency_temporaryForRefactor(transactionID, option.currencyCode, false, action === CONST.IOU.ACTION.EDIT);
navigateBack();
};

const {sections, headerMessage, initiallyFocusedOptionKey} = useMemo(() => {
const currencyOptions = _.map(currencyList, (currencyInfo, currencyCode) => {
const currencyOptions: CurrencyListItem[] = Object.entries(currencyList ?? {}).map(([currencyCode, currencyInfo]) => {
const isSelectedCurrency = currencyCode === currency.toUpperCase();
return {
currencyName: currencyInfo.name,
currencyName: currencyInfo?.name ?? '',
text: `${currencyCode} - ${CurrencyUtils.getLocalizedCurrencySymbol(currencyCode)}`,
currencyCode,
keyForList: currencyCode,
Expand All @@ -103,14 +82,11 @@ function IOURequestStepCurrency({
});

const searchRegex = new RegExp(Str.escapeForRegExp(searchValue.trim()), 'i');
const filteredCurrencies = _.filter(currencyOptions, (currencyOption) => searchRegex.test(currencyOption.text) || searchRegex.test(currencyOption.currencyName));
const filteredCurrencies = currencyOptions.filter((currencyOption) => searchRegex.test(currencyOption.text ?? '') || searchRegex.test(currencyOption.currencyName));
const isEmpty = searchValue.trim() && !filteredCurrencies.length;

return {
initiallyFocusedOptionKey: _.get(
_.find(filteredCurrencies, (filteredCurrency) => filteredCurrency.currencyCode === currency.toUpperCase()),
'keyForList',
),
initiallyFocusedOptionKey: filteredCurrencies.find((filteredCurrency) => filteredCurrency.currencyCode === currency.toUpperCase())?.keyForList,
sections: isEmpty
? []
: [
Expand All @@ -126,7 +102,6 @@ function IOURequestStepCurrency({
<StepScreenWrapper
headerTitle={translate('common.selectCurrency')}
onBackButtonPress={navigateBack}
onEntryTransitionEnd={() => optionsSelectorRef.current && optionsSelectorRef.current.focus()}
shouldShowWrapper
testID={IOURequestStepCurrency.displayName}
includeSafeAreaPaddingBottom={false}
Expand Down Expand Up @@ -154,18 +129,18 @@ function IOURequestStepCurrency({
}

IOURequestStepCurrency.displayName = 'IOURequestStepCurrency';
IOURequestStepCurrency.propTypes = propTypes;
IOURequestStepCurrency.defaultProps = defaultProps;

export default compose(
withFullTransactionOrNotFound,
withOnyx({
currencyList: {key: ONYXKEYS.CURRENCY_LIST},
draftTransaction: {
key: ({route}) => {
const transactionID = lodashGet(route, 'params.transactionID', 0);
return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`;
},
const IOURequestStepCurrencyWithOnyx = withOnyx<IOURequestStepCurrencyProps, IOURequestStepCurrencyOnyxProps>({
currencyList: {key: ONYXKEYS.CURRENCY_LIST},
draftTransaction: {
key: ({route}) => {
const transactionID = route?.params?.transactionID ?? 0;
return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`;
},
}),
)(IOURequestStepCurrency);
},
})(IOURequestStepCurrency);

/* eslint-disable rulesdir/no-negated-variables */
const IOURequestStepCurrencyWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepCurrencyWithOnyx);

export default IOURequestStepCurrencyWithFullTransactionOrNotFound;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type MoneyRequestRouteName =
| typeof SCREENS.MONEY_REQUEST.STEP_CONFIRMATION
| typeof SCREENS.MONEY_REQUEST.STEP_CATEGORY
| typeof SCREENS.MONEY_REQUEST.STEP_TAX_RATE
| typeof SCREENS.MONEY_REQUEST.STEP_SCAN;
| typeof SCREENS.MONEY_REQUEST.STEP_SCAN
| typeof SCREENS.MONEY_REQUEST.STEP_CURRENCY;

type Route<T extends MoneyRequestRouteName> = RouteProp<MoneyRequestNavigatorParamList, T>;

Expand Down
Loading