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
52 changes: 38 additions & 14 deletions src/libs/actions/Policy/ReportField.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cloneDeep from 'lodash/cloneDeep';
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 {
Expand All @@ -14,6 +14,7 @@ import type {
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
import * as ErrorUtils from '@libs/ErrorUtils';
import Log from '@libs/Log';
import * as ReportConnection from '@libs/ReportConnection';
import * as ReportUtils from '@libs/ReportUtils';
import * as WorkspaceReportFieldUtils from '@libs/WorkspaceReportFieldUtils';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -159,7 +160,7 @@ function createReportField(policyID: string, {name, type, initialValue}: CreateR
const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {};
const fieldID = WorkspaceReportFieldUtils.generateFieldID(name);
const fieldKey = ReportUtils.getReportFieldKey(fieldID);
const newReportField: PolicyReportField = {
const newReportField: Omit<OnyxValueWithOfflineFeedback<PolicyReportField>, 'value'> = {
name,
type,
defaultValue: initialValue,
Expand All @@ -168,24 +169,46 @@ 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 onyxData: OnyxData = {
optimisticData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: {...newReportField, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD},
},
errorFields: null,

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

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

const onyxOperationsToAddExpenseReportFields = policyExpenseReports.map((report) => ({
key: `${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`,
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: {...newReportField, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD},
},
},
}));

const optimisticData = [
...onyxOperationsToAddExpenseReportFields,
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
mountiny marked this conversation as resolved.
Show resolved Hide resolved
onyxMethod: Onyx.METHOD.MERGE,
value: {
fieldList: {
[fieldKey]: {...optimisticReportFieldDataForPolicy, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD},
},
errorFields: null,
},
],
},
] as OnyxUpdate[];

const onyxData: OnyxData = {
optimisticData,
successData: [
{
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
Expand Down Expand Up @@ -213,6 +236,7 @@ function createReportField(policyID: string, {name, type, initialValue}: CreateR
},
],
};

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