Skip to content

Commit

Permalink
Merge pull request #28493 from BeeMargarida/fix/26793-recently_used_t…
Browse files Browse the repository at this point in the history
…ags_offline

fix: allow creation of policyRecentlyUsedTags for policy if none exists
  • Loading branch information
amyevans authored Oct 3, 2023
2 parents cccc00b + c9ff033 commit 82fe5a6
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -472,13 +486,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
Expand Down Expand Up @@ -1290,10 +1305,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
Expand Down

0 comments on commit 82fe5a6

Please sign in to comment.