11import { useNavigation } from '@react-navigation/native' ;
2- import React , { useCallback , 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' ;
@@ -12,10 +12,11 @@ import type {ListItem} from '@components/SelectionList/ListItem/types';
1212import Text from '@components/Text' ;
1313import TextInput from '@components/TextInput' ;
1414import useAutoFocusInput from '@hooks/useAutoFocusInput' ;
15+ import useDiscardChangesConfirmation from '@hooks/useDiscardChangesConfirmation' ;
1516import useLocalize from '@hooks/useLocalize' ;
1617import useOnyx from '@hooks/useOnyx' ;
1718import useThemeStyles from '@hooks/useThemeStyles' ;
18- import { updateDraftSpendRule } from '@libs/actions/User' ;
19+ import { updateSpendRuleFormDraft } from '@libs/actions/User' ;
1920import type { PlatformStackScreenProps } from '@libs/Navigation/PlatformStackNavigation/types' ;
2021import type { SettingsNavigatorParamList } from '@libs/Navigation/types' ;
2122import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper' ;
@@ -38,28 +39,46 @@ function SpendRuleMerchantEditPage({route}: SpendRuleMerchantEditPageProps) {
3839 const styles = useThemeStyles ( ) ;
3940 const { inputCallbackRef} = useAutoFocusInput ( ) ;
4041 const [ spendRuleForm ] = useOnyx ( ONYXKEYS . FORMS . SPEND_RULE_FORM ) ;
42+ const [ spendRuleFormDraft ] = useOnyx ( ONYXKEYS . FORMS . SPEND_RULE_FORM_DRAFT ) ;
4143
42- const merchantNames = spendRuleForm ?. merchantNames ?? [ ] ;
43- const merchantMatchTypes = spendRuleForm ?. merchantMatchTypes ?? [ ] ;
44+ const merchantNames = spendRuleFormDraft ?. merchantNames ?? spendRuleForm ?. merchantNames ?? [ ] ;
45+ const merchantMatchTypes = spendRuleFormDraft ?. merchantMatchTypes ?? spendRuleForm ?. merchantMatchTypes ?? [ ] ;
4446 const isNew = merchantIndex === ROUTES . NEW ;
4547 const index = isNew ? - 1 : Number ( merchantIndex ) ;
4648 const existingMerchantName = isNew ? undefined : merchantNames . at ( index ) ;
4749 const existingMerchantMatchType = isNew ? undefined : merchantMatchTypes . at ( index ) ;
4850
4951 const [ merchantName , setMerchantName ] = useState ( existingMerchantName ?? '' ) ;
5052 const [ matchType , setMatchType ] = useState < ValueOf < typeof CONST . SEARCH . SYNTAX_OPERATORS > > ( existingMerchantMatchType ?? CONST . SEARCH . SYNTAX_OPERATORS . CONTAINS ) ;
53+ const [ isSaved , setIsSaved ] = useState ( false ) ;
5154
5255 const goBack = useCallback ( ( ) => navigation . goBack ( ) , [ navigation ] ) ;
5356
57+ useEffect ( ( ) => {
58+ if ( ! isSaved ) {
59+ return ;
60+ }
61+ goBack ( ) ;
62+ } , [ isSaved , goBack ] ) ;
63+
64+ useDiscardChangesConfirmation ( {
65+ getHasUnsavedChanges : ( ) => {
66+ if ( isSaved ) {
67+ return false ;
68+ }
69+ return merchantName !== ( existingMerchantName ?? '' ) || matchType !== ( existingMerchantMatchType ?? CONST . SEARCH . SYNTAX_OPERATORS . CONTAINS ) ;
70+ } ,
71+ } ) ;
72+
5473 const submit = ( ) => {
5574 const trimmedMerchantName = merchantName . trim ( ) ;
5675 if ( ! trimmedMerchantName ) {
5776 if ( ! isNew ) {
5877 const updatedMerchantNames = merchantNames . filter ( ( _ , merchantArrayIndex ) => merchantArrayIndex !== index ) ;
5978 const updatedMerchantMatchTypes = merchantMatchTypes . filter ( ( _ , merchantArrayIndex ) => merchantArrayIndex !== index ) ;
60- updateDraftSpendRule ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
79+ updateSpendRuleFormDraft ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
6180 }
62- goBack ( ) ;
81+ setIsSaved ( true ) ;
6382 return ;
6483 }
6584
@@ -70,8 +89,8 @@ function SpendRuleMerchantEditPage({route}: SpendRuleMerchantEditPageProps) {
7089 const updatedMerchantMatchTypes = isNew
7190 ? [ ...merchantMatchTypes , matchType ]
7291 : merchantMatchTypes . map ( ( type , merchantArrayIndex ) => ( merchantArrayIndex === index ? matchType : type ) ) ;
73- updateDraftSpendRule ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
74- goBack ( ) ;
92+ updateSpendRuleFormDraft ( { merchantNames : updatedMerchantNames , merchantMatchTypes : updatedMerchantMatchTypes } ) ;
93+ setIsSaved ( true ) ;
7594 } ;
7695
7796 const matchTypeItems : MatchTypeItem [ ] = [
0 commit comments