Skip to content

Commit c6bec13

Browse files
authored
Merge pull request Expensify#65248 from etCoderDysto/sort_outstanding_report
fix: The order of selected workspace and selected report are inconsistent
2 parents eb6b77d + 9bfea88 commit c6bec13

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10044,6 +10044,22 @@ function getOutstandingReportsForUser(
1004410044
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0);
1004510045
}
1004610046

10047+
/**
10048+
* Sort outstanding reports by their name, while keeping the selected one at the beginning.
10049+
* @param report1 Details of the first report to be compared.
10050+
* @param report2 Details of the second report to be compared.
10051+
* @param selectedReportID ID of the selected report which needs to be at the beginning.
10052+
*/
10053+
function sortOutstandingReportsBySelected(report1: OnyxEntry<Report>, report2: OnyxEntry<Report>, selectedReportID: string | undefined): number {
10054+
if (report1?.reportID === selectedReportID) {
10055+
return -1;
10056+
}
10057+
if (report2?.reportID === selectedReportID) {
10058+
return 1;
10059+
}
10060+
return report1?.reportName?.localeCompare(report2?.reportName?.toLowerCase() ?? '') ?? 0;
10061+
}
10062+
1004710063
/**
1004810064
* @returns the object to update `report.hasOutstandingChildRequest`
1004910065
*/
@@ -11435,6 +11451,7 @@ export {
1143511451
shouldReportBeInOptionList,
1143611452
shouldReportShowSubscript,
1143711453
shouldShowFlagComment,
11454+
sortOutstandingReportsBySelected,
1143811455
getReportActionWithMissingSmartscanFields,
1143911456
shouldShowRBRForMissingSmartscanFields,
1144011457
shouldUseFullTitleToDisplay,

src/pages/iou/request/step/IOURequestEditReportCommon.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import useDebouncedState from '@hooks/useDebouncedState';
1111
import useLocalize from '@hooks/useLocalize';
1212
import useOnyx from '@hooks/useOnyx';
1313
import Navigation from '@libs/Navigation/Navigation';
14-
import {getOutstandingReportsForUser, getPolicyName, isIOUReport} from '@libs/ReportUtils';
14+
import {getOutstandingReportsForUser, getPolicyName, isIOUReport, sortOutstandingReportsBySelected} from '@libs/ReportUtils';
1515
import CONST from '@src/CONST';
1616
import ONYXKEYS from '@src/ONYXKEYS';
1717
import type {Route} from '@src/ROUTES';
@@ -86,7 +86,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport,
8686
}
8787

8888
return expenseReports
89-
.sort((a, b) => a?.reportName?.localeCompare(b?.reportName?.toLowerCase() ?? '') ?? 0)
89+
.sort((report1, report2) => sortOutstandingReportsBySelected(report1, report2, onlyReport?.reportID))
9090
.filter((report) => !debouncedSearchValue || report?.reportName?.toLowerCase().includes(debouncedSearchValue.toLowerCase()))
9191
.filter((report): report is NonNullable<typeof report> => report !== undefined)
9292
.map((report) => {

tests/unit/ReportUtilsTest.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import {
6565
shouldReportBeInOptionList,
6666
shouldReportShowSubscript,
6767
shouldShowFlagComment,
68+
sortOutstandingReportsBySelected,
6869
temporary_getMoneyRequestOptions,
6970
} from '@libs/ReportUtils';
7071
import type {OptionData} from '@libs/ReportUtils';
@@ -444,6 +445,21 @@ describe('ReportUtils', () => {
444445
});
445446
});
446447

448+
describe('sortOutstandingReportsBySelected', () => {
449+
it('should return -1 when report1 is selected and report2 is not', () => {
450+
const report1 = LHNTestUtils.getFakeReport();
451+
const report2 = LHNTestUtils.getFakeReport();
452+
const selectedReportID = report1.reportID;
453+
expect(sortOutstandingReportsBySelected(report1, report2, selectedReportID)).toBe(-1);
454+
});
455+
it('should return 1 when report2 is selected and report1 is not', () => {
456+
const report1 = LHNTestUtils.getFakeReport();
457+
const report2 = LHNTestUtils.getFakeReport();
458+
const selectedReportID = report2.reportID;
459+
expect(sortOutstandingReportsBySelected(report1, report2, selectedReportID)).toBe(1);
460+
});
461+
});
462+
447463
describe('getDisplayNamesWithTooltips', () => {
448464
test('withSingleParticipantReport', () => {
449465
const participants = getDisplayNamesWithTooltips(participantsPersonalDetails, false);

0 commit comments

Comments
 (0)