@@ -19,7 +19,7 @@ import useTheme from '@hooks/useTheme';
1919import useThemeStyles from '@hooks/useThemeStyles' ;
2020import { getSpendRuleFormValuesFromCardRule } from '@libs/actions/Card' ;
2121import { openPolicyExpensifyCardsPage } from '@libs/actions/Policy/Policy' ;
22- import { filterInactiveCards , getCardDescriptionForSearchTable , getSelectedCardsSharedCurrency , isCard } from '@libs/CardUtils' ;
22+ import { filterInactiveCards , getCardDescriptionForSearchTable , getSelectedCardsSharedCurrency } from '@libs/CardUtils' ;
2323import { convertToBackendAmount , convertToDisplayString } from '@libs/CurrencyUtils' ;
2424import Navigation from '@libs/Navigation/Navigation' ;
2525import { getDisplayNameOrDefault } from '@libs/PersonalDetailsUtils' ;
@@ -28,6 +28,7 @@ import CONST from '@src/CONST';
2828import ONYXKEYS from '@src/ONYXKEYS' ;
2929import ROUTES from '@src/ROUTES' ;
3030import type { SpendRuleForm } from '@src/types/form' ;
31+ import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue' ;
3132import { getTruncatedSpendRuleSummary } from './SpendRulesUtils' ;
3233
3334type SpendRulesSectionProps = {
@@ -86,14 +87,14 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
8687 const { isOffline} = useNetwork ( ) ;
8788 const defaultFundID = useDefaultFundID ( policyID ) ;
8889 const [ expensifyCardSettings ] = useOnyx ( `${ ONYXKEYS . COLLECTION . PRIVATE_EXPENSIFY_CARD_SETTINGS } ${ defaultFundID } ` ) ;
89- const [ cardsList ] = useOnyx ( `${ ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST } ${ defaultFundID } _${ CONST . EXPENSIFY_CARD . BANK } ` , { selector : filterInactiveCards } ) ;
90+ const [ cardsList , cardsListResult ] = useOnyx ( `${ ONYXKEYS . COLLECTION . WORKSPACE_CARDS_LIST } ${ defaultFundID } _${ CONST . EXPENSIFY_CARD . BANK } ` , { selector : filterInactiveCards } ) ;
9091 const [ personalDetails ] = useOnyx ( ONYXKEYS . PERSONAL_DETAILS_LIST ) ;
9192
9293 useEffect ( ( ) => {
9394 openPolicyExpensifyCardsPage ( policyID , defaultFundID ) ;
9495 } , [ policyID , defaultFundID ] ) ;
9596
96- const isCardSettingsLoading = ! isOffline && ( ! expensifyCardSettings || expensifyCardSettings . isLoading ) && ! expensifyCardSettings ?. hasOnceLoaded ;
97+ const isSpendRulesListLoading = ! isOffline && ( isLoadingOnyxValue ( cardsListResult ) || ! expensifyCardSettings || expensifyCardSettings . isLoading ) && ! expensifyCardSettings ?. hasOnceLoaded ;
9798
9899 const showBuiltInProtectionModal = ( ) => {
99100 showConfirmModal ( {
@@ -123,11 +124,15 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
123124 return undefined ;
124125 }
125126 const actionLabel = formValues . restrictionAction === CONST . SPEND_RULES . ACTION . BLOCK ? blockLabel : allowLabel ;
126- const selectedCurrency = getSelectedCardsSharedCurrency ( formValues . cardIDs , cardsList ) ;
127+ const activeCardIDs = formValues . cardIDs . filter ( ( cardID ) => ! ! cardsList ?. [ cardID ] ) ;
128+ if ( activeCardIDs . length === 0 ) {
129+ return undefined ;
130+ }
131+ const selectedCurrency = getSelectedCardsSharedCurrency ( activeCardIDs , cardsList ) ;
127132 const cardSummary = getTruncatedSpendRuleSummary (
128- formValues . cardIDs . map ( ( cardID ) => {
133+ activeCardIDs . map ( ( cardID ) => {
129134 const card = cardsList ?. [ cardID ] ;
130- if ( ! card || ! isCard ( card ) ) {
135+ if ( ! card ) {
131136 return cardID ;
132137 }
133138
@@ -210,14 +215,14 @@ function SpendRulesSection({policyID}: SpendRulesSectionProps) {
210215 onPress = { showBuiltInProtectionModal }
211216 shouldShowRightIcon
212217 />
213- { isCardSettingsLoading ? (
218+ { isSpendRulesListLoading ? (
214219 < View style = { [ styles . justifyContentCenter , styles . alignItemsCenter , styles . mt5 , styles . mb3 ] } >
215220 < ActivityIndicator
216221 size = { CONST . ACTIVITY_INDICATOR_SIZE . LARGE }
217222 reasonAttributes = { {
218223 context : 'SpendRulesSection' ,
219224 isOffline,
220- hasOnceLoaded : ! ! expensifyCardSettings ?. hasOnceLoaded ,
225+ hasOnceLoaded : ! isSpendRulesListLoading ,
221226 } }
222227 />
223228 </ View >
0 commit comments