diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index 198ceb2b8172..ff0b412a7096 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,13 +504,14 @@ 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) { + if (policyTags) { // 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]; + 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 @@ -1308,10 +1323,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