Repository navigation
Refactor TransactionInlineEdit actions into pure functions (remove connectWithoutView) #99279
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
Changes from all commits
d6632e5
20456c0
daa060f
2b6e31d
22be760
270c063
e6f0fed
6df36d7
93e2546
77aba85
2dea13e
a68c7c0
052c2d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,17 +7,8 @@ import {hasEnabledOptions} from '@libs/OptionsListUtils'; | |
| import Permissions from '@libs/Permissions'; | ||
| import {getLoginByAccountID} from '@libs/PersonalDetailsUtils'; | ||
| import {getTagLists, isGroupPolicy, isMultiLevelTags, resolveCurrentTaxCode} from '@libs/PolicyUtils'; | ||
| import {getIOUActionForTransactionID, isMoneyRequestAction} from '@libs/ReportActionsUtils'; | ||
| import { | ||
| canEditFieldOfMoneyRequest, | ||
| canEditMoneyRequest, | ||
| canUserPerformWriteAction, | ||
| findSelfDMReportID, | ||
| isArchivedReport, | ||
| isInvoiceReport, | ||
| isIOUReport, | ||
| shouldEnableNegative, | ||
| } from '@libs/ReportUtils'; | ||
| import {isMoneyRequestAction} from '@libs/ReportActionsUtils'; | ||
| import {canEditFieldOfMoneyRequest, canEditMoneyRequest, canUserPerformWriteAction, isArchivedReport, isInvoiceReport, isIOUReport, shouldEnableNegative} from '@libs/ReportUtils'; | ||
| import {hasEnabledTags} from '@libs/TagsOptionsListUtils'; | ||
| import { | ||
| calculateTaxAmount, | ||
|
|
@@ -55,11 +46,12 @@ import type {ValueOf} from 'type-fest'; | |
| /** | ||
| * Actions for inline editing of transactions from the Search results table and the Expense Report page. | ||
| * | ||
| * Each function delegates to the corresponding IOU action which owns the canonical Onyx record, | ||
| * the API write, failure rollback, and snapshot updates (when a hash is provided). | ||
| * These functions are pure: every Onyx value they need (the transaction and violation | ||
| * collections, the resolved reports/report action, session, betas, etc.) is passed in by | ||
| * the caller (`useTransactionInlineEdit`), which reads it via `useOnyx`. Each function | ||
| * delegates to the corresponding IOU action which owns the canonical Onyx record, the API | ||
| * write, failure rollback, and snapshot updates (when a hash is provided). | ||
| */ | ||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import { | ||
| updateMoneyRequestAmountAndCurrency, | ||
| updateMoneyRequestCategory, | ||
|
|
@@ -79,64 +71,6 @@ type TransactionEditPermissions = { | |
| canEditTag: boolean; | ||
| }; | ||
|
|
||
| let allTransactions: NonNullable<OnyxCollection<Transaction>> = {}; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION, | ||
| callback: (value) => { | ||
| allTransactions = value ?? {}; | ||
| }, | ||
| }); | ||
|
|
||
| let allTransactionViolations: NonNullable<OnyxCollection<TransactionViolations>> = {}; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS, | ||
| callback: (value) => { | ||
| allTransactionViolations = value ?? {}; | ||
| }, | ||
| }); | ||
|
|
||
| let allReports: NonNullable<OnyxCollection<Report>> = {}; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.REPORT, | ||
| callback: (value) => { | ||
| allReports = value ?? {}; | ||
| }, | ||
| }); | ||
|
|
||
| let allReportActions: NonNullable<OnyxCollection<ReportActions>> = {}; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
| callback: (value) => { | ||
| allReportActions = value ?? {}; | ||
| }, | ||
| }); | ||
|
|
||
| let currentUserAccountID: number = CONST.DEFAULT_NUMBER_ID; | ||
| let currentUserEmail = ''; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.SESSION, | ||
| callback: (value) => { | ||
| currentUserEmail = value?.email ?? ''; | ||
| currentUserAccountID = value?.accountID ?? CONST.DEFAULT_NUMBER_ID; | ||
| }, | ||
| }); | ||
|
|
||
| let allBetas: Beta[] | undefined; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.BETAS, | ||
| callback: (value) => { | ||
| allBetas = value ?? undefined; | ||
| }, | ||
| }); | ||
|
|
||
| let introSelected: OnyxEntry<IntroSelected>; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.NVP_INTRO_SELECTED, | ||
| callback: (value) => { | ||
| introSelected = value; | ||
| }, | ||
| }); | ||
|
|
||
| const NO_EDIT: Readonly<TransactionEditPermissions> = Object.freeze({ | ||
| canEditDate: false, | ||
| canEditMerchant: false, | ||
|
|
@@ -200,6 +134,24 @@ type GetIouParamsInput = { | |
| isTrackIntentUser: boolean | undefined; | ||
| getCurrencyDecimals: CurrencyListActionsContextType['getCurrencyDecimals']; | ||
| getCurrencySymbol: CurrencyListActionsContextType['getCurrencySymbol']; | ||
|
|
||
| /** The transaction being edited plus any of its duplicates, scoped by the caller. */ | ||
| transactions: OnyxCollection<Transaction>; | ||
|
|
||
| /** Violations for the transaction being edited plus any of its duplicates, scoped by the caller. */ | ||
| transactionViolations: OnyxCollection<TransactionViolations>; | ||
|
|
||
| /** Betas the current user has access to, used to gate ASAP submit behavior. */ | ||
| betas: Beta[] | undefined; | ||
|
|
||
| /** Onboarding intro data, needed to build a transaction thread report when one doesn't exist yet. */ | ||
| introSelected: OnyxEntry<IntroSelected>; | ||
|
|
||
| /** The current user's account ID. */ | ||
| currentUserAccountID: number; | ||
|
|
||
| /** The current user's email/login. */ | ||
| currentUserEmail: string; | ||
| }; | ||
|
|
||
| type TransactionInlineEditParams = GetIouParamsInput & { | ||
|
|
@@ -234,52 +186,32 @@ function getIouParamsForTransaction({ | |
| isTrackIntentUser, | ||
| getCurrencyDecimals, | ||
| getCurrencySymbol, | ||
| transactionViolations, | ||
| betas, | ||
| introSelected, | ||
| currentUserAccountID, | ||
| currentUserEmail, | ||
| }: GetIouParamsInput) { | ||
| const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; | ||
| const isUnreportedExpense = !transaction?.reportID || transaction.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; | ||
|
|
||
| let resolvedParentReport = parentReport; | ||
| if (!resolvedParentReport?.reportID && transaction?.reportID && transaction.reportID !== CONST.REPORT.UNREPORTED_REPORT_ID) { | ||
| resolvedParentReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${transaction.reportID}`]; | ||
| } | ||
|
|
||
| let resolvedParentReportAction = parentReportAction; | ||
| if (!resolvedParentReportAction && resolvedParentReport?.reportID) { | ||
| const reportActions = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${resolvedParentReport.reportID}`] ?? {}; | ||
| resolvedParentReportAction = getIOUActionForTransactionID(Object.values(reportActions), transactionID); | ||
| } | ||
|
|
||
| if (isUnreportedExpense) { | ||
| const selfDMReportID = findSelfDMReportID(allReports); | ||
| if (selfDMReportID) { | ||
| resolvedParentReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${selfDMReportID}`] ?? resolvedParentReport; | ||
|
|
||
| if (!resolvedParentReportAction) { | ||
| const selfDMReportActions = allReportActions[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${selfDMReportID}`] ?? {}; | ||
| resolvedParentReportAction = getIOUActionForTransactionID(Object.values(selfDMReportActions), transactionID); | ||
| } | ||
| } | ||
| } | ||
| // transaction is passed in by the caller; only the scoped violations are derived here for the thread-report build. | ||
| const transactionViolationsForTransaction = transactionViolations?.[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`]; | ||
|
|
||
| // parentReport (already resolved to the self DM for unreported expenses), parentReportAction, and | ||
| // transactionThreadReport are resolved by the caller (useTransactionInlineEdit) via useOnyx, so they | ||
| // are used directly here. The only remaining resolution is building a transaction thread report when | ||
| // one doesn't exist in Onyx yet. | ||
| let resolvedTransactionThreadReport = transactionThreadReport; | ||
| const transactionThreadReportID = resolvedTransactionThreadReport?.reportID ?? transaction?.transactionThreadReportID ?? resolvedParentReportAction?.childReportID; | ||
|
|
||
| if (!resolvedTransactionThreadReport && transactionThreadReportID) { | ||
| resolvedTransactionThreadReport = allReports[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`]; | ||
| } | ||
|
|
||
| if (!resolvedTransactionThreadReport && resolvedParentReportAction && transaction) { | ||
| if (!resolvedTransactionThreadReport && parentReportAction && transaction) { | ||
| resolvedTransactionThreadReport = createTransactionThreadReport({ | ||
| introSelected, | ||
| // Deferred: thread the real conciergeChat when this cascade is migrated (https://github.com/Expensify/App/issues/66411) | ||
| conciergeChat: undefined, | ||
| currentUserLogin: currentUserEmail, | ||
| currentUserAccountID, | ||
| betas: allBetas, | ||
| iouReport: resolvedParentReport, | ||
| iouReportAction: resolvedParentReportAction, | ||
| betas, | ||
| iouReport: parentReport, | ||
| iouReportAction: parentReportAction, | ||
| transaction, | ||
| transactionViolations: transactionViolations ?? undefined, | ||
| transactionViolations: transactionViolationsForTransaction ?? undefined, | ||
| personalDetails: personalDetailsList, | ||
| isSelfTourViewed, | ||
| hasCompletedGuidedSetupFlow, | ||
|
|
@@ -289,14 +221,14 @@ function getIouParamsForTransaction({ | |
| return { | ||
| transactionID, | ||
| transactionThreadReport: resolvedTransactionThreadReport, | ||
| parentReport: resolvedParentReport, | ||
| iouReportOwnerLogin: getLoginByAccountID(resolvedParentReport?.ownerAccountID, personalDetailsList), | ||
| parentReport, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On a Search row whose partial snapshot contains the transaction/action but omits its Useful? React with 👍 / 👎.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed! we fallback to live report in |
||
| iouReportOwnerLogin: getLoginByAccountID(parentReport?.ownerAccountID, personalDetailsList), | ||
| policy, | ||
| policyForTrackExpense, | ||
| policyCategories, | ||
| currentUserAccountIDParam: currentUserAccountID, | ||
| currentUserEmailParam: currentUserEmail, | ||
| isASAPSubmitBetaEnabled: Permissions.isBetaEnabled(CONST.BETAS.ASAP_SUBMIT, allBetas), | ||
| isASAPSubmitBetaEnabled: Permissions.isBetaEnabled(CONST.BETAS.ASAP_SUBMIT, betas), | ||
| delegateAccountID, | ||
| isTrackIntentUser, | ||
| getCurrencyDecimals, | ||
|
|
@@ -319,8 +251,8 @@ function editTransactionDateInline(params: TransactionInlineEditParams, newDate: | |
| // updateMoneyRequestDate uses 'policyTags' (not policyTagList) | ||
| policyTags: iouParams.policyTagList, | ||
| value: newDate, | ||
| transactions: allTransactions, | ||
| transactionViolations: allTransactionViolations, | ||
| transactions: params.transactions, | ||
| transactionViolations: params.transactionViolations, | ||
| isOffline: params.isOffline, | ||
| hash: params.hash, | ||
| distanceOriginalPolicy: params.distanceOriginalPolicy, | ||
|
|
@@ -330,6 +262,9 @@ function editTransactionDateInline(params: TransactionInlineEditParams, newDate: | |
|
|
||
| /** Updates the merchant of an expense from the Search results table or the Expense Report page. */ | ||
| function editTransactionMerchantInline(params: TransactionInlineEditParams, newMerchant: string) { | ||
| // Validate before building iouParams: getIouParamsForTransaction can call createTransactionThreadReport, | ||
| // which fires Onyx.merge + openReport (an API call). Building params first would optimistically create a | ||
| // thread report for an edit we're about to discard, so validate first. | ||
| if (!isValidMerchant(newMerchant, params.transaction, params.parentReport)) { | ||
| return; | ||
| } | ||
|
|
@@ -366,15 +301,19 @@ function editTransactionCategoryInline(params: TransactionInlineEditParams, newC | |
|
|
||
| /** Updates the amount and currency of an expense from the Search results table or the Expense Report page. */ | ||
| function editTransactionAmountInline(params: TransactionInlineEditParams, newAmount: number) { | ||
| const iouParams = getIouParamsForTransaction(params); | ||
| // Validate before building iouParams: getIouParamsForTransaction can call createTransactionThreadReport, | ||
| // which fires Onyx.merge + openReport (an API call). Building params first would optimistically create a | ||
| // thread report for an edit we're about to discard, so validate against params directly first. | ||
| const iouType = isInvoiceReport(params.parentReport) ? CONST.IOU.TYPE.INVOICE : CONST.IOU.TYPE.SUBMIT; | ||
| const allowNegative = shouldEnableNegative(params.parentReport, iouParams.policy, iouType); | ||
| const allowNegative = shouldEnableNegative(params.parentReport, params.policy, iouType); | ||
| const isP2P = isIOUReport(params.parentReport); | ||
|
|
||
| if (!isValidMoneyRequestAmount(newAmount, iouType, allowNegative, isP2P)) { | ||
| return; | ||
| } | ||
|
|
||
| const iouParams = getIouParamsForTransaction(params); | ||
|
|
||
| // Keep the existing currency — only the amount is changing from the search table | ||
| const currency = iouParams.transaction?.modifiedCurrency ?? iouParams.transaction?.currency ?? CONST.CURRENCY.USD; | ||
| // Recalculate tax from the existing tax code and the new amount | ||
|
|
@@ -390,8 +329,8 @@ function editTransactionAmountInline(params: TransactionInlineEditParams, newAmo | |
| taxCode, | ||
| taxValue: taxPercentage, | ||
| allowNegative, | ||
| transactions: allTransactions, | ||
| transactionViolations: allTransactionViolations, | ||
| transactions: params.transactions, | ||
| transactionViolations: params.transactionViolations, | ||
| policyRecentlyUsedCurrencies: [], | ||
| hash: params.hash, | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Search snapshot mode,
transactionThreadReportcan be undefined even whenparentReportAction.childReportIDidentifies an existing thread stored in live Onyx. This branch now creates a new thread with a generated ID and callsOpenReportinstead of reusing that child thread; the removedallReportsfallback previously recovered the live report. Editing such a Search row can therefore create a duplicate transaction thread and associate the update with the wrong optimistic report.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MelvinBot Please fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — fixed in
22be760.Root cause: the hook already resolved
transactionThreadReportIDfromparentReportAction.childReportID/transaction.transactionThreadReportID, but it read the report itself through the wrappeduseOnyx, which serves the partial Search snapshot. In Search mode that snapshot often doesn't contain the child thread report, sotransactionThreadReportcame backundefinedeven though the thread exists in live Onyx — and the action then built a brand-new thread (generated ID +OpenReport) instead of reusing it, creating a duplicate. The pre-refactorallReports[...]fallback read from live global Onyx, so it never hit this.Fix: read
transactionThreadReportviaoriginalUseOnyx(live Onyx) instead of the snapshot-aware wrapper — the same pattern already used a few lines up forparentReportActionsandcompletePolicy. This restores the pre-refactor "reuse the live child thread" behavior while keeping the action pure. Seesrc/hooks/useTransactionInlineEdit.ts:126-129.Verified locally: ESLint ✅, React Compiler compliance ✅,
npm run typecheck✅,tests/unit/inlineEditing/TransactionInlineEdit.test.ts(26/26) ✅,tests/actions/IOUTest/UpdateMoneyRequestTest.ts(35/35) ✅.view run