diff --git a/src/libs/actions/Policy/ReportField.ts b/src/libs/actions/Policy/ReportField.ts index 70f44d158b95..bc19c9d46d89 100644 --- a/src/libs/actions/Policy/ReportField.ts +++ b/src/libs/actions/Policy/ReportField.ts @@ -24,15 +24,6 @@ import type {Policy, PolicyReportField, Report} from '@src/types/onyx'; import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon'; import type {OnyxData} from '@src/types/onyx/Request'; -let allReports: OnyxCollection; -Onyx.connect({ - key: ONYXKEYS.COLLECTION.REPORT, - waitForCollectionCallback: true, - callback: (value) => { - allReports = value; - }, -}); - const allPolicies: OnyxCollection = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY, @@ -94,6 +85,7 @@ type CreateReportFieldParams = Pick | undefined; }; function openPolicyReportFieldsPage(policyID: string) { @@ -178,7 +170,7 @@ function deleteReportFieldsListValue({valueIndexes, listValues, disabledListValu /** * Creates a new report field. */ -function createReportField({name, type, initialValue, listValues, disabledListValues, policyID}: CreateReportFieldParams) { +function createReportField({name, type, initialValue, listValues, disabledListValues, policyID, policyExpenseReportIDs}: CreateReportFieldParams) { const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {}; const fieldID = WorkspaceReportFieldUtils.generateFieldID(name); const fieldKey = ReportUtils.getReportFieldKey(fieldID); @@ -197,8 +189,6 @@ function createReportField({name, type, initialValue, listValues, disabledListVa isTax: false, }; - const policyExpenseReports = Object.values(allReports ?? {}).filter((report) => report?.policyID === policyID && report.type === CONST.REPORT.TYPE.EXPENSE) as Report[]; - const optimisticData = [ { key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, @@ -210,8 +200,8 @@ function createReportField({name, type, initialValue, listValues, disabledListVa errorFields: null, }, }, - ...policyExpenseReports.map((report) => ({ - key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + ...(policyExpenseReportIDs ?? []).map((reportID) => ({ + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { @@ -234,8 +224,8 @@ function createReportField({name, type, initialValue, listValues, disabledListVa }, }, }, - ...policyExpenseReports.map((report) => ({ - key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, + ...(policyExpenseReportIDs ?? []).map((reportID) => ({ + key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`, onyxMethod: Onyx.METHOD.MERGE, value: { fieldList: { diff --git a/src/pages/workspace/reports/CreateReportFieldsPage.tsx b/src/pages/workspace/reports/CreateReportFieldsPage.tsx index 12ee04a14aed..132a6b724052 100644 --- a/src/pages/workspace/reports/CreateReportFieldsPage.tsx +++ b/src/pages/workspace/reports/CreateReportFieldsPage.tsx @@ -43,6 +43,13 @@ function WorkspaceCreateReportFieldsPage({ const {translate, localeCompare} = useLocalize(); const formRef = useRef(null); const [formDraft] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_REPORT_FIELDS_FORM_DRAFT, {canBeMissing: true}); + const [policyExpenseReportIDs] = useOnyx(ONYXKEYS.COLLECTION.REPORT, { + canBeMissing: true, + selector: (value) => + Object.values(value ?? {}) + .filter((report) => report?.policyID === policyID && report.type === CONST.REPORT.TYPE.EXPENSE) + .map((report) => report?.reportID), + }); const availableListValuesLength = (formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? []).filter((disabledListValue) => !disabledListValue).length; @@ -55,10 +62,11 @@ function WorkspaceCreateReportFieldsPage({ initialValue: !(values[INPUT_IDS.TYPE] === CONST.REPORT_FIELD_TYPES.LIST && availableListValuesLength === 0) ? values[INPUT_IDS.INITIAL_VALUE] : '', listValues: formDraft?.[INPUT_IDS.LIST_VALUES] ?? [], disabledListValues: formDraft?.[INPUT_IDS.DISABLED_LIST_VALUES] ?? [], + policyExpenseReportIDs, }); Navigation.goBack(); }, - [availableListValuesLength, formDraft, policyID], + [availableListValuesLength, formDraft, policyExpenseReportIDs, policyID], ); const validateForm = useCallback( diff --git a/tests/actions/ReportFieldTest.ts b/tests/actions/ReportFieldTest.ts index ecf3ae18a2ae..5df387fa4655 100644 --- a/tests/actions/ReportFieldTest.ts +++ b/tests/actions/ReportFieldTest.ts @@ -79,6 +79,7 @@ describe('actions/ReportField', () => { initialValue: 'Default Value', listValues: [], disabledListValues: [], + policyExpenseReportIDs: [], }; ReportField.createReportField(createReportFieldArguments); @@ -133,6 +134,7 @@ describe('actions/ReportField', () => { initialValue: defaultDate, listValues: [], disabledListValues: [], + policyExpenseReportIDs: [], }; ReportField.createReportField(createReportFieldArguments); @@ -189,6 +191,7 @@ describe('actions/ReportField', () => { initialValue: '', listValues: ['Value 1', 'Value 2'], disabledListValues: [false, true], + policyExpenseReportIDs: [], }; ReportField.createReportField(createReportFieldArguments);