From e0d34b803aed6316fb9804a51ad5caf24f415cbf Mon Sep 17 00:00:00 2001 From: Fitsum Abebe Date: Fri, 1 Dec 2023 15:56:15 +0300 Subject: [PATCH] fixed duplicate search result of the selected parent:child tag --- src/libs/OptionsListUtils.js | 10 ++-------- src/libs/PolicyUtils.ts | 18 ++++++++++++++---- src/types/onyx/PolicyTag.ts | 6 +++++- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/libs/OptionsListUtils.js b/src/libs/OptionsListUtils.js index e3b6ec77380e..e1c06abd9d24 100644 --- a/src/libs/OptionsListUtils.js +++ b/src/libs/OptionsListUtils.js @@ -924,7 +924,7 @@ function getTagsOptions(tags) { /** * Build the section list for tags * - * @param {Object[]} rawTags + * @param {Object[]} tags * @param {String} tags[].name * @param {Boolean} tags[].enabled * @param {String[]} recentlyUsedTags @@ -934,14 +934,8 @@ function getTagsOptions(tags) { * @param {Number} maxRecentReportsToShow * @returns {Array} */ -function getTagListSections(rawTags, recentlyUsedTags, selectedOptions, searchInputValue, maxRecentReportsToShow) { +function getTagListSections(tags, recentlyUsedTags, selectedOptions, searchInputValue, maxRecentReportsToShow) { const tagSections = []; - const tags = _.map(rawTags, (tag) => { - // This is to remove unnecessary escaping backslash in tag name sent from backend. - const tagName = tag.name && tag.name.replace(/\\{1,2}:/g, ':'); - - return {...tag, name: tagName}; - }); const sortedTags = sortTags(tags); const enabledTags = _.filter(sortedTags, (tag) => tag.enabled); const numberOfTags = _.size(enabledTags); diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 19129959d016..ff7fc2ee3e82 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -1,8 +1,9 @@ import Str from 'expensify-common/lib/str'; +import {cloneDeep} from 'lodash'; import {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import {PersonalDetails, Policy, PolicyMembers, PolicyTag, PolicyTags} from '@src/types/onyx'; +import {PersonalDetails, Policy, PolicyMembers, PolicyTags} from '@src/types/onyx'; import {EmptyObject, isEmptyObject} from '@src/types/utils/EmptyObject'; type MemberEmailsToAccountIDs = Record; @@ -158,7 +159,7 @@ function getIneligibleInvitees(policyMembers: OnyxEntry, personal /** * Gets the tag from policy tags, defaults to the first if no key is provided. */ -function getTag(policyTags: OnyxEntry, tagKey?: keyof typeof policyTags): PolicyTag | undefined | EmptyObject { +function getTag(policyTags: OnyxCollection, tagKey?: keyof typeof policyTags): PolicyTags | undefined | EmptyObject { if (isEmptyObject(policyTags)) { return {}; } @@ -171,7 +172,7 @@ function getTag(policyTags: OnyxEntry, tagKey?: keyof typeof policyT /** * Gets the first tag name from policy tags. */ -function getTagListName(policyTags: OnyxEntry) { +function getTagListName(policyTags: OnyxCollection) { if (Object.keys(policyTags ?? {})?.length === 0) { return ''; } @@ -190,8 +191,17 @@ function getTagList(policyTags: OnyxCollection, tagKey: string) { } const policyTagKey = tagKey ?? Object.keys(policyTags ?? {})[0]; + const tags = cloneDeep(policyTags?.[policyTagKey]?.tags ?? {}); - return policyTags?.[policyTagKey]?.tags ?? {}; + // This is to remove unnecessary escaping backslash in tag name sent from backend for "Parent: Child" type of tags. + Object.keys(tags).forEach((key) => { + const cleanedTagName = tags[key]?.name?.replace(/\\{1,2}:/g, ':'); + if (cleanedTagName) { + tags[key].name = cleanedTagName; + } + }); + + return tags; } function isPendingDeletePolicy(policy: OnyxEntry): boolean { diff --git a/src/types/onyx/PolicyTag.ts b/src/types/onyx/PolicyTag.ts index 7807dcc00433..190f388265a1 100644 --- a/src/types/onyx/PolicyTag.ts +++ b/src/types/onyx/PolicyTag.ts @@ -10,7 +10,11 @@ type PolicyTag = { 'GL Code': string; }; -type PolicyTags = Record; +type PolicyTags = { + name: string; + required: boolean; + tags: Record; +}; export default PolicyTag; export type {PolicyTags};