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
33 changes: 30 additions & 3 deletions src/libs/actions/Policy/ReportField.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, PolicyReportFieldsReplace} from '@libs/API/parameters';
Expand Down Expand Up @@ -28,6 +28,13 @@
},
});

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

dominictb marked this conversation as resolved.
Show resolved Hide resolved
const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
Expand Down Expand Up @@ -136,7 +143,7 @@
const previousFieldList = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.fieldList ?? {};
const fieldID = generateFieldID(name);
const fieldKey = ReportUtils.getReportFieldKey(fieldID);
const newReportField: OnyxValueWithOfflineFeedback<PolicyReportField> = {

Check failure on line 146 in src/libs/actions/Policy/ReportField.ts

View workflow job for this annotation

GitHub Actions / typecheck

Type '{ name: string; type: PolicyReportFieldType; defaultValue: string; values: string[]; disabledOptions: boolean[]; fieldID: string; orderWeight: number; deletable: false; keys: never[]; externalIDs: never[]; isTax: false; pendingAction: "add"; }' is not assignable to type 'PolicyReportField & OfflineFeedback<keyof PolicyReportField>'.
name,
type,
defaultValue: initialValue,
Expand All @@ -145,20 +152,25 @@
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,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
};

const optimisticReportFieldDataForPolicy: OnyxValueWithOfflineFeedback<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,
},
errorFields: null,
},
Expand Down Expand Up @@ -191,6 +203,21 @@
},
],
};

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

onyxData.optimisticData?.push(
dominictb marked this conversation as resolved.
Show resolved Hide resolved
...(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