Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10880,11 +10880,22 @@ function getChatUsedForOnboarding(): OnyxEntry<Report> {
* Checks if given field has any violations and returns name of the first encountered one
*/
function getFieldViolation(violations: OnyxEntry<ReportViolations>, reportField: PolicyReportField): ReportViolationName | undefined {
if (!violations || !reportField) {
if (!reportField) {
return undefined;
}

return Object.values(CONST.REPORT_VIOLATIONS).find((violation) => !!violations[violation] && violations[violation][reportField.fieldID]);
if (!violations) {
return reportField.value ? undefined : CONST.REPORT_VIOLATIONS.FIELD_REQUIRED;
}

const fieldViolation = Object.values(CONST.REPORT_VIOLATIONS).find((violation) => !!violations[violation] && violations[violation][reportField.fieldID]);

// If the field has no value or no violation, we return 'fieldRequired' violation
if (!fieldViolation) {
return reportField.value ? undefined : CONST.REPORT_VIOLATIONS.FIELD_REQUIRED;
}

return fieldViolation;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2386,13 +2386,16 @@ function clearReportFieldKeyErrors(reportID: string | undefined, fieldKey: strin
});
}

function updateReportField(reportID: string, reportField: PolicyReportField, previousReportField: PolicyReportField) {
function updateReportField(report: Report, reportField: PolicyReportField, previousReportField: PolicyReportField, policy: Policy, shouldFixViolations = false) {
const reportID = report.reportID;
const fieldKey = getReportFieldKey(reportField.fieldID);
const reportViolations = getReportViolations(reportID);
const fieldViolation = getFieldViolation(reportViolations, reportField);
const recentlyUsedValues = allRecentlyUsedReportFields?.[fieldKey] ?? [];

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

const optimisticData: OnyxUpdate[] = [
{
Expand All @@ -2407,6 +2410,11 @@ function updateReportField(reportID: string, reportField: PolicyReportField, pre
},
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`,
value: optimisticNextStep,
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`,
Expand Down
5 changes: 4 additions & 1 deletion src/pages/EditReportFieldPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {Policy} from '@src/types/onyx';
import EditReportFieldDate from './EditReportFieldDate';
import EditReportFieldDropdown from './EditReportFieldDropdown';
import EditReportFieldText from './EditReportFieldText';
Expand All @@ -36,6 +37,8 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) {
const {translate} = useLocalize();
const isReportFieldTitle = isReportFieldOfTypeTitle(reportField);
const reportFieldsEnabled = ((isPaidGroupPolicyExpenseReport(report) || isInvoiceReport(report)) && !!policy?.areReportFieldsEnabled) || isReportFieldTitle;
const hasOtherViolations =
report?.fieldList && Object.entries(report.fieldList).some(([key, field]) => key !== fieldKey && field.value === '' && !isReportFieldDisabled(report, reportField, policy));

if (!reportFieldsEnabled || !reportField || !policyField || !report || isDisabled) {
return (
Expand Down Expand Up @@ -64,7 +67,7 @@ function EditReportFieldPage({route}: EditReportFieldPageProps) {
goBack();
} else {
if (value !== '') {
updateReportField(report.reportID, {...reportField, value}, reportField);
updateReportField({...report, reportID: report.reportID}, {...reportField, value}, reportField, policy as unknown as Policy, hasOtherViolations);
}
goBack();
}
Expand Down