11import { useNavigation } from '@react-navigation/native' ;
2- import React , { useCallback , useRef , useState } from 'react' ;
2+ import React , { useCallback , useEffect , useState } from 'react' ;
33import { View } from 'react-native' ;
44import type { ValueOf } from 'type-fest' ;
55import FormProvider from '@components/Form/FormProvider' ;
@@ -38,40 +38,47 @@ function SpendRuleMerchantEditPage({route}: SpendRuleMerchantEditPageProps) {
3838 const { translate} = useLocalize ( ) ;
3939 const styles = useThemeStyles ( ) ;
4040 const { inputCallbackRef} = useAutoFocusInput ( ) ;
41+ const [ spendRuleForm ] = useOnyx ( ONYXKEYS . FORMS . SPEND_RULE_FORM ) ;
4142 const [ spendRuleFormDraft ] = useOnyx ( ONYXKEYS . FORMS . SPEND_RULE_FORM_DRAFT ) ;
4243
43- const merchantNames = spendRuleFormDraft ?. merchantNames ?? [ ] ;
44- const merchantMatchTypes = spendRuleFormDraft ?. merchantMatchTypes ?? [ ] ;
44+ const merchantNames = spendRuleFormDraft ?. merchantNames ?? spendRuleForm ?. merchantNames ?? [ ] ;
45+ const merchantMatchTypes = spendRuleFormDraft ?. merchantMatchTypes ?? spendRuleForm ?. merchantMatchTypes ?? [ ] ;
4546 const isNew = merchantIndex === ROUTES . NEW ;
4647 const index = isNew ? - 1 : Number ( merchantIndex ) ;
4748 const existingMerchantName = isNew ? undefined : merchantNames . at ( index ) ;
4849 const existingMerchantMatchType = isNew ? undefined : merchantMatchTypes . at ( index ) ;
4950
5051 const [ merchantName , setMerchantName ] = useState ( existingMerchantName ?? '' ) ;
5152 const [ matchType , setMatchType ] = useState < ValueOf < typeof CONST . SEARCH . SYNTAX_OPERATORS > > ( existingMerchantMatchType ?? CONST . SEARCH . SYNTAX_OPERATORS . CONTAINS ) ;
52- const didSaveRef = useRef ( false ) ;
53+ const [ isSaved , setIsSaved ] = useState ( false ) ;
5354
5455 const goBack = useCallback ( ( ) => navigation . goBack ( ) , [ navigation ] ) ;
5556
57+ useEffect ( ( ) => {
58+ if ( ! isSaved ) {
59+ return ;
60+ }
61+ goBack ( ) ;
62+ } , [ isSaved , goBack ] ) ;
63+
5664 useDiscardChangesConfirmation ( {
5765 getHasUnsavedChanges : ( ) => {
58- if ( didSaveRef . current ) {
66+ if ( isSaved ) {
5967 return false ;
6068 }
6169 return merchantName !== ( existingMerchantName ?? '' ) || matchType !== ( existingMerchantMatchType ?? CONST . SEARCH . SYNTAX_OPERATORS . CONTAINS ) ;
6270 } ,
6371 } ) ;
6472
6573 const submit = ( ) => {
66- didSaveRef . current = true ;
6774 const trimmedMerchantName = merchantName . trim ( ) ;
6875 if ( ! trimmedMerchantName ) {
6976 if ( ! isNew ) {
7077 const updatedMerchantNames = merchantNames . filter ( ( _ , merchantArrayIndex ) => merchantArrayIndex !== index ) ;
7178 const updatedMerchantMatchTypes = merchantMatchTypes . filter ( ( _ , merchantArrayIndex ) => merchantArrayIndex !== index ) ;
7279 updateSpendRuleFormDraft ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
7380 }
74- goBack ( ) ;
81+ setIsSaved ( true ) ;
7582 return ;
7683 }
7784
@@ -83,7 +90,7 @@ function SpendRuleMerchantEditPage({route}: SpendRuleMerchantEditPageProps) {
8390 ? [ ...merchantMatchTypes , matchType ]
8491 : merchantMatchTypes . map ( ( type , merchantArrayIndex ) => ( merchantArrayIndex === index ? matchType : type ) ) ;
8592 updateSpendRuleFormDraft ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
86- goBack ( ) ;
93+ setIsSaved ( true ) ;
8794 } ;
8895
8996 const matchTypeItems : MatchTypeItem [ ] = [
0 commit comments