Skip to content

Commit 5f485a7

Browse files
authored
Merge pull request Expensify#67985 from shubham1206agra/refactor-onyx-41
Removed Onyx.connect from DistanceRequestUtils
2 parents 44c6017 + 7d9327e commit 5f485a7

8 files changed

Lines changed: 189 additions & 589 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=264 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=262 --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/DistanceRequestUtils.ts

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
2-
import Onyx from 'react-native-onyx';
1+
import type {OnyxEntry} from 'react-native-onyx';
32
import type {LocaleContextProps} from '@components/LocaleContextProvider';
43
import CONST from '@src/CONST';
5-
import ONYXKEYS from '@src/ONYXKEYS';
6-
import type {LastSelectedDistanceRates, OnyxInputOrEntry, Report, Transaction} from '@src/types/onyx';
4+
import type {LastSelectedDistanceRates, OnyxInputOrEntry, Transaction} from '@src/types/onyx';
75
import type {Unit} from '@src/types/onyx/Policy';
86
import type Policy from '@src/types/onyx/Policy';
97
import {isEmptyObject} from '@src/types/utils/EmptyObject';
108
import {getCurrencySymbol} from './CurrencyUtils';
11-
import {getDistanceRateCustomUnit, getDistanceRateCustomUnitRate, getPersonalPolicy, getPolicy, getUnitRateValue} from './PolicyUtils';
12-
import {isPolicyExpenseChat} from './ReportUtils';
9+
import {getDistanceRateCustomUnit, getDistanceRateCustomUnitRate, getPersonalPolicy, getUnitRateValue} from './PolicyUtils';
1310
import {getCurrency, getRateID, isCustomUnitRateIDForP2P} from './TransactionUtils';
1411

1512
type MileageRate = {
@@ -21,23 +18,6 @@ type MileageRate = {
2118
enabled?: boolean;
2219
};
2320

24-
let lastSelectedDistanceRates: OnyxEntry<LastSelectedDistanceRates> = {};
25-
Onyx.connect({
26-
key: ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES,
27-
callback: (value) => {
28-
lastSelectedDistanceRates = value;
29-
},
30-
});
31-
32-
let allReports: OnyxCollection<Report>;
33-
Onyx.connect({
34-
key: ONYXKEYS.COLLECTION.REPORT,
35-
waitForCollectionCallback: true,
36-
callback: (value) => {
37-
allReports = value;
38-
},
39-
});
40-
4121
const METERS_TO_KM = 0.001; // 1 kilometer is 1000 meters
4222
const METERS_TO_MILES = 0.000621371; // There are approximately 0.000621371 miles in a meter
4323

@@ -298,23 +278,28 @@ function convertToDistanceInMeters(distance: number, unit: Unit): number {
298278
/**
299279
* Returns custom unit rate ID for the distance transaction
300280
*/
301-
function getCustomUnitRateID(reportID?: string) {
281+
function getCustomUnitRateID({
282+
reportID,
283+
isPolicyExpenseChat,
284+
policy,
285+
lastSelectedDistanceRates,
286+
}: {
287+
reportID: string | undefined;
288+
isPolicyExpenseChat: boolean;
289+
policy: OnyxEntry<Policy> | undefined;
290+
lastSelectedDistanceRates?: OnyxEntry<LastSelectedDistanceRates>;
291+
}): string {
302292
let customUnitRateID: string = CONST.CUSTOM_UNITS.FAKE_P2P_ID;
303293

304294
if (!reportID) {
305295
return customUnitRateID;
306296
}
307-
const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
308-
const parentReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`];
309-
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
310-
// eslint-disable-next-line deprecation/deprecation
311-
const policy = getPolicy(report?.policyID ?? parentReport?.policyID);
312297

313298
if (isEmptyObject(policy)) {
314299
return customUnitRateID;
315300
}
316301

317-
if (isPolicyExpenseChat(report) || isPolicyExpenseChat(parentReport)) {
302+
if (isPolicyExpenseChat) {
318303
const distanceUnit = Object.values(policy.customUnits ?? {}).find((unit) => unit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE);
319304
const lastSelectedDistanceRateID = lastSelectedDistanceRates?.[policy.id];
320305
const lastSelectedDistanceRate = lastSelectedDistanceRateID ? distanceUnit?.rates[lastSelectedDistanceRateID] : undefined;

src/libs/actions/IOU.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ type InitMoneyRequestParams = {
262262
newIouRequestType: IOURequestType | undefined;
263263
report: OnyxEntry<OnyxTypes.Report>;
264264
parentReport: OnyxEntry<OnyxTypes.Report>;
265+
lastSelectedDistanceRates?: OnyxEntry<OnyxTypes.LastSelectedDistanceRates>;
265266
};
266267

267268
type MoneyRequestInformation = {
@@ -912,7 +913,7 @@ function getReportPreviewAction(chatReportID: string | undefined, iouReportID: s
912913
* @param report the report to attach the transaction to
913914
* @param parentReport the parent report to attach the transaction to
914915
*/
915-
function initMoneyRequest({reportID, policy, isFromGlobalCreate, currentIouRequestType, newIouRequestType, report, parentReport}: InitMoneyRequestParams) {
916+
function initMoneyRequest({reportID, policy, isFromGlobalCreate, currentIouRequestType, newIouRequestType, report, parentReport, lastSelectedDistanceRates}: InitMoneyRequestParams) {
916917
// Generate a brand new transactionID
917918
// This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
918919
// eslint-disable-next-line deprecation/deprecation
@@ -953,7 +954,8 @@ function initMoneyRequest({reportID, policy, isFromGlobalCreate, currentIouReque
953954
waypoint1: {keyForList: 'stop_waypoint'},
954955
};
955956
if (!isFromGlobalCreate) {
956-
const customUnitRateID = DistanceRequestUtils.getCustomUnitRateID(reportID);
957+
const isPolicyExpenseChat = isPolicyExpenseChatReportUtil(report) || isPolicyExpenseChatReportUtil(parentReport);
958+
const customUnitRateID = DistanceRequestUtils.getCustomUnitRateID({reportID, isPolicyExpenseChat, policy, lastSelectedDistanceRates});
957959
comment.customUnit = {customUnitRateID};
958960
}
959961
}

src/pages/iou/request/IOURequestStartPage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function IOURequestStartPage({
6464
const isLoadingSelectedTab = shouldUseTab ? isLoadingOnyxValue(selectedTabResult) : false;
6565
const [transaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${getNonEmptyStringOnyxID(route?.params.transactionID)}`, {canBeMissing: true});
6666
const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: false});
67+
const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES, {canBeMissing: true});
6768
const [optimisticTransactions] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {
6869
selector: (items) => Object.values(items ?? {}),
6970
canBeMissing: true,
@@ -129,6 +130,7 @@ function IOURequestStartPage({
129130
newIouRequestType: transaction?.iouRequestType,
130131
report,
131132
parentReport,
133+
lastSelectedDistanceRates,
132134
});
133135
// eslint-disable-next-line
134136
}, []);
@@ -148,9 +150,10 @@ function IOURequestStartPage({
148150
newIouRequestType: newIOUType,
149151
report,
150152
parentReport,
153+
lastSelectedDistanceRates,
151154
});
152155
},
153-
[policy, reportID, isFromGlobalCreate, transaction, report, parentReport],
156+
[transaction?.iouRequestType, reportID, policy, isFromGlobalCreate, report, parentReport, lastSelectedDistanceRates],
154157
);
155158

156159
// Clear out the temporary expense if the reportID in the URL has changed from the transaction's reportID.

src/pages/iou/request/step/IOURequestStepDistance.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ function IOURequestStepDistance({
8787
const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: false});
8888
const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, {canBeMissing: false});
8989
const [skipConfirmation] = useOnyx(`${ONYXKEYS.COLLECTION.SKIP_CONFIRMATION}${transactionID}`, {canBeMissing: false});
90+
const [lastSelectedDistanceRates] = useOnyx(ONYXKEYS.NVP_LAST_SELECTED_DISTANCE_RATES, {canBeMissing: true});
9091
const [optimisticWaypoints, setOptimisticWaypoints] = useState<WaypointCollection | null>(null);
9192
const waypoints = useMemo(
9293
() =>
@@ -343,6 +344,8 @@ function IOURequestStepDistance({
343344
return;
344345
}
345346

347+
const isPolicyExpenseChat = !!participant?.isPolicyExpenseChat;
348+
346349
createDistanceRequest({
347350
report,
348351
participants,
@@ -359,7 +362,7 @@ function IOURequestStepDistance({
359362
billable: !!policy?.defaultBillable,
360363
reimbursable: !!policy?.defaultReimbursable,
361364
validWaypoints: getValidWaypoints(waypoints, true),
362-
customUnitRateID: DistanceRequestUtils.getCustomUnitRateID(report.reportID),
365+
customUnitRateID: DistanceRequestUtils.getCustomUnitRateID({reportID: report.reportID, isPolicyExpenseChat, policy, lastSelectedDistanceRates}),
363366
splitShares: transaction?.splitShares,
364367
attendees: transaction?.comment?.attendees,
365368
},
@@ -377,7 +380,12 @@ function IOURequestStepDistance({
377380
// and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense.
378381
if (iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(activePolicy) && activePolicy?.isPolicyExpenseChatEnabled && !shouldRestrictUserBillableActions(activePolicy.id)) {
379382
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, activePolicy?.id);
380-
const rateID = DistanceRequestUtils.getCustomUnitRateID(activePolicyExpenseChat?.reportID);
383+
const rateID = DistanceRequestUtils.getCustomUnitRateID({
384+
reportID: activePolicyExpenseChat?.reportID,
385+
isPolicyExpenseChat: true,
386+
policy: activePolicy,
387+
lastSelectedDistanceRates,
388+
});
381389
setCustomUnitRateID(transactionID, rateID);
382390
setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => {
383391
Navigation.navigate(
@@ -413,6 +421,7 @@ function IOURequestStepDistance({
413421
customUnitRateID,
414422
navigateToConfirmationPage,
415423
reportID,
424+
lastSelectedDistanceRates,
416425
]);
417426

418427
const getError = () => {

0 commit comments

Comments
 (0)