Skip to content

Commit 0b62a4e

Browse files
authored
Merge pull request #81487 from shubham1206agra/refactor-explain
Refactor: isolate explain from Onyx.connect ONYXKEYS.COLLECTION.REPORT
2 parents 4945938 + aa6b858 commit 0b62a4e

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

src/libs/actions/Report/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,20 +1741,19 @@ function createChildReport(childReport: OnyxEntry<Report>, parentReportAction: R
17411741
* Adds a "Please explain this to me." comment from the user
17421742
*/
17431743
function explain(
1744+
childReport: OnyxEntry<Report>,
1745+
originalReport: OnyxEntry<Report>,
17441746
reportAction: OnyxEntry<ReportAction>,
1745-
originalReportID: string | undefined,
17461747
translate: LocalizedTranslate,
17471748
currentUserAccountID: number,
17481749
timezone: Timezone = CONST.DEFAULT_TIME_ZONE,
17491750
) {
1750-
if (!originalReportID || !reportAction) {
1751+
if (!originalReport?.reportID || !reportAction) {
17511752
return;
17521753
}
17531754

17541755
// Check if explanation thread report already exists
1755-
const existingChildReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`];
1756-
const originalReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${originalReportID}`];
1757-
const report = existingChildReport ?? createChildReport(existingChildReport, reportAction, originalReport);
1756+
const report = childReport ?? createChildReport(childReport, reportAction, originalReport);
17581757

17591758
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.reportID, undefined, undefined, Navigation.getActiveRoute()));
17601759
// Schedule adding the explanation comment on the next animation frame

src/pages/inbox/report/ContextMenu/ContextMenuActions.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,21 @@ const ContextMenuActions: ContextMenuAction[] = [
473473

474474
return hasReasoning(reportAction);
475475
},
476-
onPress: (closePopover, {reportAction, reportID, translate, currentUserPersonalDetails}) => {
477-
if (!reportID) {
476+
onPress: (closePopover, {reportAction, childReport, originalReport, translate, currentUserPersonalDetails}) => {
477+
if (!originalReport?.reportID) {
478478
return;
479479
}
480480

481-
const originalReportID = getOriginalReportID(reportID, reportAction);
482481
if (closePopover) {
483482
hideContextMenu(false, () => {
484483
KeyboardUtils.dismiss().then(() => {
485-
explain(reportAction, originalReportID, translate, currentUserPersonalDetails.accountID, currentUserPersonalDetails.timezone);
484+
explain(childReport, originalReport, reportAction, translate, currentUserPersonalDetails.accountID, currentUserPersonalDetails?.timezone);
486485
});
487486
});
488487
return;
489488
}
490489

491-
explain(reportAction, originalReportID, translate, currentUserPersonalDetails.accountID, currentUserPersonalDetails.timezone);
490+
explain(childReport, originalReport, reportAction, translate, currentUserPersonalDetails.accountID, currentUserPersonalDetails?.timezone);
492491
},
493492
getDescription: () => {},
494493
sentryLabel: CONST.SENTRY_LABEL.CONTEXT_MENU.EXPLAIN,

src/pages/inbox/report/PureReportActionItem.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ function PureReportActionItem({
551551
const personalDetail = useCurrentUserPersonalDetails();
552552
const {shouldUseNarrowLayout} = useResponsiveLayout();
553553
const reportID = report?.reportID ?? action?.reportID;
554+
const childReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${action.childReportID}`];
554555
const theme = useTheme();
555556
const styles = useThemeStyles();
556557
const StyleUtils = useStyleUtils();
@@ -1309,7 +1310,8 @@ function PureReportActionItem({
13091310
<ReportActionItemMessageWithExplain
13101311
message={modifiedExpenseMessage}
13111312
action={action}
1312-
reportID={reportID}
1313+
childReport={childReport}
1314+
originalReport={originalReport}
13131315
/>
13141316
);
13151317
} else if (isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.SUBMITTED) || isActionOfType(action, CONST.REPORT.ACTIONS.TYPE.SUBMITTED_AND_CLOSED) || isMarkAsClosedAction(action)) {
@@ -1322,7 +1324,8 @@ function PureReportActionItem({
13221324
<ReportActionItemMessageWithExplain
13231325
message={translate('iou.automaticallySubmitted')}
13241326
action={action}
1325-
reportID={reportID}
1327+
childReport={childReport}
1328+
originalReport={originalReport}
13261329
/>
13271330
);
13281331
} else if (hasPendingDEWSubmit(reportMetadata, isDEWPolicy) && isPendingAdd) {

src/pages/inbox/report/ReportActionItemMessageWithExplain.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
66
import useLocalize from '@hooks/useLocalize';
77
import {explain} from '@libs/actions/Report';
88
import {hasReasoning} from '@libs/ReportActionsUtils';
9-
import {getOriginalReportID} from '@libs/ReportUtils';
109
import CONST from '@src/CONST';
11-
import type {ReportAction} from '@src/types/onyx';
10+
import type {Report, ReportAction} from '@src/types/onyx';
1211
import ReportActionItemBasicMessage from './ReportActionItemBasicMessage';
1312

1413
type ReportActionItemMessageWithExplainProps = {
@@ -18,15 +17,18 @@ type ReportActionItemMessageWithExplainProps = {
1817
/** All the data of the action item */
1918
action: OnyxEntry<ReportAction>;
2019

21-
/** The report ID of linked report */
22-
reportID: string | undefined;
20+
/** The child report of the action item */
21+
childReport: OnyxEntry<Report>;
22+
23+
/** Original report from which the given reportAction is first created */
24+
originalReport: OnyxEntry<Report>;
2325
};
2426

2527
/**
2628
* Wrapper component that renders a message and automatically appends the "Explain" link
2729
* if the action has reasoning.
2830
*/
29-
function ReportActionItemMessageWithExplain({message, action, reportID}: ReportActionItemMessageWithExplainProps) {
31+
function ReportActionItemMessageWithExplain({message, action, childReport, originalReport}: ReportActionItemMessageWithExplainProps) {
3032
const {translate} = useLocalize();
3133
const personalDetail = useCurrentUserPersonalDetails();
3234

@@ -39,10 +41,9 @@ function ReportActionItemMessageWithExplain({message, action, reportID}: ReportA
3941
return;
4042
}
4143

42-
const actionOriginalReportID = getOriginalReportID(reportID, action);
43-
explain(action, actionOriginalReportID, translate, personalDetail.accountID, personalDetail?.timezone);
44+
explain(childReport, originalReport, action, translate, personalDetail.accountID, personalDetail?.timezone);
4445
},
45-
[action, reportID, translate, personalDetail?.timezone, personalDetail.accountID],
46+
[childReport, originalReport, action, translate, personalDetail?.timezone, personalDetail.accountID],
4647
);
4748

4849
return (

tests/actions/ReportTest.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3861,25 +3861,26 @@ describe('actions/Report', () => {
38613861
const CHILD_REPORT_ID = '2';
38623862
const REPORT_ACTION_ID = 1;
38633863

3864-
it('should return early if originalReportID is not provided', () => {
3864+
it('should return early if originalReport is not provided', () => {
38653865
const REPORT_ACTION: OnyxTypes.ReportAction = {
38663866
...createRandomReportAction(REPORT_ACTION_ID),
38673867
reportActionID: '1',
38683868
actorAccountID: TEST_USER_ACCOUNT_ID,
38693869
};
38703870

3871-
const result = Report.explain(REPORT_ACTION, undefined, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
3871+
const result = Report.explain(undefined, undefined, REPORT_ACTION, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
38723872

38733873
expect(result).toBeUndefined();
38743874
});
38753875

38763876
it('should return early if reportAction is not provided', () => {
3877-
const result = Report.explain(undefined, PARENT_REPORT_ID, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
3877+
const result = Report.explain(undefined, undefined, undefined, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
38783878

38793879
expect(result).toBeUndefined();
38803880
});
38813881

3882-
it('should accept report action and original report ID', async () => {
3882+
it('should accept report action and original report', async () => {
3883+
const PARENT_REPORT = createRandomReport(1, undefined);
38833884
const EXISTING_CHILD_REPORT = createRandomReport(2, undefined);
38843885
const REPORT_ACTION: OnyxTypes.ReportAction = {
38853886
...createRandomReportAction(REPORT_ACTION_ID),
@@ -3892,7 +3893,7 @@ describe('actions/Report', () => {
38923893
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${CHILD_REPORT_ID}`, EXISTING_CHILD_REPORT);
38933894
await waitForBatchedUpdates();
38943895

3895-
Report.explain(REPORT_ACTION, PARENT_REPORT_ID, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
3896+
Report.explain(EXISTING_CHILD_REPORT, PARENT_REPORT, REPORT_ACTION, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
38963897
await waitForBatchedUpdates();
38973898

38983899
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(EXISTING_CHILD_REPORT.reportID));
@@ -3911,13 +3912,14 @@ describe('actions/Report', () => {
39113912
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${PARENT_REPORT_ID}`, PARENT_REPORT);
39123913
await waitForBatchedUpdates();
39133914

3914-
Report.explain(REPORT_ACTION, PARENT_REPORT_ID, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
3915+
Report.explain(undefined, PARENT_REPORT, REPORT_ACTION, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID, CONST.DEFAULT_TIME_ZONE);
39153916
await waitForBatchedUpdates();
39163917

39173918
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute('9876'));
39183919
});
39193920

39203921
it('should handle explain with default timezone parameter', async () => {
3922+
const PARENT_REPORT = createRandomReport(1, undefined);
39213923
const EXISTING_CHILD_REPORT = createRandomReport(2, undefined);
39223924
const REPORT_ACTION: OnyxTypes.ReportAction = {
39233925
...createRandomReportAction(REPORT_ACTION_ID),
@@ -3930,7 +3932,7 @@ describe('actions/Report', () => {
39303932
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${CHILD_REPORT_ID}`, EXISTING_CHILD_REPORT);
39313933
await waitForBatchedUpdates();
39323934

3933-
Report.explain(REPORT_ACTION, PARENT_REPORT_ID, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID);
3935+
Report.explain(EXISTING_CHILD_REPORT, PARENT_REPORT, REPORT_ACTION, TestHelper.translateLocal, TEST_USER_ACCOUNT_ID);
39343936
await waitForBatchedUpdates();
39353937

39363938
expect(Navigation.navigate).toHaveBeenCalledWith(ROUTES.REPORT_WITH_ID.getRoute(EXISTING_CHILD_REPORT.reportID));

0 commit comments

Comments
 (0)