Skip to content

Commit 92e0649

Browse files
authored
Merge pull request Expensify#67472 from shubham1206agra/refactor-onyx-17
Refactored Onyx.connect in `src/libs/actions/Policy/PerDiem.ts`
2 parents d606587 + 74022de commit 92e0649

4 files changed

Lines changed: 22 additions & 65 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=297 --cache --cache-location=node_modules/.cache/eslint",
49+
"lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=295 --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/IOU.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ import {isEmptyObject} from '@src/types/utils/EmptyObject';
225225
import {clearByKey as clearPdfByOnyxKey} from './CachedPDFPaths';
226226
import {buildOptimisticPolicyRecentlyUsedCategories, getPolicyCategoriesData} from './Policy/Category';
227227
import {buildAddMembersToWorkspaceOnyxData, buildUpdateWorkspaceMembersRoleOnyxData} from './Policy/Member';
228-
import {buildOptimisticPolicyRecentlyUsedDestinations} from './Policy/PerDiem';
229228
import {buildOptimisticRecentlyUsedCurrencies, buildPolicyData, generatePolicyID} from './Policy/Policy';
230229
import {buildOptimisticPolicyRecentlyUsedTags, getPolicyTagsData} from './Policy/Tag';
231230
import type {GuidedSetupData} from './Report';
@@ -406,6 +405,10 @@ type BasePolicyParams = {
406405
policyCategories?: OnyxEntry<OnyxTypes.PolicyCategories>;
407406
};
408407

408+
type RecentlyUsedParams = {
409+
destinations?: OnyxEntry<OnyxTypes.RecentlyUsedCategories>;
410+
};
411+
409412
type RequestMoneyParticipantParams = {
410413
payeeEmail: string | undefined;
411414
payeeAccountID: number;
@@ -416,6 +419,7 @@ type PerDiemExpenseInformation = {
416419
report: OnyxEntry<OnyxTypes.Report>;
417420
participantParams: RequestMoneyParticipantParams;
418421
policyParams?: BasePolicyParams;
422+
recentlyUsedParams?: RecentlyUsedParams;
419423
transactionParams: PerDiemExpenseTransactionParams;
420424
};
421425

@@ -424,6 +428,7 @@ type PerDiemExpenseInformationParams = {
424428
transactionParams: PerDiemExpenseTransactionParams;
425429
participantParams: RequestMoneyParticipantParams;
426430
policyParams?: BasePolicyParams;
431+
recentlyUsedParams?: RecentlyUsedParams;
427432
moneyRequestReportID?: string;
428433
};
429434

@@ -3563,9 +3568,10 @@ function computeDefaultPerDiemExpenseComment(customUnit: TransactionCustomUnit,
35633568
* it creates optimistic versions of them and uses those instead
35643569
*/
35653570
function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseInformationParams): MoneyRequestInformation {
3566-
const {parentChatReport, transactionParams, participantParams, policyParams = {}, moneyRequestReportID = ''} = perDiemExpenseInformation;
3571+
const {parentChatReport, transactionParams, participantParams, policyParams = {}, recentlyUsedParams = {}, moneyRequestReportID = ''} = perDiemExpenseInformation;
35673572
const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams;
35683573
const {policy, policyCategories, policyTagList} = policyParams;
3574+
const {destinations: recentlyUsedDestinations} = recentlyUsedParams;
35693575
const {comment = '', currency, created, category, tag, customUnit, billable, attendees} = transactionParams;
35703576

35713577
const amount = computePerDiemExpenseAmount(customUnit);
@@ -3655,7 +3661,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
36553661
const optimisticPolicyRecentlyUsedCategories = buildOptimisticPolicyRecentlyUsedCategories(iouReport.policyID, category);
36563662
const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags(iouReport.policyID, tag);
36573663
const optimisticPolicyRecentlyUsedCurrencies = buildOptimisticRecentlyUsedCurrencies(currency);
3658-
const optimisticPolicyRecentlyUsedDestinations = buildOptimisticPolicyRecentlyUsedDestinations(iouReport.policyID, customUnit.customUnitRateID);
3664+
const optimisticPolicyRecentlyUsedDestinations = customUnit.customUnitRateID ? [...new Set([customUnit.customUnitRateID, ...(recentlyUsedDestinations ?? [])])] : [];
36593665

36603666
// STEP 4: Build optimistic reportActions. We need:
36613667
// 1. CREATED action for the chatReport
@@ -5646,7 +5652,7 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) {
56465652
* Submit per diem expense to another user
56475653
*/
56485654
function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInformation) {
5649-
const {report, participantParams, policyParams = {}, transactionParams} = submitPerDiemExpenseInformation;
5655+
const {report, participantParams, policyParams = {}, recentlyUsedParams = {}, transactionParams} = submitPerDiemExpenseInformation;
56505656
const {payeeAccountID} = participantParams;
56515657
const {currency, comment = '', category, tag, created, customUnit, attendees} = transactionParams;
56525658

@@ -5682,6 +5688,7 @@ function submitPerDiemExpense(submitPerDiemExpenseInformation: PerDiemExpenseInf
56825688
parentChatReport: currentChatReport,
56835689
participantParams,
56845690
policyParams,
5691+
recentlyUsedParams,
56855692
transactionParams,
56865693
moneyRequestReportID,
56875694
});

src/libs/actions/Policy/PerDiem.ts

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,22 @@
11
import lodashDeepClone from 'lodash/cloneDeep';
2-
import lodashUnion from 'lodash/union';
3-
import type {NullishDeep, OnyxCollection} from 'react-native-onyx';
2+
import type {NullishDeep} from 'react-native-onyx';
43
import Onyx from 'react-native-onyx';
54
import * as API from '@libs/API';
65
import {READ_COMMANDS, WRITE_COMMANDS} from '@libs/API/types';
7-
import * as ApiUtils from '@libs/ApiUtils';
6+
import {getCommandURL} from '@libs/ApiUtils';
87
import fileDownload from '@libs/fileDownload';
98
import getIsNarrowLayout from '@libs/getIsNarrowLayout';
109
import {translateLocal} from '@libs/Localize';
1110
import enhanceParameters from '@libs/Network/enhanceParameters';
12-
import * as NumberUtils from '@libs/NumberUtils';
11+
import {generateHexadecimalValue} from '@libs/NumberUtils';
1312
import {goBackWhenEnableFeature} from '@libs/PolicyUtils';
14-
import * as ReportUtils from '@libs/ReportUtils';
1513
import CONST from '@src/CONST';
1614
import ONYXKEYS from '@src/ONYXKEYS';
17-
import type {Policy, RecentlyUsedCategories, Report} from '@src/types/onyx';
1815
import type {ErrorFields, PendingAction} from '@src/types/onyx/OnyxCommon';
1916
import type {CustomUnit, Rate} from '@src/types/onyx/Policy';
2017
import type {OnyxData} from '@src/types/onyx/Request';
2118
import {isEmptyObject} from '@src/types/utils/EmptyObject';
2219

23-
const allPolicies: OnyxCollection<Policy> = {};
24-
Onyx.connect({
25-
key: ONYXKEYS.COLLECTION.POLICY,
26-
callback: (val, key) => {
27-
if (!key) {
28-
return;
29-
}
30-
if (val === null || val === undefined) {
31-
// If we are deleting a policy, we have to check every report linked to that policy
32-
// 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.
33-
// More info: https://github.com/Expensify/App/issues/14260
34-
const policyID = key.replace(ONYXKEYS.COLLECTION.POLICY, '');
35-
const policyReports = ReportUtils.getAllPolicyReports(policyID);
36-
const cleanUpMergeQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT}${string}`, NullishDeep<Report>> = {};
37-
const cleanUpSetQueries: Record<`${typeof ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${string}` | `${typeof ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${string}`, null> = {};
38-
policyReports.forEach((policyReport) => {
39-
if (!policyReport) {
40-
return;
41-
}
42-
const {reportID} = policyReport;
43-
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`] = null;
44-
cleanUpSetQueries[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_DRAFTS}${reportID}`] = null;
45-
});
46-
Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, cleanUpMergeQueries);
47-
Onyx.multiSet(cleanUpSetQueries);
48-
delete allPolicies[key];
49-
return;
50-
}
51-
52-
allPolicies[key] = val;
53-
},
54-
});
55-
5620
type SubRateData = {
5721
pendingAction?: PendingAction;
5822
destination: string;
@@ -67,7 +31,7 @@ type SubRateData = {
6731
* Returns a client generated 13 character hexadecimal value for a custom unit ID
6832
*/
6933
function generateCustomUnitID(): string {
70-
return NumberUtils.generateHexadecimalValue(13);
34+
return generateHexadecimalValue(13);
7135
}
7236

7337
function enablePerDiem(policyID: string, enabled: boolean, customUnitID?: string, shouldGoBack?: boolean) {
@@ -193,7 +157,7 @@ function downloadPerDiemCSV(policyID: string, onDownloadFailed: () => void) {
193157
formData.append(key, String(value));
194158
});
195159

196-
fileDownload(ApiUtils.getCommandURL({command: WRITE_COMMANDS.EXPORT_PER_DIEM_CSV}), fileName, '', false, formData, CONST.NETWORK.METHOD.POST, onDownloadFailed);
160+
fileDownload(getCommandURL({command: WRITE_COMMANDS.EXPORT_PER_DIEM_CSV}), fileName, '', false, formData, CONST.NETWORK.METHOD.POST, onDownloadFailed);
197161
}
198162

199163
function clearPolicyPerDiemRatesErrorFields(policyID: string, customUnitID: string, updatedErrorFields: ErrorFields) {
@@ -400,23 +364,6 @@ function editPerDiemRateCurrency(policyID: string, rateID: string, customUnit: C
400364
API.write(WRITE_COMMANDS.UPDATE_WORKSPACE_CUSTOM_UNIT, parameters, onyxData);
401365
}
402366

403-
let allRecentlyUsedDestinations: OnyxCollection<RecentlyUsedCategories> = {};
404-
Onyx.connect({
405-
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS,
406-
waitForCollectionCallback: true,
407-
callback: (val) => (allRecentlyUsedDestinations = val),
408-
});
409-
410-
function buildOptimisticPolicyRecentlyUsedDestinations(policyID: string | undefined, destination: string | undefined) {
411-
if (!policyID || !destination) {
412-
return [];
413-
}
414-
415-
const policyRecentlyUsedDestinations = allRecentlyUsedDestinations?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS}${policyID}`] ?? [];
416-
417-
return lodashUnion([destination], policyRecentlyUsedDestinations);
418-
}
419-
420367
export {
421368
generateCustomUnitID,
422369
enablePerDiem,
@@ -429,5 +376,4 @@ export {
429376
editPerDiemRateSubrate,
430377
editPerDiemRateAmount,
431378
editPerDiemRateCurrency,
432-
buildOptimisticPolicyRecentlyUsedDestinations,
433379
};

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ function IOURequestStepConfirmation({
130130
const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${realPolicyID}`, {canBeMissing: true});
131131
const [userLocation] = useOnyx(ONYXKEYS.USER_LOCATION, {canBeMissing: true});
132132
const [reportAttributesDerived] = useOnyx(ONYXKEYS.DERIVED.REPORT_ATTRIBUTES, {canBeMissing: true, selector: (val) => val?.reports});
133+
const [recentlyUsedDestinations] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_DESTINATIONS}${realPolicyID}`, {canBeMissing: true});
133134

134135
/*
135136
* We want to use a report from the transaction if it exists
@@ -536,6 +537,9 @@ function IOURequestStepConfirmation({
536537
policyTagList: policyTags,
537538
policyCategories,
538539
},
540+
recentlyUsedParams: {
541+
destinations: recentlyUsedDestinations,
542+
},
539543
transactionParams: {
540544
currency: transaction.currency,
541545
created: transaction.created,
@@ -548,7 +552,7 @@ function IOURequestStepConfirmation({
548552
},
549553
});
550554
},
551-
[report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories],
555+
[report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy, policyTags, policyCategories, recentlyUsedDestinations],
552556
);
553557

554558
const trackExpense = useCallback(

0 commit comments

Comments
 (0)