Skip to content

Commit 7c73c1e

Browse files
committed
rm ref, rm init function
1 parent e4a4149 commit 7c73c1e

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

src/pages/workspace/rules/SpendRules/SpendRuleMerchantEditPage.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useNavigation} from '@react-navigation/native';
2-
import React, {useCallback, useRef, useState} from 'react';
2+
import React, {useCallback, useEffect, useState} from 'react';
33
import {View} from 'react-native';
44
import type {ValueOf} from 'type-fest';
55
import 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[] = [

src/pages/workspace/rules/SpendRules/SpendRuleMerchantsPage.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {useMemoizedLazyExpensifyIcons, useMemoizedLazyIllustrations} from '@hook
1212
import useLocalize from '@hooks/useLocalize';
1313
import useOnyx from '@hooks/useOnyx';
1414
import useThemeStyles from '@hooks/useThemeStyles';
15-
import {clearSpendRuleFormDraft, initDraftSpendRuleMerchants, updateDraftSpendRule} from '@libs/actions/User';
15+
import {clearSpendRuleFormDraft, updateDraftSpendRule} from '@libs/actions/User';
1616
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
1717
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
1818
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
@@ -37,14 +37,12 @@ function SpendRuleMerchantsPage({route}: SpendRuleMerchantsPageProps) {
3737
const merchantNames = spendRuleFormDraft?.merchantNames ?? spendRuleForm?.merchantNames ?? [];
3838
const merchantMatchTypes = spendRuleFormDraft?.merchantMatchTypes ?? spendRuleForm?.merchantMatchTypes ?? [];
3939

40-
useEffect(() => {
41-
initDraftSpendRuleMerchants(spendRuleForm?.merchantNames ?? [], spendRuleForm?.merchantMatchTypes ?? []);
42-
43-
return () => {
40+
useEffect(
41+
() => () => {
4442
clearSpendRuleFormDraft();
45-
};
46-
// eslint-disable-next-line react-hooks/exhaustive-deps
47-
}, []);
43+
},
44+
[],
45+
);
4846

4947
const emptyStateTitle =
5048
restrictionAction === CONST.SPEND_RULES.ACTION.BLOCK ? translate('workspace.rules.spendRules.noBlockedMerchants') : translate('workspace.rules.spendRules.noAllowedMerchants');

0 commit comments

Comments
 (0)