|
1 | | -import React, {useMemo} from 'react'; |
2 | | -import type {ValueOf} from 'type-fest'; |
| 1 | +import React from 'react'; |
| 2 | +import type {OnyxCollection} from 'react-native-onyx'; |
3 | 3 | import RuleSelectionBase from '@components/Rule/RuleSelectionBase'; |
4 | 4 | import useOnyx from '@hooks/useOnyx'; |
5 | 5 | import {updateDraftRule} from '@libs/actions/User'; |
6 | 6 | import Navigation from '@libs/Navigation/Navigation'; |
7 | 7 | import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types'; |
8 | 8 | import type {SettingsNavigatorParamList} from '@libs/Navigation/types'; |
9 | | -import {getCleanedTagName, getTagLists} from '@libs/PolicyUtils'; |
10 | | -import {trimTag} from '@libs/TagUtils'; |
11 | | -import {getTagArrayFromName} from '@libs/TransactionUtils'; |
| 9 | +import {getCleanedTagName, getTagNamesFromTagsLists} from '@libs/PolicyUtils'; |
12 | 10 | import ONYXKEYS from '@src/ONYXKEYS'; |
13 | 11 | import ROUTES from '@src/ROUTES'; |
14 | 12 | import type SCREENS from '@src/SCREENS'; |
15 | 13 | import type {PolicyTagLists} from '@src/types/onyx'; |
16 | | -import getEmptyArray from '@src/types/utils/getEmptyArray'; |
| 14 | +import {getEmptyObject} from '@src/types/utils/EmptyObject'; |
17 | 15 |
|
18 | 16 | type AddTagPageProps = PlatformStackScreenProps<SettingsNavigatorParamList, typeof SCREENS.SETTINGS.RULES.EDIT_TAG>; |
19 | 17 |
|
20 | 18 | function AddTagPage({route}: AddTagPageProps) { |
21 | | - const {hash, index: orderWeight} = route.params ?? {}; |
22 | | - |
23 | 19 | const [form] = useOnyx(ONYXKEYS.FORMS.EXPENSE_RULE_FORM, {canBeMissing: true}); |
24 | | - const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); |
25 | | - const [policyTags = getEmptyArray<ValueOf<PolicyTagLists>>()] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${activePolicyID}`, {canBeMissing: true, selector: getTagLists}); |
26 | | - const hasDependentTags = policyTags.some((tagList) => Object.values(tagList.tags).some((tag) => !!tag.rules?.parentTagsFilter || !!tag.parentTagsFilter)); |
27 | | - const tagList = policyTags.find((item) => item.orderWeight === orderWeight); |
28 | | - const formTags = getTagArrayFromName(form?.tag ?? ''); |
29 | | - const formTag = formTags.at(orderWeight); |
| 20 | + const [allPolicyTagLists = getEmptyObject<NonNullable<OnyxCollection<PolicyTagLists>>>()] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS, { |
| 21 | + canBeMissing: true, |
| 22 | + }); |
30 | 23 |
|
31 | | - const tagItems = useMemo(() => { |
32 | | - const tags: Array<{name: string; value: string}> = []; |
| 24 | + const selectedTagItem = form?.tag ? {name: getCleanedTagName(form.tag), value: form.tag} : undefined; |
33 | 25 |
|
34 | | - for (const tag of Object.values(tagList?.tags ?? {})) { |
35 | | - if (tag.name !== formTag && !tag.enabled) { |
36 | | - continue; |
37 | | - } |
38 | | - tags.push({name: getCleanedTagName(tag.name), value: tag.name}); |
39 | | - } |
| 26 | + const tagItems = () => { |
| 27 | + const uniqueTagNames = new Set<string>(); |
40 | 28 |
|
41 | | - return tags; |
42 | | - }, [tagList?.tags, formTag]); |
| 29 | + const tagListsUnpacked = Object.values(allPolicyTagLists ?? {}).filter((item) => !!item); |
| 30 | + for (const tag of tagListsUnpacked.map(getTagNamesFromTagsLists).flat()) { |
| 31 | + uniqueTagNames.add(tag); |
| 32 | + } |
43 | 33 |
|
44 | | - const selectedTagItem = tagItems.find(({value}) => value === formTag); |
| 34 | + return Array.from(uniqueTagNames).map((tagName) => ({name: getCleanedTagName(tagName), value: tagName})); |
| 35 | + }; |
45 | 36 |
|
| 37 | + const hash = route.params?.hash; |
46 | 38 | const backToRoute = hash ? ROUTES.SETTINGS_RULES_EDIT.getRoute(hash) : ROUTES.SETTINGS_RULES_ADD.getRoute(); |
47 | 39 |
|
48 | 40 | const onSave = (value?: string) => { |
49 | | - const newTags = [...formTags]; |
50 | | - if (hasDependentTags) { |
51 | | - newTags.splice(orderWeight, newTags.length - orderWeight, value ?? ''); |
52 | | - } else { |
53 | | - newTags[orderWeight] = value ?? ''; |
54 | | - } |
55 | | - updateDraftRule({tag: trimTag(newTags.join(':'))}); |
| 41 | + updateDraftRule({tag: value}); |
56 | 42 | }; |
57 | 43 |
|
58 | 44 | return ( |
59 | 45 | <RuleSelectionBase |
60 | 46 | titleKey="common.tag" |
61 | | - title={tagList?.name} |
62 | 47 | testID="AddTagPage" |
63 | 48 | selectedItem={selectedTagItem} |
64 | | - items={tagItems} |
| 49 | + items={tagItems()} |
65 | 50 | onSave={onSave} |
66 | 51 | onBack={() => Navigation.goBack(backToRoute)} |
67 | 52 | backToRoute={backToRoute} |
|
0 commit comments