Skip to content

Commit 17051f9

Browse files
authored
Merge pull request Expensify#68004 from shubham1206agra/refactor-onyx-43
Removed Onyx.connect from DistanceRate Actions (part 2)
2 parents 99770cf + 16b5cac commit 17051f9

4 files changed

Lines changed: 121 additions & 71 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:debug": "TZ=utc NODE_OPTIONS='--inspect-brk --experimental-vm-modules' jest --runInBand",
4747
"perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure",
4848
"typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc",
49-
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=251 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=250 --cache --cache-location=node_modules/.cache/eslint",
5050
"lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh",
5151
"lint-watch": "npx eslint-watch --watch --changed",
5252
"shellcheck": "./scripts/shellCheck.sh",

src/libs/actions/Policy/DistanceRate.ts

Lines changed: 28 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,48 +14,14 @@ import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
1414
import * as ErrorUtils from '@libs/ErrorUtils';
1515
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
1616
import {buildOnyxDataForPolicyDistanceRateUpdates} from '@libs/PolicyDistanceRatesUtils';
17-
import {getDistanceRateCustomUnit, goBackWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
18-
import * as ReportUtils from '@libs/ReportUtils';
17+
import {goBackWhenEnableFeature, removePendingFieldsFromCustomUnit} from '@libs/PolicyUtils';
1918
import CONST from '@src/CONST';
2019
import ONYXKEYS from '@src/ONYXKEYS';
21-
import type {Policy, Report, TransactionViolation} from '@src/types/onyx';
20+
import type {TransactionViolation} from '@src/types/onyx';
2221
import type {ErrorFields} from '@src/types/onyx/OnyxCommon';
2322
import type {CustomUnit, Rate} from '@src/types/onyx/Policy';
2423
import type {OnyxData} from '@src/types/onyx/Request';
2524

26-
const allPolicies: OnyxCollection<Policy> = {};
27-
Onyx.connect({
28-
key: ONYXKEYS.COLLECTION.POLICY,
29-
callback: (val, key) => {
30-
if (!key) {
31-
return;
32-
}
33-
if (val === null || val === undefined) {
34-
// If we are deleting a policy, we have to check every report linked to that policy
35-
// and unset the draft indicator (pencil icon) alongside removing any draft comments. Clearing these values will keep the newly archived chats from being displayed in the LHN.
36-
// More info: https://github.com/Expensify/App/issues/14260
37-
const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, '');
38-
const policyReports = ReportUtils.getAllPolicyReports(policyID);
39-
const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep<Report>> = {};
40-
const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {};
41-
policyReports.forEach((policyReport) => {
42-
if (!policyReport) {
43-
return;
44-
}
45-
const {reportID} = policyReport;
46-
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null;
47-
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null;
48-
});
49-
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries);
50-
Onyx.multiSet(cleanUpSetQueries);
51-
delete allPolicies[key];
52-
return;
53-
}
54-
55-
allPolicies[key] = val;
56-
},
57-
});
58-
5925
/**
6026
* Takes array of customUnitRates and removes pendingFields and errorFields from each rate - we don't want to send those via API
6127
*/
@@ -81,7 +47,7 @@ function openPolicyDistanceRatesPage(policyID?: string) {
8147
API.read(READ_COMMANDS.OPEN_POLICY_DISTANCE_RATES_PAGE, params);
8248
}
8349

84-
function enablePolicyDistanceRates(policyID: string, enabled: boolean) {
50+
function enablePolicyDistanceRates(policyID: string, enabled: boolean, customUnit: CustomUnit | undefined) {
8551
const onyxData: OnyxData = {
8652
optimisticData: [
8753
{
@@ -120,39 +86,34 @@ function enablePolicyDistanceRates(policyID: string, enabled: boolean) {
12086
],
12187
};
12288

123-
if (!enabled) {
124-
const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`];
125-
const customUnit = getDistanceRateCustomUnit(policy);
126-
if (customUnit) {
127-
const customUnitID = customUnit.customUnitID;
128-
129-
const rateEntries = Object.entries(customUnit.rates ?? {});
130-
// find the rate to be enabled after disabling the distance rate feature
131-
const rateEntryToBeEnabled = rateEntries.at(0);
89+
if (!enabled && customUnit) {
90+
const customUnitID = customUnit.customUnitID;
91+
const rateEntries = Object.entries(customUnit.rates ?? {});
92+
// find the rate to be enabled after disabling the distance rate feature
93+
const rateEntryToBeEnabled = rateEntries.at(0);
13294

133-
onyxData.optimisticData?.push({
134-
onyxMethod: Onyx.METHOD.MERGE,
135-
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
136-
value: {
137-
customUnits: {
138-
[customUnitID]: {
139-
rates: Object.fromEntries(
140-
rateEntries.map((rateEntry) => {
141-
const [rateID, rate] = rateEntry;
142-
return [
143-
rateID,
144-
{
145-
...rate,
146-
enabled: rateID === rateEntryToBeEnabled?.at(0),
147-
},
148-
];
149-
}),
150-
),
151-
},
95+
onyxData.optimisticData?.push({
96+
onyxMethod: Onyx.METHOD.MERGE,
97+
key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`,
98+
value: {
99+
customUnits: {
100+
[customUnitID]: {
101+
rates: Object.fromEntries(
102+
rateEntries.map((rateEntry) => {
103+
const [rateID, rate] = rateEntry;
104+
return [
105+
rateID,
106+
{
107+
...rate,
108+
enabled: rateID === rateEntryToBeEnabled?.at(0),
109+
},
110+
];
111+
}),
112+
),
152113
},
153114
},
154-
});
155-
}
115+
},
116+
});
156117
}
157118

158119
const parameters: EnablePolicyDistanceRatesParams = {policyID, enabled};

src/pages/workspace/WorkspaceMoreFeaturesPage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {getLatestErrorField} from '@libs/ErrorUtils';
2020
import Navigation from '@libs/Navigation/Navigation';
2121
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
2222
import type {WorkspaceSplitNavigatorParamList} from '@libs/Navigation/types';
23-
import {getPerDiemCustomUnit, isControlPolicy} from '@libs/PolicyUtils';
23+
import {getDistanceRateCustomUnit, getPerDiemCustomUnit, isControlPolicy} from '@libs/PolicyUtils';
2424
import {enablePolicyCategories} from '@userActions/Policy/Category';
2525
import {enablePolicyDistanceRates} from '@userActions/Policy/DistanceRate';
2626
import {enablePerDiem} from '@userActions/Policy/PerDiem';
@@ -99,6 +99,7 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
9999
const [isDisableWorkflowWarningModalOpen, setIsDisableWorkflowWarningModalOpen] = useState(false);
100100

101101
const perDiemCustomUnit = getPerDiemCustomUnit(policy);
102+
const distanceRateCustomUnit = getDistanceRateCustomUnit(policy);
102103

103104
const [cardList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`, {canBeMissing: true});
104105
const workspaceCards = getAllCardsForWorkspace(workspaceAccountID, cardList, cardFeeds);
@@ -132,7 +133,7 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
132133
if (!policyID) {
133134
return;
134135
}
135-
enablePolicyDistanceRates(policyID, isEnabled);
136+
enablePolicyDistanceRates(policyID, isEnabled, distanceRateCustomUnit);
136137
},
137138
},
138139
{

tests/unit/DistanceRateTest.ts

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type {OnyxKey} from 'react-native-onyx';
12
import Onyx from 'react-native-onyx';
2-
import {deletePolicyDistanceRates} from '@libs/actions/Policy/DistanceRate';
3+
import {deletePolicyDistanceRates, enablePolicyDistanceRates} from '@libs/actions/Policy/DistanceRate';
34
import CONST from '@src/CONST';
45
import ONYXKEYS from '@src/ONYXKEYS';
56
import type {Policy, Transaction, TransactionViolations} from '@src/types/onyx';
@@ -94,4 +95,91 @@ describe('DistanceRate', () => {
9495
});
9596
});
9697
});
98+
99+
describe('enablePolicyDistanceRates', () => {
100+
it('should disable all rates except the default rate when the we disable the feature', async () => {
101+
const customUnitID = '5A55C2B68DDCB';
102+
const customUnitRateID1 = '7255CA72C7E7B';
103+
const customUnitRateID2 = '7255CA72C7E72';
104+
const policy: Policy = {
105+
...createRandomPolicy(3),
106+
...{
107+
areDistanceRatesEnabled: true,
108+
customUnits: {
109+
[customUnitID]: {
110+
attributes: {
111+
unit: CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES,
112+
},
113+
customUnitID,
114+
defaultCategory: 'Car',
115+
enabled: true,
116+
name: 'Distance',
117+
rates: {
118+
[customUnitRateID1]: {
119+
currency: 'ETB',
120+
customUnitRateID: customUnitRateID1,
121+
enabled: true,
122+
name: 'Default Rate',
123+
rate: 70,
124+
subRates: [],
125+
},
126+
[customUnitRateID2]: {
127+
currency: 'ETB',
128+
customUnitRateID: customUnitRateID2,
129+
enabled: true,
130+
name: 'Default Rate',
131+
rate: 71,
132+
subRates: [],
133+
},
134+
},
135+
},
136+
},
137+
},
138+
};
139+
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy);
140+
141+
if (policy.customUnits) {
142+
enablePolicyDistanceRates(policy.id, false, policy.customUnits[customUnitID]);
143+
}
144+
await waitForBatchedUpdates();
145+
const onyxPolicy = await new Promise<Policy>((resolve) => {
146+
Onyx.connect({
147+
key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}` as OnyxKey,
148+
// eslint-disable-next-line rulesdir/prefer-early-return
149+
callback: (value) => {
150+
if (value !== undefined) {
151+
resolve(value as Policy);
152+
}
153+
},
154+
});
155+
});
156+
157+
if (!policy || !policy.customUnits) {
158+
return;
159+
}
160+
161+
expect(onyxPolicy).toEqual({
162+
...policy,
163+
areDistanceRatesEnabled: false,
164+
pendingFields: {
165+
areDistanceRatesEnabled: 'update',
166+
},
167+
customUnits: {
168+
[customUnitID]: {
169+
...policy.customUnits[customUnitID],
170+
rates: {
171+
[customUnitRateID1]: {
172+
...policy.customUnits[customUnitID].rates[customUnitRateID1],
173+
enabled: true,
174+
},
175+
[customUnitRateID2]: {
176+
...policy.customUnits[customUnitID].rates[customUnitRateID2],
177+
enabled: false,
178+
},
179+
},
180+
},
181+
},
182+
});
183+
});
184+
});
97185
});

0 commit comments

Comments
 (0)