Skip to content

Commit 15556a2

Browse files
authored
Merge pull request Expensify#86948 from software-mansion-labs/jakubstec/remove-onyx-connect-policy-tags-in-sendInvoice-2
Remove `Onyx.connect()` usage for `ONYXKEYS.COLLECTION.POLICY_TAGS` from `src/libs/actions/IOU/SendInvoice.ts`
2 parents 988ae03 + 5b19047 commit 15556a2

4 files changed

Lines changed: 25 additions & 35 deletions

File tree

src/libs/actions/IOU/SendInvoice.ts

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import type {Receipt} from '@src/types/onyx/Transaction';
3535
import {isEmptyObject} from '@src/types/utils/EmptyObject';
3636
import {
3737
getAllPersonalDetails,
38-
getPolicyTags,
3938
getReceiptError,
4039
getSearchOnyxUpdate,
4140
handleNavigateAfterExpenseCreate,
@@ -86,6 +85,7 @@ type SendInvoiceOptions = {
8685
policyRecentlyUsedCategories?: OnyxEntry<OnyxTypes.RecentlyUsedCategories>;
8786
policyRecentlyUsedTags?: OnyxEntry<OnyxTypes.RecentlyUsedTags>;
8887
isFromGlobalCreate?: boolean;
88+
senderPolicyTags: OnyxEntry<OnyxTypes.PolicyTagLists>;
8989
};
9090

9191
type BuildOnyxDataForInvoiceParams = {
@@ -117,16 +117,6 @@ type BuildOnyxDataForInvoiceParams = {
117117
participant?: Participant;
118118
};
119119

120-
/**
121-
* @deprecated This function uses Onyx.connect and should be replaced with useOnyx for reactive data access.
122-
* TODO: remove `getPolicyTagsData` from this file https://github.com/Expensify/App/issues/80048
123-
* All usages of this function should be replaced with useOnyx hook in React components.
124-
*/
125-
function getPolicyTagsData(policyID: string | undefined) {
126-
const allPolicyTags = getPolicyTags();
127-
return allPolicyTags?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`] ?? {};
128-
}
129-
130120
/** Builds the Onyx data for an invoice */
131121
function buildOnyxDataForInvoice(
132122
invoiceParams: BuildOnyxDataForInvoiceParams,
@@ -599,7 +589,7 @@ function getSendInvoiceInformation({
599589
policyRecentlyUsedCategories,
600590
policyRecentlyUsedTags,
601591
senderPolicyTags,
602-
}: SendInvoiceOptions & {senderPolicyTags: OnyxTypes.PolicyTagLists}): SendInvoiceInformation {
592+
}: SendInvoiceOptions): SendInvoiceInformation {
603593
const {amount = 0, currency = '', created = '', merchant = '', category = '', tag = '', taxCode = '', taxAmount = 0, taxValue, billable, comment, participants} = transaction ?? {};
604594
const trimmedComment = (comment?.comment ?? '').trim();
605595
const senderWorkspaceID = participants?.find((participant) => participant?.isSender)?.policyID;
@@ -656,7 +646,7 @@ function getSendInvoiceInformation({
656646

657647
const optimisticPolicyRecentlyUsedCategories = mergePolicyRecentlyUsedCategories(category, policyRecentlyUsedCategories);
658648
const optimisticPolicyRecentlyUsedTags = buildOptimisticPolicyRecentlyUsedTags({
659-
policyTags: senderPolicyTags,
649+
policyTags: senderPolicyTags ?? {},
660650
policyRecentlyUsedTags,
661651
transactionTags: tag,
662652
});
@@ -744,11 +734,8 @@ function sendInvoice({
744734
policyRecentlyUsedCategories,
745735
policyRecentlyUsedTags,
746736
isFromGlobalCreate,
737+
senderPolicyTags,
747738
}: SendInvoiceOptions) {
748-
// TODO: remove `allPolicyTags` from this file https://github.com/Expensify/App/issues/80048
749-
// eslint-disable-next-line @typescript-eslint/no-deprecated
750-
const senderPolicyTags = getPolicyTagsData(transaction?.participants?.find((p) => p?.isSender)?.policyID) ?? {};
751-
752739
const parsedComment = getParsedComment(transaction?.comment?.comment?.trim() ?? '');
753740
if (transaction?.comment) {
754741
// eslint-disable-next-line no-param-reassign
@@ -781,7 +768,7 @@ function sendInvoice({
781768
companyWebsite,
782769
policyRecentlyUsedCategories,
783770
policyRecentlyUsedTags,
784-
senderPolicyTags,
771+
senderPolicyTags: senderPolicyTags ?? {},
785772
});
786773

787774
const parameters: SendInvoiceParams = {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function IOURequestStepCompanyInfo({route, report, transaction}: IOURequestStepC
103103
policyRecentlyUsedCategories,
104104
policyRecentlyUsedTags,
105105
isFromGlobalCreate: transaction?.isFromFloatingActionButton ?? transaction?.isFromGlobalCreate,
106+
senderPolicyTags: policyTags ?? {},
106107
});
107108
};
108109

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ function IOURequestStepConfirmation({
274274
const receiverAccountID = receiverParticipant && 'accountID' in receiverParticipant && receiverParticipant.accountID ? receiverParticipant.accountID : CONST.DEFAULT_NUMBER_ID;
275275
const receiverType = getReceiverType(receiverParticipant);
276276
const senderWorkspaceID = transaction?.participants?.find((participant) => participant?.isSender)?.policyID;
277+
const [senderWorkspacePolicyTags] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${senderWorkspaceID}`);
277278

278279
const existingInvoiceReport = useParticipantsInvoiceReport(receiverAccountID, receiverType, senderWorkspaceID);
279280

@@ -1348,6 +1349,7 @@ function IOURequestStepConfirmation({
13481349
policyRecentlyUsedCategories,
13491350
isFromGlobalCreate: transaction?.isFromFloatingActionButton ?? transaction?.isFromGlobalCreate,
13501351
policyRecentlyUsedTags,
1352+
senderPolicyTags: senderWorkspacePolicyTags ?? {},
13511353
});
13521354
markSubmitExpenseEnd();
13531355
return;
@@ -1444,6 +1446,7 @@ function IOURequestStepConfirmation({
14441446
existingInvoiceReport,
14451447
policy,
14461448
policyTags,
1449+
senderWorkspacePolicyTags,
14471450
policyCategories,
14481451
trackExpense,
14491452
userLocation,

tests/actions/IOUTest/SendInvoiceTest.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ describe('actions/SendInvoice', () => {
614614
policy,
615615
companyName,
616616
companyWebsite,
617+
senderPolicyTags: undefined,
617618
});
618619

619620
// Then a new invoice chat is created instead of incorrectly using the invoice chat which has been converted from individual to business
@@ -641,6 +642,7 @@ describe('actions/SendInvoice', () => {
641642
currentUserAccountID: 1,
642643
transaction,
643644
policyRecentlyUsedCurrencies: initialCurrencies,
645+
senderPolicyTags: undefined,
644646
});
645647

646648
mockFetch?.fail?.();
@@ -680,6 +682,7 @@ describe('actions/SendInvoice', () => {
680682
transaction,
681683
policyRecentlyUsedCurrencies: [],
682684
policyRecentlyUsedCategories,
685+
senderPolicyTags: undefined,
683686
});
684687

685688
// Then onyxData should be passed to API.write
@@ -707,32 +710,27 @@ describe('actions/SendInvoice', () => {
707710
const policyRecentlyUsedTags: OnyxEntry<RecentlyUsedTags> = {
708711
[tagName]: ['old tag'],
709712
};
710-
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`, {
711-
[tagName]: {name: tagName},
712-
});
713-
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`, policyRecentlyUsedTags);
714-
715713
// When sending an invoice
716714
sendInvoice({
717715
currentUserAccountID: 1,
718716
transaction,
719717
policyRecentlyUsedCurrencies: [],
720718
policyRecentlyUsedTags,
719+
senderPolicyTags: {
720+
[tagName]: {
721+
name: tagName,
722+
required: false,
723+
tags: {},
724+
orderWeight: 0,
725+
},
726+
},
721727
});
722-
waitForBatchedUpdates();
728+
await waitForBatchedUpdates();
723729

724730
// Then the transaction tag should be added to the recently used tags collection
725-
const newPolicyRecentlyUsedTags: RecentlyUsedTags = await new Promise((resolve) => {
726-
const connection = Onyx.connectWithoutView({
727-
key: `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`,
728-
callback: (recentlyUsedTags) => {
729-
resolve(recentlyUsedTags ?? {});
730-
Onyx.disconnect(connection);
731-
},
732-
});
733-
});
734-
expect(newPolicyRecentlyUsedTags[tagName].length).toBe(2);
735-
expect(newPolicyRecentlyUsedTags[tagName].at(0)).toBe(transactionTag);
731+
const newPolicyRecentlyUsedTags = await getOnyxValue(`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}`);
732+
expect(newPolicyRecentlyUsedTags?.[tagName]?.length).toBe(2);
733+
expect(newPolicyRecentlyUsedTags?.[tagName]?.at(0)).toBe(transactionTag);
736734
});
737735

738736
it('should use invoiceChatReportID when creating new invoice chat via sendInvoice', () => {
@@ -760,6 +758,7 @@ describe('actions/SendInvoice', () => {
760758
transaction,
761759
policyRecentlyUsedCurrencies: [],
762760
invoiceChatReportID: preGeneratedReportID,
761+
senderPolicyTags: undefined,
763762
});
764763

765764
expect(writeSpy).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)