Skip to content

Commit 0fe3426

Browse files
refactor: remove module-level collection subscriptions
1 parent d1d92ee commit 0fe3426

2 files changed

Lines changed: 81 additions & 103 deletions

File tree

src/hooks/useTransactionInlineEdit.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {useCallback, useRef} from 'react';
77
import type {OnyxEntry} from 'react-native-onyx';
88
import {useSearchStateContext} from '@components/Search/SearchContext';
99
import type {SearchQueryJSON} from '@components/Search/types';
10+
import type {TransactionInlineEditParams} from '@libs/actions/TransactionInlineEdit';
1011
import {
1112
editTransactionAmountInline,
1213
editTransactionCategoryInline,
@@ -134,6 +135,12 @@ function useTransactionInlineEdit({
134135
const [policyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${getNonEmptyStringOnyxID(policyID)}`);
135136
const [transactionThreadNVP] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(transactionThreadReportID)}`);
136137
const [chatReportNVP] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${getNonEmptyStringOnyxID(chatReportID)}`);
138+
const [policyRecentlyUsedCategories] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${getNonEmptyStringOnyxID(policyID)}`);
139+
const [policyRecentlyUsedTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${getNonEmptyStringOnyxID(policyID)}`);
140+
const [parentReportNextStep] = useOnyx(`${ONYXKEYS.COLLECTION.NEXT_STEP}${getNonEmptyStringOnyxID(reportID)}`);
141+
142+
const originalTransactionID = transaction?.comment?.originalTransactionID;
143+
const [originalTransaction] = useOnyx(`${ONYXKEYS.COLLECTION.TRANSACTION}${getNonEmptyStringOnyxID(originalTransactionID)}`);
137144

138145
const {hasSelectedTransactions} = useSearchStateContext();
139146

@@ -147,33 +154,49 @@ function useTransactionInlineEdit({
147154
policyTags,
148155
transactionThreadNVP,
149156
chatReportNVP,
157+
originalTransaction,
150158
disabled: hasSelectedTransactions,
151159
});
152160

153161
const wasEditingOnMouseDownRef = useRef(false);
154162

163+
const getEditParams = (): TransactionInlineEditParams => {
164+
return {
165+
hash,
166+
transactionID,
167+
parentReport: parentReport ?? fallbackReport,
168+
transactionThreadReport,
169+
policy,
170+
policyCategories,
171+
policyTags,
172+
policyRecentlyUsedCategories,
173+
policyRecentlyUsedTags,
174+
parentReportNextStep,
175+
};
176+
};
177+
155178
const onEditDate = (newDate: string) => {
156-
editTransactionDateInline(hash, transactionID, transactionThreadReportID, newDate);
179+
editTransactionDateInline(getEditParams(), newDate);
157180
};
158181

159182
const onEditMerchant = (newMerchant: string) => {
160-
editTransactionMerchantInline(hash, transactionID, transactionThreadReportID, newMerchant);
183+
editTransactionMerchantInline(getEditParams(), newMerchant);
161184
};
162185

163186
const onEditDescription = (newDescription: string) => {
164-
editTransactionDescriptionInline(hash, transactionID, transactionThreadReportID, newDescription);
187+
editTransactionDescriptionInline(getEditParams(), newDescription);
165188
};
166189

167190
const onEditCategory = (newCategory: string) => {
168-
editTransactionCategoryInline(hash, transactionID, transactionThreadReportID, newCategory);
191+
editTransactionCategoryInline(getEditParams(), newCategory);
169192
};
170193

171194
const onEditAmount = (newAmount: number) => {
172-
editTransactionAmountInline(hash, transactionID, transactionThreadReportID, newAmount);
195+
editTransactionAmountInline(getEditParams(), newAmount);
173196
};
174197

175198
const onEditTag = (newTag: string) => {
176-
editTransactionTagInline(hash, transactionID, transactionThreadReportID, newTag);
199+
editTransactionTagInline(getEditParams(), newTag);
177200
};
178201

179202
return {

src/libs/actions/TransactionInlineEdit.ts

Lines changed: 52 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -67,69 +67,6 @@ Onyx.connectWithoutView({
6767
},
6868
});
6969

70-
let allReports: OnyxCollection<Report>;
71-
Onyx.connectWithoutView({
72-
key: ONYXKEYS.COLLECTION.REPORT,
73-
waitForCollectionCallback: true,
74-
callback: (value) => {
75-
allReports = value;
76-
},
77-
});
78-
79-
let allPolicies: OnyxCollection<Policy>;
80-
Onyx.connectWithoutView({
81-
key: ONYXKEYS.COLLECTION.POLICY,
82-
waitForCollectionCallback: true,
83-
callback: (value) => {
84-
allPolicies = value;
85-
},
86-
});
87-
88-
let allPolicyTags: OnyxCollection<PolicyTagLists> = {};
89-
Onyx.connectWithoutView({
90-
key: ONYXKEYS.COLLECTION.POLICY_TAGS,
91-
waitForCollectionCallback: true,
92-
callback: (value) => {
93-
allPolicyTags = value ?? {};
94-
},
95-
});
96-
97-
let allPolicyCategories: OnyxCollection<PolicyCategories>;
98-
Onyx.connectWithoutView({
99-
key: ONYXKEYS.COLLECTION.POLICY_CATEGORIES,
100-
waitForCollectionCallback: true,
101-
callback: (value) => {
102-
allPolicyCategories = value;
103-
},
104-
});
105-
106-
let allPolicyRecentlyUsedCategories: OnyxCollection<RecentlyUsedCategories>;
107-
Onyx.connectWithoutView({
108-
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES,
109-
waitForCollectionCallback: true,
110-
callback: (value) => {
111-
allPolicyRecentlyUsedCategories = value;
112-
},
113-
});
114-
115-
let allPolicyRecentlyUsedTags: OnyxCollection<RecentlyUsedTags> = {};
116-
Onyx.connectWithoutView({
117-
key: ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
118-
waitForCollectionCallback: true,
119-
callback: (value) => {
120-
allPolicyRecentlyUsedTags = value ?? {};
121-
},
122-
});
123-
124-
let allNextSteps: OnyxCollection<ReportNextStepDeprecated>;
125-
Onyx.connectWithoutView({
126-
key: ONYXKEYS.COLLECTION.NEXT_STEP,
127-
waitForCollectionCallback: true,
128-
callback: (value) => {
129-
allNextSteps = value;
130-
},
131-
});
132-
13370
let currentUserAccountID: number = CONST.DEFAULT_NUMBER_ID;
13471
let currentUserEmail = '';
13572
Onyx.connectWithoutView({
@@ -176,28 +113,47 @@ type TransactionEditPermissionsParams = {
176113

177114
chatReportNVP?: OnyxEntry<ReportNameValuePairs>;
178115

116+
originalTransaction?: OnyxEntry<Transaction>;
117+
179118
/** When true, all editing is disabled regardless of permissions. */
180119
disabled?: boolean;
181120
};
182121

122+
type GetIouParamsInput = {
123+
transactionID: string;
124+
parentReport: OnyxEntry<Report>;
125+
transactionThreadReport: OnyxEntry<Report>;
126+
policy: OnyxEntry<Policy>;
127+
policyCategories: OnyxEntry<PolicyCategories>;
128+
policyTags: OnyxEntry<PolicyTagLists>;
129+
policyRecentlyUsedCategories: OnyxEntry<RecentlyUsedCategories>;
130+
policyRecentlyUsedTags: OnyxEntry<RecentlyUsedTags>;
131+
parentReportNextStep: OnyxEntry<ReportNextStepDeprecated>;
132+
};
133+
134+
type TransactionInlineEditParams = GetIouParamsInput & {
135+
hash: number | undefined;
136+
};
137+
183138
/**
184139
* @private
185-
* Builds all params needed for IOU action calls from module-level Onyx caches.
140+
* Builds all params needed for IOU action calls.
186141
* The returned object can be spread directly into any updateMoneyRequest* call
187142
* (all shared fields are at the top level); field-specific extras like
188143
* policyTagList, policyRecentlyUsedCategories, and transaction are also included.
189144
*/
190-
function getIouParamsForTransaction(transactionID: string, transactionThreadReportID: string | undefined) {
145+
function getIouParamsForTransaction({
146+
transactionID,
147+
parentReport,
148+
transactionThreadReport,
149+
policy,
150+
policyCategories,
151+
policyTags,
152+
policyRecentlyUsedCategories,
153+
policyRecentlyUsedTags,
154+
parentReportNextStep,
155+
}: GetIouParamsInput) {
191156
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
192-
const reportID = transaction?.reportID;
193-
const parentReport = reportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`] : undefined;
194-
const policyID = parentReport?.policyID;
195-
const policy = policyID ? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`] : undefined;
196-
const policyTagList = policyID ? allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] : undefined;
197-
const policyCategories = policyID ? allPolicyCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`] : undefined;
198-
const policyRecentlyUsedCategories = policyID ? allPolicyRecentlyUsedCategories?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_CATEGORIES}${policyID}`] : undefined;
199-
const parentReportNextStep = reportID ? allNextSteps?.[`${ONYXKEYS.COLLECTION.NEXT_STEP}${reportID}`] : undefined;
200-
const transactionThreadReport = transactionThreadReportID ? allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`] : undefined;
201157

202158
return {
203159
// Shared base fields — spread directly into any updateMoneyRequest* call
@@ -212,67 +168,67 @@ function getIouParamsForTransaction(transactionID: string, transactionThreadRepo
212168
isASAPSubmitBetaEnabled: Permissions.isBetaEnabled(CONST.BETAS.ASAP_SUBMIT, allBetas),
213169
// Field-specific extras
214170
transaction,
215-
policyTagList,
171+
policyTagList: policyTags,
216172
policyRecentlyUsedCategories,
217-
policyRecentlyUsedTags: policyID ? allPolicyRecentlyUsedTags?.[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`] : undefined,
173+
policyRecentlyUsedTags,
218174
};
219175
}
220176

221177
/** Updates the date of an expense from the Search results table or the Expense Report page. */
222-
function editTransactionDateInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newDate: string) {
223-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
178+
function editTransactionDateInline(params: TransactionInlineEditParams, newDate: string) {
179+
const iouParams = getIouParamsForTransaction(params);
224180
updateMoneyRequestDate({
225181
...iouParams,
226182
// updateMoneyRequestDate uses 'policyTags' (not policyTagList)
227183
policyTags: iouParams.policyTagList,
228184
value: newDate,
229185
transactions: allTransactions,
230186
transactionViolations: allTransactionViolations,
231-
hash,
187+
hash: params.hash,
232188
});
233189
}
234190

235191
/** Updates the merchant of an expense from the Search results table or the Expense Report page. */
236-
function editTransactionMerchantInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newMerchant: string) {
192+
function editTransactionMerchantInline(params: TransactionInlineEditParams, newMerchant: string) {
237193
// Merchant must be a non-empty string. An empty merchant is not a valid
238194
// state and the IOU action would save it as a blank row label.
239195
if (!newMerchant.trim()) {
240196
return;
241197
}
242-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
198+
const iouParams = getIouParamsForTransaction(params);
243199
updateMoneyRequestMerchant({
244200
...iouParams,
245201
value: newMerchant,
246-
hash,
202+
hash: params.hash,
247203
});
248204
}
249205

250206
/** Updates the description of an expense from the Search results table or the Expense Report page. */
251-
function editTransactionDescriptionInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newDescription: string) {
252-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
207+
function editTransactionDescriptionInline(params: TransactionInlineEditParams, newDescription: string) {
208+
const iouParams = getIouParamsForTransaction(params);
253209
updateMoneyRequestDescription({
254210
...iouParams,
255211
comment: newDescription,
256-
hash,
212+
hash: params.hash,
257213
});
258214
}
259215

260216
/** Updates the category of an expense from the Search results table or the Expense Report page. */
261-
function editTransactionCategoryInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newCategory: string) {
262-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
217+
function editTransactionCategoryInline(params: TransactionInlineEditParams, newCategory: string) {
218+
const iouParams = getIouParamsForTransaction(params);
263219
updateMoneyRequestCategory({
264220
...iouParams,
265221
category: newCategory,
266-
hash,
222+
hash: params.hash,
267223
});
268224
}
269225

270226
/** Updates the amount and currency of an expense from the Search results table or the Expense Report page. */
271-
function editTransactionAmountInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newAmount: number) {
227+
function editTransactionAmountInline(params: TransactionInlineEditParams, newAmount: number) {
272228
if (newAmount < 0) {
273229
return;
274230
}
275-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
231+
const iouParams = getIouParamsForTransaction(params);
276232
// Keep the existing currency — only the amount is changing from the search table
277233
const currency = iouParams.transaction?.modifiedCurrency ?? iouParams.transaction?.currency ?? CONST.CURRENCY.USD;
278234
// Recalculate tax from the existing tax code and the new amount
@@ -291,18 +247,18 @@ function editTransactionAmountInline(hash: number | undefined, transactionID: st
291247
transactions: allTransactions,
292248
transactionViolations: allTransactionViolations,
293249
policyRecentlyUsedCurrencies: [],
294-
hash,
250+
hash: params.hash,
295251
});
296252
}
297253

298254
/** Updates the tag of an expense from the Search results table or the Expense Report page. */
299-
function editTransactionTagInline(hash: number | undefined, transactionID: string, transactionThreadReportID: string | undefined, newTag: string) {
300-
const iouParams = getIouParamsForTransaction(transactionID, transactionThreadReportID);
255+
function editTransactionTagInline(params: TransactionInlineEditParams, newTag: string) {
256+
const iouParams = getIouParamsForTransaction(params);
301257
updateMoneyRequestTag({
302258
...iouParams,
303259
tag: newTag,
304260
policyRecentlyUsedTags: iouParams.policyRecentlyUsedTags,
305-
hash,
261+
hash: params.hash,
306262
});
307263
}
308264

@@ -324,6 +280,7 @@ function getTransactionEditPermissions({
324280
policyTags,
325281
transactionThreadNVP,
326282
chatReportNVP,
283+
originalTransaction,
327284
disabled,
328285
}: TransactionEditPermissionsParams): TransactionEditPermissions {
329286
if (disabled) {
@@ -365,8 +322,6 @@ function getTransactionEditPermissions({
365322
const canEditRestricted = (field: ValueOf<typeof CONST.EDIT_REQUEST_FIELD>) => {
366323
if (field === CONST.EDIT_REQUEST_FIELD.AMOUNT) {
367324
// Split expense children cannot have their amount edited inline
368-
const originalTransactionID = transaction?.comment?.originalTransactionID;
369-
const originalTransaction = originalTransactionID ? allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${originalTransactionID}`] : undefined;
370325
const {isExpenseSplit} = getOriginalTransactionWithSplitInfo(transaction, originalTransaction);
371326

372327
if (isExpenseSplit) {
@@ -411,4 +366,4 @@ export {
411366
getTransactionEditPermissions,
412367
};
413368

414-
export type {TransactionEditPermissions};
369+
export type {TransactionInlineEditParams, TransactionEditPermissions};

0 commit comments

Comments
 (0)