Skip to content

Commit a85a2dd

Browse files
fix: handlle change report in creation flow
1 parent 457ba18 commit a85a2dd

5 files changed

Lines changed: 101 additions & 19 deletions

File tree

src/components/MoneyRequestConfirmationListFooter.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ import {getDestinationForDisplay, getSubratesFields, getSubratesForDisplay, getT
1818
import {canSendInvoice, getPerDiemCustomUnit, hasDependentTags as hasDependentTagsPolicyUtils, isMultiLevelTags as isMultiLevelTagsPolicyUtils, isPaidGroupPolicy} from '@libs/PolicyUtils';
1919
import type {ThumbnailAndImageURI} from '@libs/ReceiptUtils';
2020
import {getThumbnailAndImageURIs} from '@libs/ReceiptUtils';
21-
import {buildOptimisticExpenseReport, getDefaultWorkspaceAvatar, getOutstandingReportsForUser, isReportOutstanding, populateOptimisticReportFormula} from '@libs/ReportUtils';
21+
import {
22+
buildOptimisticExpenseReport,
23+
getDefaultWorkspaceAvatar,
24+
getOutstandingReportsForUser,
25+
isMoneyRequestReport,
26+
isReportOutstanding,
27+
populateOptimisticReportFormula,
28+
} from '@libs/ReportUtils';
2229
import {hasEnabledTags} from '@libs/TagsOptionsListUtils';
2330
import {
2431
getTagForDisplay,
@@ -300,7 +307,10 @@ function MoneyRequestConfirmationListFooter({
300307
const optimisticReport = buildOptimisticExpenseReport(reportID, policy?.id, policy?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID, Number(formattedAmount), currency);
301308
reportName = populateOptimisticReportFormula(policy?.fieldList?.text_title?.defaultValue ?? '', optimisticReport, policy);
302309
}
303-
const shouldReportBeEditable = !!firstOutstandingReport;
310+
311+
// When creating an expense in an individual report, the report field becomes read-only
312+
// since the destination is already determined and there's no need to show a selectable list.
313+
const shouldReportBeEditable = !!firstOutstandingReport && !isMoneyRequestReport(reportID, allReports);
304314

305315
const isTypeSend = iouType === CONST.IOU.TYPE.PAY;
306316
const taxRates = policy?.taxRates ?? null;

src/libs/ReportUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,7 @@ function isMoneyRequest(reportOrID: OnyxEntry<Report> | string): boolean {
22602260
/**
22612261
* Checks if a report is an IOU or expense report.
22622262
*/
2263-
function isMoneyRequestReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string, reports?: SearchReport[]): boolean {
2263+
function isMoneyRequestReport(reportOrID: OnyxInputOrEntry<Report> | SearchReport | string, reports?: SearchReport[] | OnyxCollection<Report>): boolean {
22642264
const report = typeof reportOrID === 'string' ? (getReport(reportOrID, reports ?? allReports) ?? null) : reportOrID;
22652265
return isIOUReport(report) || isExpenseReport(report);
22662266
}

src/libs/actions/Transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ function generateTransactionID(): string {
594594
return NumberUtils.generateHexadecimalValue(16);
595595
}
596596

597-
function setTransactionReport(transactionID: string, reportID: string, isDraft: boolean) {
598-
Onyx.merge(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, {reportID});
597+
function setTransactionReport(transactionID: string, transaction: Partial<Transaction>, isDraft: boolean) {
598+
Onyx.merge(`${isDraft ? ONYXKEYS.COLLECTION.TRANSACTION_DRAFT : ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction);
599599
}
600600

601601
function changeTransactionsReport(transactionIDs: string[], reportID: string) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ type Props = {
4141
backTo: Route | undefined;
4242
transactionsReports: Report[];
4343
selectReport: (item: ReportListItem) => void;
44+
policyID?: string;
4445
};
4546

46-
function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}: Props) {
47+
function IOURequestEditReportCommon({backTo, transactionsReports, policyID: policyIDFromProps, selectReport}: Props) {
4748
const {translate} = useLocalize();
4849
const {options} = useOptionsList();
4950
const [allReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (reports) => mapOnyxCollectionItems(reports, reportSelector), canBeMissing: true});
@@ -56,7 +57,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
5657
const expenseReports = useMemo(
5758
() =>
5859
Object.values(allPoliciesID ?? {}).flatMap((policyID) => {
59-
if (!policyID) {
60+
if (!policyID || (policyIDFromProps && policyID !== policyIDFromProps)) {
6061
return [];
6162
}
6263
const reports = getOutstandingReportsForUser(
@@ -67,7 +68,7 @@ function IOURequestEditReportCommon({backTo, transactionsReports, selectReport}:
6768
);
6869
return reports;
6970
}),
70-
[allReports, currentUserPersonalDetails.accountID, transactionsReports, allPoliciesID, reportNameValuePairs],
71+
[allReports, currentUserPersonalDetails.accountID, transactionsReports, allPoliciesID, reportNameValuePairs, policyIDFromProps],
7172
);
7273

7374
const reportOptions: ReportListItem[] = useMemo(() => {

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

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {useOnyx} from 'react-native-onyx';
33
import type {ListItem} from '@components/SelectionList/types';
44
import {changeTransactionsReport, setTransactionReport} from '@libs/actions/Transaction';
55
import Navigation from '@libs/Navigation/Navigation';
6+
import {getReportOrDraftReport} from '@libs/ReportUtils';
67
import CONST from '@src/CONST';
78
import ONYXKEYS from '@src/ONYXKEYS';
9+
import ROUTES from '@src/ROUTES';
810
import type SCREENS from '@src/SCREENS';
911
import IOURequestEditReportCommon from './IOURequestEditReportCommon';
1012
import withFullTransactionOrNotFound from './withFullTransactionOrNotFound';
@@ -20,34 +22,103 @@ type ReportListItem = ListItem & {
2022
type IOURequestStepReportProps = WithWritableReportOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_REPORT> & WithFullTransactionOrNotFoundProps<typeof SCREENS.MONEY_REQUEST.STEP_REPORT>;
2123

2224
function IOURequestStepReport({route, transaction}: IOURequestStepReportProps) {
23-
const {backTo, action} = route.params;
25+
const {backTo, action, iouType, transactionID, reportID: reportIDFromRoute} = route.params;
2426
const reportID = transaction?.reportID === '0' ? transaction?.participants?.at(0)?.reportID : transaction?.reportID;
2527
const [transactionReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {canBeMissing: true});
26-
28+
const isCreateReport = action === CONST.IOU.ACTION.CREATE;
2729
const isEditing = action === CONST.IOU.ACTION.EDIT;
30+
const isFromGlobalCreate = !!transaction?.isFromGlobalCreate;
31+
const reportOrDraftReport = getReportOrDraftReport(reportIDFromRoute);
2832

29-
const selectReport = (item: ReportListItem) => {
33+
const handleGoBackWithReportID = (id: string) => {
34+
if (isEditing) {
35+
Navigation.dismissModalWithReport({reportID: id});
36+
} else {
37+
Navigation.goBack(backTo);
38+
}
39+
};
40+
41+
const handleGlobalCreateReport = (item: ReportListItem) => {
3042
if (!transaction) {
3143
return;
3244
}
33-
if (item.value !== transaction.reportID) {
34-
setTransactionReport(transaction.transactionID, item.value, !isEditing);
35-
if (isEditing) {
36-
changeTransactionsReport([transaction.transactionID], item.value);
37-
}
45+
const reportOrDraftReportFromValue = getReportOrDraftReport(item.value);
46+
const participants = [
47+
{
48+
selected: true,
49+
accountID: 0,
50+
isPolicyExpenseChat: true,
51+
reportID: reportOrDraftReportFromValue?.chatReportID,
52+
policyID: reportOrDraftReportFromValue?.policyID,
53+
},
54+
];
55+
56+
setTransactionReport(
57+
transaction.transactionID,
58+
{
59+
reportID: item.value,
60+
participants,
61+
},
62+
true,
63+
);
64+
65+
const iouConfirmationPageRoute = ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(action, iouType, transactionID, reportOrDraftReportFromValue?.chatReportID);
66+
// If the backTo parameter is set, we should navigate back to the confirmation screen that is already on the stack.
67+
if (backTo) {
68+
Navigation.goBack(iouConfirmationPageRoute, {compareParams: false});
69+
} else {
70+
Navigation.navigate(iouConfirmationPageRoute);
71+
}
72+
};
73+
74+
const handleRegularReportSelection = (item: ReportListItem) => {
75+
if (!transaction) {
76+
return;
3877
}
78+
setTransactionReport(
79+
transaction.transactionID,
80+
{
81+
reportID: item.value,
82+
},
83+
!isEditing,
84+
);
85+
3986
if (isEditing) {
40-
Navigation.dismissModalWithReport({reportID: item.value});
41-
} else {
42-
Navigation.goBack(backTo);
87+
changeTransactionsReport([transaction.transactionID], item.value);
88+
}
89+
90+
handleGoBackWithReportID(item.value);
91+
};
92+
93+
const selectReport = (item: ReportListItem) => {
94+
if (!transaction) {
95+
return;
4396
}
97+
98+
const isSameReport = item.value === transaction.reportID;
99+
100+
// Early return for same report selection
101+
if (isSameReport) {
102+
handleGoBackWithReportID(item.value);
103+
return;
104+
}
105+
106+
// Handle global create report
107+
if (isCreateReport && isFromGlobalCreate) {
108+
handleGlobalCreateReport(item);
109+
return;
110+
}
111+
112+
// Handle regular report selection
113+
handleRegularReportSelection(item);
44114
};
45115

46116
return (
47117
<IOURequestEditReportCommon
48118
backTo={backTo}
49119
transactionsReports={transactionReport ? [transactionReport] : []}
50120
selectReport={selectReport}
121+
policyID={!isEditing && !isFromGlobalCreate ? reportOrDraftReport?.policyID : undefined}
51122
/>
52123
);
53124
}

0 commit comments

Comments
 (0)