From e011efb5a171590c5a7d775505cf49757149f609 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Fri, 29 Sep 2023 14:47:45 +0100 Subject: [PATCH 1/3] fix: allow creation of policyRecentlyUsedTags for policy if none exists --- src/libs/actions/IOU.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 198ceb2b8172..898679ee4e2e 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -65,6 +65,20 @@ Onyx.connect({ }, }); +let allPolicyTags = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.POLICY_TAGS, + waitForCollectionCallback: true, + callback: (value) => { + if (!value) { + allPolicyTags = {}; + return; + } + + allPolicyTags = value; + }, +}); + let userAccountID = ''; let currentUserEmail = ''; Onyx.connect({ @@ -490,14 +504,13 @@ function getMoneyRequestInformation( const optimisticPolicyRecentlyUsedCategories = [category, ...uniquePolicyRecentlyUsedCategories]; const optimisticPolicyRecentlyUsedTags = {}; + const policyTags = allPolicyTags[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${iouReport.policyID}`]; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; - if (recentlyUsedPolicyTags) { - // For now it only uses the first tag of the policy, since multi-tags are not yet supported - const recentlyUsedTagListKey = _.first(_.keys(recentlyUsedPolicyTags)); - const uniquePolicyRecentlyUsedTags = _.filter(recentlyUsedPolicyTags[recentlyUsedTagListKey], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== tag); - optimisticPolicyRecentlyUsedTags[recentlyUsedTagListKey] = [tag, ...uniquePolicyRecentlyUsedTags]; - } + // For now it only uses the first tag of the policy, since multi-tags are not yet supported + const tagListKey = _.first(_.keys(policyTags)); + const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags ? _.filter(recentlyUsedPolicyTags[tagListKey], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== tag) : []; + optimisticPolicyRecentlyUsedTags[tagListKey] = [tag, ...uniquePolicyRecentlyUsedTags]; // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction @@ -1308,10 +1321,10 @@ function editMoneyRequest(transactionID, transactionThreadReportID, transactionC const tagListName = transactionChanges.tagListName; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; - if (recentlyUsedPolicyTags) { - const uniquePolicyRecentlyUsedTags = _.filter(recentlyUsedPolicyTags[tagListName], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== transactionChanges.tag); - optimisticPolicyRecentlyUsedTags[tagListName] = [transactionChanges.tag, ...uniquePolicyRecentlyUsedTags]; - } + const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags + ? _.filter(recentlyUsedPolicyTags[tagListName], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== transactionChanges.tag) + : []; + optimisticPolicyRecentlyUsedTags[tagListName] = [transactionChanges.tag, ...uniquePolicyRecentlyUsedTags]; } // STEP 4: Compose the optimistic data From 7e1956f5c759186a270869645ef955db6d66d957 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Fri, 29 Sep 2023 15:33:00 +0100 Subject: [PATCH 2/3] fix: move check --- src/libs/actions/IOU.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 898679ee4e2e..28d68f68261b 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -69,14 +69,7 @@ let allPolicyTags = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY_TAGS, waitForCollectionCallback: true, - callback: (value) => { - if (!value) { - allPolicyTags = {}; - return; - } - - allPolicyTags = value; - }, + callback: (value) => (allPolicyTags = value), }); let userAccountID = ''; @@ -507,10 +500,12 @@ function getMoneyRequestInformation( const policyTags = allPolicyTags[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${iouReport.policyID}`]; const recentlyUsedPolicyTags = allRecentlyUsedTags[`${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${iouReport.policyID}`]; - // For now it only uses the first tag of the policy, since multi-tags are not yet supported - const tagListKey = _.first(_.keys(policyTags)); - const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags ? _.filter(recentlyUsedPolicyTags[tagListKey], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== tag) : []; - optimisticPolicyRecentlyUsedTags[tagListKey] = [tag, ...uniquePolicyRecentlyUsedTags]; + if (policyTags) { + // For now it only uses the first tag of the policy, since multi-tags are not yet supported + const tagListKey = _.first(_.keys(policyTags)); + const uniquePolicyRecentlyUsedTags = recentlyUsedPolicyTags ? _.filter(recentlyUsedPolicyTags[tagListKey], (recentlyUsedPolicyTag) => recentlyUsedPolicyTag !== tag) : []; + optimisticPolicyRecentlyUsedTags[tagListKey] = [tag, ...uniquePolicyRecentlyUsedTags]; + } // If there is an existing transaction (which is the case for distance requests), then the data from the existing transaction // needs to be manually merged into the optimistic transaction. This is because buildOnyxDataForMoneyRequest() uses `Onyx.set()` for the transaction From c9ff033d28e6e78e372cf2fdc425598cb044a267 Mon Sep 17 00:00:00 2001 From: Ana Margarida Silva Date: Fri, 29 Sep 2023 15:39:26 +0100 Subject: [PATCH 3/3] revert: check removal --- src/libs/actions/IOU.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 28d68f68261b..ff0b412a7096 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -69,7 +69,14 @@ let allPolicyTags = {}; Onyx.connect({ key: ONYXKEYS.COLLECTION.POLICY_TAGS, waitForCollectionCallback: true, - callback: (value) => (allPolicyTags = value), + callback: (value) => { + if (!value) { + allPolicyTags = {}; + return; + } + + allPolicyTags = value; + }, }); let userAccountID = '';