diff --git a/src/pages/iou/request/step/IOURequestStepCurrency.tsx b/src/pages/iou/request/step/IOURequestStepCurrency.tsx index 813055c7b1c0..aac259caea38 100644 --- a/src/pages/iou/request/step/IOURequestStepCurrency.tsx +++ b/src/pages/iou/request/step/IOURequestStepCurrency.tsx @@ -1,42 +1,33 @@ import React from 'react'; import {Keyboard} from 'react-native'; -import {withOnyx} from 'react-native-onyx'; -import type {OnyxEntry} from 'react-native-onyx'; import CurrencySelectionList from '@components/CurrencySelectionList'; import type {CurrencyListItem} from '@components/CurrencySelectionList/types'; import useLocalize from '@hooks/useLocalize'; -import * as CurrencyUtils from '@libs/CurrencyUtils'; +import useOnyx from '@hooks/useOnyx'; +import {isValidCurrencyCode} from '@libs/CurrencyUtils'; import Navigation from '@libs/Navigation/Navigation'; -import * as ReportUtils from '@libs/ReportUtils'; +import {getTransactionDetails} from '@libs/ReportUtils'; import {appendParam} from '@libs/Url'; -import * as IOU from '@userActions/IOU'; +import {setMoneyRequestCurrency} from '@userActions/IOU'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type SCREENS from '@src/SCREENS'; -import type {Transaction} from '@src/types/onyx'; import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithFullTransactionOrNotFoundProps} from './withFullTransactionOrNotFound'; -type IOURequestStepCurrencyOnyxProps = { - /** The draft transaction object being modified in Onyx */ - draftTransaction: OnyxEntry; - /** List of recently used currencies */ - recentlyUsedCurrencies: OnyxEntry; -}; - -type IOURequestStepCurrencyProps = IOURequestStepCurrencyOnyxProps & WithFullTransactionOrNotFoundProps; +type IOURequestStepCurrencyProps = WithFullTransactionOrNotFoundProps; function IOURequestStepCurrency({ route: { params: {backTo, pageIndex, transactionID, action, currency: selectedCurrency = ''}, }, - draftTransaction, - recentlyUsedCurrencies, }: IOURequestStepCurrencyProps) { const {translate} = useLocalize(); - const {currency: originalCurrency = ''} = ReportUtils.getTransactionDetails(draftTransaction) ?? {}; - const currency = CurrencyUtils.isValidCurrencyCode(selectedCurrency) ? selectedCurrency : originalCurrency; + const [draftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {canBeMissing: true}); + const [recentlyUsedCurrencies] = useOnyx(ONYXKEYS.RECENTLY_USED_CURRENCIES, {canBeMissing: true}); + const {currency: originalCurrency = ''} = getTransactionDetails(draftTransaction) ?? {}; + const currency = isValidCurrencyCode(selectedCurrency) ? selectedCurrency : originalCurrency; const navigateBack = (selectedCurrencyValue = '') => { // If the currency selection was done from the confirmation step (eg. + > submit expense > manual > confirm > amount > currency) @@ -57,7 +48,7 @@ function IOURequestStepCurrency({ const confirmCurrencySelection = (option: CurrencyListItem) => { Keyboard.dismiss(); if (pageIndex !== CONST.IOU.PAGE_INDEX.CONFIRM) { - IOU.setMoneyRequestCurrency(transactionID, option.currencyCode, action === CONST.IOU.ACTION.EDIT); + setMoneyRequestCurrency(transactionID, option.currencyCode, action === CONST.IOU.ACTION.EDIT); } Navigation.setNavigationActionToMicrotaskQueue(() => navigateBack(option.currencyCode)); @@ -90,19 +81,7 @@ function IOURequestStepCurrency({ IOURequestStepCurrency.displayName = 'IOURequestStepCurrency'; -const IOURequestStepCurrencyWithOnyx = withOnyx({ - draftTransaction: { - key: ({route}) => { - const transactionID = route?.params?.transactionID ?? -1; - return `${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`; - }, - }, - recentlyUsedCurrencies: { - key: ONYXKEYS.RECENTLY_USED_CURRENCIES, - }, -})(IOURequestStepCurrency); - /* eslint-disable rulesdir/no-negated-variables */ -const IOURequestStepCurrencyWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepCurrencyWithOnyx); +const IOURequestStepCurrencyWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepCurrency); export default IOURequestStepCurrencyWithFullTransactionOrNotFound; diff --git a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx index a581982cf76d..07772afaf8d3 100644 --- a/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx +++ b/src/pages/iou/request/step/IOURequestStepTaxAmountPage.tsx @@ -1,9 +1,9 @@ import {useFocusEffect} from '@react-navigation/native'; import React, {useCallback, useRef} from 'react'; import type {OnyxEntry} from 'react-native-onyx'; -import {withOnyx} from 'react-native-onyx'; import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types'; import useLocalize from '@hooks/useLocalize'; +import useOnyx from '@hooks/useOnyx'; import {setDraftSplitTransaction, setMoneyRequestCurrency, setMoneyRequestParticipantsFromReport, setMoneyRequestTaxAmount, updateMoneyRequestTaxAmount} from '@libs/actions/IOU'; import {convertToBackendAmount, isValidCurrencyCode} from '@libs/CurrencyUtils'; import Navigation from '@libs/Navigation/Navigation'; @@ -15,29 +15,17 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; -import type {Policy, PolicyCategories, PolicyTagLists, Transaction} from '@src/types/onyx'; +import type {Policy, Transaction} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; import withWritableReportOrNotFound from './withWritableReportOrNotFound'; -type IOURequestStepTaxAmountPageOnyxProps = { - policy: OnyxEntry; - policyCategories: OnyxEntry; - - /** Collection of tag list on a policy */ - policyTags: OnyxEntry; - - /** The draft transaction that holds data to be persisted on the current transaction */ - splitDraftTransaction: OnyxEntry; +type IOURequestStepTaxAmountPageProps = WithWritableReportOrNotFoundProps & { + transaction: OnyxEntry; }; -type IOURequestStepTaxAmountPageProps = IOURequestStepTaxAmountPageOnyxProps & - WithWritableReportOrNotFoundProps & { - transaction: OnyxEntry; - }; - function getTaxAmount(transaction: OnyxEntry, policy: OnyxEntry, currency: string | undefined, isEditing: boolean): number | undefined { if (!transaction?.amount && !transaction?.modifiedAmount) { return; @@ -59,16 +47,15 @@ function IOURequestStepTaxAmountPage({ }, transaction, report, - policy, - policyTags, - policyCategories, - splitDraftTransaction, }: IOURequestStepTaxAmountPageProps) { const {translate} = useLocalize(); const textInput = useRef(null); const isEditing = action === CONST.IOU.ACTION.EDIT; const isEditingSplitBill = isEditing && iouType === CONST.IOU.TYPE.SPLIT; - + const [splitDraftTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`, {canBeMissing: true}); + const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`, {canBeMissing: true}); + const [policyCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report?.policyID}`, {canBeMissing: true}); + const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${report?.policyID}`, {canBeMissing: true}); const focusTimeoutRef = useRef(undefined); const currentTransaction = isEditingSplitBill && !isEmptyObject(splitDraftTransaction) ? splitDraftTransaction : transaction; @@ -178,26 +165,8 @@ function IOURequestStepTaxAmountPage({ IOURequestStepTaxAmountPage.displayName = 'IOURequestStepTaxAmountPage'; -const IOURequestStepTaxAmountPageWithOnyx = withOnyx({ - splitDraftTransaction: { - key: ({route}) => { - const transactionID = route?.params.transactionID ?? 0; - return `${ONYXKEYS.COLLECTION.SPLIT_TRANSACTION_DRAFT}${transactionID}`; - }, - }, - policy: { - key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY}${report ? report.policyID : '-1'}`, - }, - policyCategories: { - key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${report ? report.policyID : '-1'}`, - }, - policyTags: { - key: ({report}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${report ? report.policyID : '-1'}`, - }, -})(IOURequestStepTaxAmountPage); - // eslint-disable-next-line rulesdir/no-negated-variables -const IOURequestStepTaxAmountPageWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepTaxAmountPageWithOnyx); +const IOURequestStepTaxAmountPageWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepTaxAmountPage); // eslint-disable-next-line rulesdir/no-negated-variables const IOURequestStepTaxAmountPageWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepTaxAmountPageWithWritableReportOrNotFound);