@@ -6,13 +6,13 @@ import SelectionScreen from '@components/SelectionScreen';
66import Text from '@components/Text' ;
77import useLocalize from '@hooks/useLocalize' ;
88import useThemeStyles from '@hooks/useThemeStyles' ;
9- import * as Xero from '@libs/actions/connections/Xero' ;
10- import * as ErrorUtils from '@libs/ErrorUtils' ;
9+ import { updateXeroMappings } from '@libs/actions/connections/Xero' ;
10+ import { clearXeroErrorField , enablePolicyReportFields } from '@libs/actions/Policy/Policy' ;
11+ import { getLatestErrorField } from '@libs/ErrorUtils' ;
1112import Navigation from '@libs/Navigation/Navigation' ;
1213import { isControlPolicy , settingsPendingAction } from '@libs/PolicyUtils' ;
1314import type { WithPolicyProps } from '@pages/workspace/withPolicy' ;
1415import withPolicyConnections from '@pages/workspace/withPolicyConnections' ;
15- import * as Policy from '@userActions/Policy/Policy' ;
1616import CONST from '@src/CONST' ;
1717import type { TranslationPaths } from '@src/languages/types' ;
1818import ROUTES from '@src/ROUTES' ;
@@ -29,13 +29,16 @@ function XeroMapTrackingCategoryConfigurationPage({policy}: WithPolicyProps) {
2929 const styles = useThemeStyles ( ) ;
3030 const categoryId = params ?. categoryId ?? '' ;
3131 const categoryName = decodeURIComponent ( params ?. categoryName ?? '' ) ;
32- const policyID = policy ?. id ?? '-1' ;
32+ const policyID = policy ?. id ;
3333 const { config} = policy ?. connections ?. xero ?? { } ;
3434 const { trackingCategories} = policy ?. connections ?. xero ?. data ?? { } ;
3535 const { mappings} = policy ?. connections ?. xero ?. config ?? { } ;
3636
3737 const currentTrackingCategory = trackingCategories ?. find ( ( category ) => category . id === categoryId ) ;
3838 const currentTrackingCategoryValue = currentTrackingCategory ? ( mappings ?. [ `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ currentTrackingCategory . id } ` ] ?? '' ) : '' ;
39+ const reportFieldTrackingCategories = Object . entries ( mappings ?? { } ) . filter (
40+ ( [ key , value ] ) => key . startsWith ( CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX ) && value === CONST . XERO_CONFIG . TRACKING_CATEGORY_OPTIONS . REPORT_FIELD ,
41+ ) ;
3942
4043 const optionsList = useMemo (
4144 ( ) =>
@@ -69,15 +72,24 @@ function XeroMapTrackingCategoryConfigurationPage({policy}: WithPolicyProps) {
6972 Navigation . navigate ( `${ backToRoute } &categoryId=${ categoryId } ` ) ;
7073 return ;
7174 }
72- Xero . updateXeroMappings (
75+ if ( ! policyID ) {
76+ return ;
77+ }
78+ updateXeroMappings (
7379 policyID ,
7480 categoryId ? { [ `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ] : option . value } : { } ,
7581 categoryId ? { [ `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ] : currentTrackingCategoryValue } : { } ,
7682 ) ;
83+ if ( ! reportFieldTrackingCategories . length && option . value === CONST . XERO_CONFIG . TRACKING_CATEGORY_OPTIONS . REPORT_FIELD ) {
84+ enablePolicyReportFields ( policyID , true ) ;
85+ }
86+ if ( reportFieldTrackingCategories . length === 1 && currentTrackingCategoryValue === CONST . XERO_CONFIG . TRACKING_CATEGORY_OPTIONS . REPORT_FIELD ) {
87+ enablePolicyReportFields ( policyID , false ) ;
88+ }
7789 }
7890 Navigation . goBack ( ROUTES . POLICY_ACCOUNTING_XERO_TRACKING_CATEGORIES . getRoute ( policyID ) ) ;
7991 } ,
80- [ categoryId , currentTrackingCategoryValue , policy , policyID ] ,
92+ [ categoryId , currentTrackingCategoryValue , reportFieldTrackingCategories , policy , policyID ] ,
8193 ) ;
8294
8395 return (
@@ -95,9 +107,9 @@ function XeroMapTrackingCategoryConfigurationPage({policy}: WithPolicyProps) {
95107 headerTitleAlreadyTranslated = { translate ( 'workspace.xero.mapTrackingCategoryTo' , { categoryName} ) }
96108 connectionName = { CONST . POLICY . CONNECTIONS . NAME . XERO }
97109 pendingAction = { settingsPendingAction ( [ `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ] , config ?. pendingFields ) }
98- errors = { ErrorUtils . getLatestErrorField ( config ?? { } , `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ) }
110+ errors = { getLatestErrorField ( config ?? { } , `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ) }
99111 errorRowStyles = { [ styles . ph5 , styles . pv3 ] }
100- onClose = { ( ) => Policy . clearXeroErrorField ( policyID , `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ) }
112+ onClose = { ( ) => clearXeroErrorField ( policyID , `${ CONST . XERO_CONFIG . TRACKING_CATEGORY_PREFIX } ${ categoryId } ` ) }
101113 shouldSingleExecuteRowSelect
102114 />
103115 ) ;
0 commit comments