Skip to content

Commit 7587a45

Browse files
authored
Merge pull request Expensify#66918 from Tony-MK/fix/65460
Fixed report fields error
2 parents 82ade61 + ebc7475 commit 7587a45

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/libs/ReportUtils.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10880,11 +10880,22 @@ function getChatUsedForOnboarding(): OnyxEntry<Report> {
1088010880
* Checks if given field has any violations and returns name of the first encountered one
1088110881
*/
1088210882
function getFieldViolation(violations: OnyxEntry<ReportViolations>, reportField: PolicyReportField): ReportViolationName | undefined {
10883-
if (!violations || !reportField) {
10883+
if (!reportField) {
1088410884
return undefined;
1088510885
}
1088610886

10887-
return Object.values(CONST.REPORT_VIOLATIONS).find((violation) => !!violations[violation] && violations[violation][reportField.fieldID]);
10887+
if (!violations) {
10888+
return reportField.value ? undefined : CONST.REPORT_VIOLATIONS.FIELD_REQUIRED;
10889+
}
10890+
10891+
const fieldViolation = Object.values(CONST.REPORT_VIOLATIONS).find((violation) => !!violations[violation] && violations[violation][reportField.fieldID]);
10892+
10893+
// If the field has no value or no violation, we return 'fieldRequired' violation
10894+
if (!fieldViolation) {
10895+
return reportField.value ? undefined : CONST.REPORT_VIOLATIONS.FIELD_REQUIRED;
10896+
}
10897+
10898+
return fieldViolation;
1088810899
}
1088910900

1089010901
/**

src/libs/actions/Report.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2386,13 +2386,16 @@ function clearReportFieldKeyErrors(reportID: string | undefined, fieldKey: strin
23862386
});
23872387
}
23882388

2389-
function updateReportField(reportID: string, reportField: PolicyReportField, previousReportField: PolicyReportField) {
2389+
function updateReportField(report: Report, reportField: PolicyReportField, previousReportField: PolicyReportField, policy: Policy, shouldFixViolations = false) {
2390+
const reportID = report.reportID;
23902391
const fieldKey = getReportFieldKey(reportField.fieldID);
23912392
const reportViolations = getReportViolations(reportID);
23922393
const fieldViolation = getFieldViolation(reportViolations, reportField);
23932394
const recentlyUsedValues = allRecentlyUsedReportFields?.[fieldKey] ?? [];
23942395

23952396
const optimisticChangeFieldAction = buildOptimisticChangeFieldAction(reportField, previousReportField);
2397+
const predictedNextStatus = policy?.reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO ? CONST.REPORT.STATUS_NUM.CLOSED : CONST.REPORT.STATUS_NUM.OPEN;
2398+
const optimisticNextStep = buildNextStep(report, predictedNextStatus, shouldFixViolations);
23962399

23972400
const optimisticData: OnyxUpdate[] = [
23982401
{
@@ -2407,6 +2410,11 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
24072410
},
24082411
},
24092412
},
2413+
{
2414+
onyxMethod: Onyx.METHOD.MERGE,
2415+
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`,
2416+
value: optimisticNextStep,
2417+
},
24102418
{
24112419
onyxMethod: Onyx.METHOD.MERGE,
24122420
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,

src/pages/EditReportFieldPage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import CONST from '@src/CONST';
1818
import ONYXKEYS from '@src/ONYXKEYS';
1919
import ROUTES from '@src/ROUTES';
2020
import type SCREENS from '@src/SCREENS';
21+
import type {Policy} from '@src/types/onyx';
2122
import EditReportFieldDate from './EditReportFieldDate';
2223
import EditReportFieldDropdown from './EditReportFieldDropdown';
2324
import EditReportFieldText from './EditReportFieldText';
@@ -36,6 +37,8 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) {
3637
const {translate} = useLocalize();
3738
const isReportFieldTitle = isReportFieldOfTypeTitle(reportField);
3839
const reportFieldsEnabled = ((isPaidGroupPolicyExpenseReport(report) || isInvoiceReport(report)) && !!policy?.areReportFieldsEnabled) || isReportFieldTitle;
40+
const hasOtherViolations =
41+
report?.fieldList && Object.entries(report.fieldList).some(([key, field]) => key !== fieldKey && field.value === '' && !isReportFieldDisabled(report, reportField, policy));
3942

4043
if (!reportFieldsEnabled || !reportField || !policyField || !report || isDisabled) {
4144
return (
@@ -64,7 +67,7 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) {
6467
goBack();
6568
} else {
6669
if (value !== '') {
67-
updateReportField(report.reportID, {...reportField, value}, reportField);
70+
updateReportField({...report, reportID: report.reportID}, {...reportField, value}, reportField, policy as unknown as Policy, hasOtherViolations);
6871
}
6972
goBack();
7073
}

0 commit comments

Comments
 (0)