Skip to content

Commit 7e70ab1

Browse files
authored
Merge pull request Expensify#86183 from dukenv0307/fix/66407-part-3
refactor putOnHold to use isOffline from useOnyx
2 parents 73fc4dd + 159cdc4 commit 7e70ab1

8 files changed

Lines changed: 240 additions & 19 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,15 +2684,16 @@ function isPayAtEndExpenseReport(report: OnyxEntry<Report>, transactions: Transa
26842684
/**
26852685
* Checks if a report is a transaction thread associated with a report that has only one transaction
26862686
*/
2687-
function isOneTransactionThread(report: OnyxEntry<Report>, parentReport: OnyxEntry<Report>, threadParentReportAction: OnyxEntry<ReportAction>) {
2687+
// TODO: isOffline will be required eventually. Refactor issue: https://github.com/Expensify/App/issues/66407
2688+
function isOneTransactionThread(report: OnyxEntry<Report>, parentReport: OnyxEntry<Report>, threadParentReportAction: OnyxEntry<ReportAction>, isOffline?: boolean) {
26882689
if (!report || !parentReport) {
26892690
return false;
26902691
}
26912692

26922693
const parentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReport?.reportID}`] ?? ([] as ReportAction[]);
26932694

26942695
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${parentReport?.chatReportID}`];
2695-
const transactionThreadReportID = getOneTransactionThreadReportID(parentReport, chatReport, parentReportActions);
2696+
const transactionThreadReportID = getOneTransactionThreadReportID(parentReport, chatReport, parentReportActions, isOffline);
26962697
return report?.reportID === transactionThreadReportID && !isSentMoneyReportAction(threadParentReportAction);
26972698
}
26982699

@@ -2707,12 +2708,13 @@ function isReportTransactionThread(report: OnyxEntry<Report>) {
27072708
/**
27082709
* Get displayed report ID, it will be parentReportID if the report is one transaction thread
27092710
*/
2710-
function getDisplayedReportID(reportID: string): string {
2711+
// TODO: isOffline will be required eventually. Refactor issue: https://github.com/Expensify/App/issues/66407
2712+
function getDisplayedReportID(reportID: string, isOffline?: boolean): string {
27112713
const report = getReport(reportID, allReports);
27122714
const parentReportID = report?.parentReportID;
27132715
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`];
27142716
const parentReportAction = getReportAction(parentReportID, report?.parentReportActionID);
2715-
return parentReportID && isOneTransactionThread(report, parentReport, parentReportAction) ? parentReportID : reportID;
2717+
return parentReportID && isOneTransactionThread(report, parentReport, parentReportAction, isOffline) ? parentReportID : reportID;
27162718
}
27172719

27182720
/**

src/libs/actions/IOU/Hold.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {getAllReports, getAllTransactions, getAllTransactionViolations, getCurre
3232
/**
3333
* Put expense on HOLD
3434
*/
35-
function putOnHold(transactionID: string, comment: string, initialReportID: string | undefined, ancestors: Ancestor[] = []) {
35+
function putOnHold(transactionID: string, comment: string, initialReportID: string | undefined, isOffline: boolean, ancestors: Ancestor[] = []) {
3636
const allTransactions = getAllTransactions();
3737
const allTransactionViolations = getAllTransactionViolations();
3838
const allReports = getAllReports();
@@ -318,14 +318,14 @@ function putOnHold(transactionID: string, comment: string, initialReportID: stri
318318

319319
API.write(WRITE_COMMANDS.HOLD_MONEY_REQUEST, params, {optimisticData, successData, failureData});
320320

321-
const currentReportID = getDisplayedReportID(reportID);
321+
const currentReportID = getDisplayedReportID(reportID, isOffline);
322322
Navigation.setNavigationActionToMicrotaskQueue(() => notifyNewAction(currentReportID, undefined, true));
323323
}
324324

325-
function putTransactionsOnHold(transactionsID: string[], comment: string, reportID: string, ancestors: Ancestor[] = []) {
325+
function putTransactionsOnHold(transactionsID: string[], comment: string, reportID: string, isOffline: boolean, ancestors: Ancestor[] = []) {
326326
for (const transactionID of transactionsID) {
327327
const {childReportID} = getIOUActionForReportID(reportID, transactionID) ?? {};
328-
putOnHold(transactionID, comment, childReportID, ancestors);
328+
putOnHold(transactionID, comment, childReportID, isOffline, ancestors);
329329
}
330330
}
331331

src/pages/Search/SearchHoldReasonPage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {useSearchActionsContext, useSearchStateContext} from '@components/Search
55
import useAncestors from '@hooks/useAncestors';
66
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
77
import useLocalize from '@hooks/useLocalize';
8+
import useNetwork from '@hooks/useNetwork';
89
import useOnyx from '@hooks/useOnyx';
910
import {clearErrorFields, clearErrors} from '@libs/actions/FormActions';
1011
import {putTransactionsOnHold} from '@libs/actions/IOU/Hold';
@@ -30,6 +31,7 @@ function SearchHoldReasonPage({route}: SearchHoldReasonPageProps) {
3031
const {accountID: currentUserAccountID} = useCurrentUserPersonalDetails();
3132
const [allTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION);
3233
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
34+
const {isOffline} = useNetwork();
3335

3436
const selectedTransactionsList = Object.values(selectedTransactions);
3537
const isSubmitter = report ? report.ownerAccountID === currentUserAccountID : selectedTransactionsList.some((t) => t.ownerAccountID === currentUserAccountID);
@@ -46,7 +48,7 @@ function SearchHoldReasonPage({route}: SearchHoldReasonPageProps) {
4648
}
4749

4850
if (route.name === SCREENS.SEARCH.MONEY_REQUEST_REPORT_HOLD_TRANSACTIONS) {
49-
putTransactionsOnHold(selectedTransactionIDs, comment, reportID, ancestors);
51+
putTransactionsOnHold(selectedTransactionIDs, comment, reportID, isOffline, ancestors);
5052
clearSelectedTransactions(true);
5153
} else {
5254
holdMoneyRequestOnSearch(currentSearchHash, Object.keys(selectedTransactions), comment, allTransactions, allReportActions);
@@ -65,6 +67,7 @@ function SearchHoldReasonPage({route}: SearchHoldReasonPageProps) {
6567
allTransactions,
6668
allReportActions,
6769
ancestors,
70+
isOffline,
6871
isDelegateAccessRestricted,
6972
showDelegateNoAccessModal,
7073
],

src/pages/iou/HoldReasonPage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
55
import useAncestors from '@hooks/useAncestors';
66
import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails';
77
import useLocalize from '@hooks/useLocalize';
8+
import useNetwork from '@hooks/useNetwork';
89
import useOnyx from '@hooks/useOnyx';
910
import {putOnHold} from '@libs/actions/IOU/Hold';
1011
import {addErrorMessage} from '@libs/ErrorUtils';
@@ -31,6 +32,7 @@ function HoldReasonPage({route}: HoldReasonPageProps) {
3132
const {transactionID, reportID, backTo} = route.params;
3233

3334
const [report] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
35+
const {isOffline} = useNetwork();
3436
const ancestors = useAncestors(report);
3537

3638
const [parentReportOwnerAccountID] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`, {selector: getReportOwnerAccountID});
@@ -56,7 +58,7 @@ function HoldReasonPage({route}: HoldReasonPageProps) {
5658
return;
5759
}
5860

59-
putOnHold(transactionID, values.comment, reportID, ancestors);
61+
putOnHold(transactionID, values.comment, reportID, isOffline, ancestors);
6062
Navigation.goBack(backTo);
6163
};
6264

tests/actions/IOUTest.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7308,7 +7308,7 @@ describe('actions/IOU', () => {
73087308
return waitForBatchedUpdates()
73097309
.then(() => Onyx.multiSet({...transactionCollectionDataSet, ...actionCollectionDataSet}))
73107310
.then(() => {
7311-
putOnHold(transaction1.transactionID, 'comment', iouReport.reportID);
7311+
putOnHold(transaction1.transactionID, 'comment', iouReport.reportID, false);
73127312
return waitForBatchedUpdates();
73137313
})
73147314
.then(() => {
@@ -13394,7 +13394,7 @@ describe('actions/IOU', () => {
1339413394

1339513395
// Put the expense on hold
1339613396
if (originalTransactionID && transactionThreadReportID) {
13397-
putOnHold(originalTransactionID, 'Test hold reason', transactionThreadReportID);
13397+
putOnHold(originalTransactionID, 'Test hold reason', transactionThreadReportID, false);
1339813398
}
1339913399
await waitForBatchedUpdates();
1340013400

0 commit comments

Comments
 (0)