Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimistically add report field for all expense reports #44894

Merged
merged 10 commits into from
Jul 22, 2024
37 changes: 32 additions & 5 deletions src/libs/actions/Policy/ReportFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {NullishDeep, OnyxCollection} from 'react-native-onyx';
import type {NullishDeep, OnyxCollection, OnyxUpdate} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import * as API from '@libs/API';
import type {CreateWorkspaceReportFieldParams} from '@libs/API/parameters';
Expand All @@ -10,7 +10,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {WorkspaceReportFieldsForm} from '@src/types/form/WorkspaceReportFieldsForm';
import INPUT_IDS from '@src/types/form/WorkspaceReportFieldsForm';
import type {Policy, PolicyReportField} from '@src/types/onyx';
import type {Policy, PolicyReportField, Report} from '@src/types/onyx';
import type {OnyxData} from '@src/types/onyx/Request';

let listValues: string[];
Expand All @@ -27,6 +27,13 @@ Onyx.connect({
},
});

let allReports: OnyxCollection<Report>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT,
dominictb marked this conversation as resolved.
Show resolved Hide resolved
waitForCollectionCallback: true,
callback: (value) => (allReports = value),
});

const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
Expand Down Expand Up @@ -136,7 +143,7 @@ function createReportField(policyID: string, {name, type, initialValue}: CreateR
const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {};
const fieldID = generateFieldID(name);
const fieldKey = ReportUtils.getReportFieldKey(fieldID);
const newReportField: PolicyReportField = {
const newReportField: Omit<PolicyReportField, 'value'> = {
name,
type,
defaultValue: initialValue,
Expand All @@ -145,19 +152,24 @@ function createReportField(policyID: string, {name, type, initialValue}: CreateR
fieldID,
orderWeight: Object.keys(previousFieldList).length + 1,
deletable: false,
value: type === CONST.REPORT_FIELD_TYPES.LIST ? CONST.REPORT_FIELD_TYPES.LIST : null,
keys: [],
externalIDs: [],
isTax: false,
};

const optimisticReportFieldDataForPolicy: PolicyReportField = {
...newReportField,
value: type === CONST.REPORT_FIELD_TYPES.LIST ? CONST.REPORT_FIELD_TYPES.LIST : null,
};

const onyxData: OnyxData = {
optimisticData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: newReportField,
[fieldKey]: optimisticReportFieldDataForPolicy,
},
pendingFields: {
[fieldKey]: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
Expand Down Expand Up @@ -196,6 +208,21 @@ function createReportField(policyID: string, {name, type, initialValue}: CreateR
},
],
};

const policyExpenseReports = Object.values(allReports ?? {}).filter((report) => report?.policyID === policyID && report.type === CONST.REPORT.TYPE.EXPENSE) as Report[];

onyxData.optimisticData?.push(
...(policyExpenseReports.map((report) => ({
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: newReportField,
},
},
})) as OnyxUpdate[]),
);

const parameters: CreateWorkspaceReportFieldParams = {
policyID,
reportFields: JSON.stringify([newReportField]),
Expand Down
Loading