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
22 changes: 6 additions & 16 deletions src/libs/actions/Policy/ReportField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,8 @@
import type {OnyxValueWithOfflineFeedback} from '@src/types/onyx/OnyxCommon';
import type {OnyxData} from '@src/types/onyx/Request';

let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
allReports = value;
},
});

const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 28 in src/libs/actions/Policy/ReportField.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
callback: (value, key) => {
if (!key) {
Expand Down Expand Up @@ -94,6 +85,7 @@
listValues: string[];
disabledListValues: boolean[];
policyID: string;
policyExpenseReportIDs: Array<string | undefined> | undefined;
};

function openPolicyReportFieldsPage(policyID: string) {
Expand Down Expand Up @@ -178,7 +170,7 @@
/**
* 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);
Expand All @@ -197,8 +189,6 @@
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}`,
Expand All @@ -210,8 +200,8 @@
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: {
Expand All @@ -234,8 +224,8 @@
},
},
},
...policyExpenseReports.map((report) => ({
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
...(policyExpenseReportIDs ?? []).map((reportID) => ({
key: `${ONYXKEYS.COLLECTION.REPORT}${reportID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
Expand Down
10 changes: 9 additions & 1 deletion src/pages/workspace/reports/CreateReportFieldsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ function WorkspaceCreateReportFieldsPage({
const {translate, localeCompare} = useLocalize();
const formRef = useRef<FormRef>(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;

Expand All @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions tests/actions/ReportFieldTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('actions/ReportField', () => {
initialValue: 'Default Value',
listValues: [],
disabledListValues: [],
policyExpenseReportIDs: [],
};

ReportField.createReportField(createReportFieldArguments);
Expand Down Expand Up @@ -133,6 +134,7 @@ describe('actions/ReportField', () => {
initialValue: defaultDate,
listValues: [],
disabledListValues: [],
policyExpenseReportIDs: [],
};

ReportField.createReportField(createReportFieldArguments);
Expand Down Expand Up @@ -189,6 +191,7 @@ describe('actions/ReportField', () => {
initialValue: '',
listValues: ['Value 1', 'Value 2'],
disabledListValues: [false, true],
policyExpenseReportIDs: [],
};

ReportField.createReportField(createReportFieldArguments);
Expand Down
Loading