1
1
import type { StackScreenProps } from '@react-navigation/stack' ;
2
- import React , { useEffect , useState } from 'react' ;
2
+ import React , { useCallback , useEffect , useState } from 'react' ;
3
3
import { View } from 'react-native' ;
4
- import type { OnyxEntry } from 'react-native-onyx' ;
5
- import { withOnyx } from 'react-native-onyx' ;
4
+ import { useOnyx } from 'react-native-onyx' ;
6
5
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton' ;
7
6
import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
8
7
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription' ;
9
8
import ScreenWrapper from '@components/ScreenWrapper' ;
10
9
import SingleOptionSelector from '@components/SingleOptionSelector' ;
11
10
import Text from '@components/Text' ;
11
+ import ValidateCodeActionModal from '@components/ValidateCodeActionModal' ;
12
12
import useLocalize from '@hooks/useLocalize' ;
13
13
import usePrevious from '@hooks/usePrevious' ;
14
14
import useStyledSafeAreaInsets from '@hooks/useStyledSafeAreaInsets' ;
15
15
import useThemeStyles from '@hooks/useThemeStyles' ;
16
+ import { requestValidateCodeAction } from '@libs/actions/User' ;
17
+ import * as ErrorUtils from '@libs/ErrorUtils' ;
16
18
import Navigation from '@libs/Navigation/Navigation' ;
17
19
import type { SettingsNavigatorParamList } from '@libs/Navigation/types' ;
18
20
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils' ;
@@ -24,8 +26,6 @@ import type {TranslationPaths} from '@src/languages/types';
24
26
import ONYXKEYS from '@src/ONYXKEYS' ;
25
27
import ROUTES from '@src/ROUTES' ;
26
28
import type SCREENS from '@src/SCREENS' ;
27
- import type { ReportPhysicalCardForm } from '@src/types/form' ;
28
- import type { Card , PrivatePersonalDetails } from '@src/types/onyx' ;
29
29
import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
30
30
31
31
const OPTIONS_KEYS = {
@@ -50,49 +50,32 @@ const OPTIONS: Option[] = [
50
50
} ,
51
51
] ;
52
52
53
- type ReportCardLostPageOnyxProps = {
54
- /** Onyx form data */
55
- formData : OnyxEntry < ReportPhysicalCardForm > ;
56
-
57
- /** User's private personal details */
58
- privatePersonalDetails : OnyxEntry < PrivatePersonalDetails > ;
59
-
60
- /** User's cards list */
61
- cardList : OnyxEntry < Record < string , Card > > ;
62
- } ;
63
-
64
- type ReportCardLostPageProps = ReportCardLostPageOnyxProps & StackScreenProps < SettingsNavigatorParamList , typeof SCREENS . SETTINGS . REPORT_CARD_LOST_OR_DAMAGED > ;
53
+ type ReportCardLostPageProps = StackScreenProps < SettingsNavigatorParamList , typeof SCREENS . SETTINGS . REPORT_CARD_LOST_OR_DAMAGED > ;
65
54
66
55
function ReportCardLostPage ( {
67
- privatePersonalDetails = {
68
- addresses : [
69
- {
70
- street : '' ,
71
- street2 : '' ,
72
- city : '' ,
73
- state : '' ,
74
- zip : '' ,
75
- country : '' ,
76
- } ,
77
- ] ,
78
- } ,
79
- cardList = { } ,
80
56
route : {
81
57
params : { cardID = '' } ,
82
58
} ,
83
- formData,
84
59
} : ReportCardLostPageProps ) {
85
60
const styles = useThemeStyles ( ) ;
86
61
87
- const physicalCard = cardList ?. [ cardID ] ;
88
-
89
62
const { translate} = useLocalize ( ) ;
90
63
64
+ const [ loginList ] = useOnyx ( ONYXKEYS . LOGIN_LIST ) ;
65
+ const [ account ] = useOnyx ( ONYXKEYS . ACCOUNT ) ;
66
+ const [ formData ] = useOnyx ( ONYXKEYS . FORMS . REPORT_PHYSICAL_CARD_FORM ) ;
67
+ const [ cardList ] = useOnyx ( ONYXKEYS . CARD_LIST ) ;
68
+ const [ privatePersonalDetails ] = useOnyx ( ONYXKEYS . PRIVATE_PERSONAL_DETAILS ) ;
69
+
91
70
const [ reason , setReason ] = useState < Option > ( ) ;
92
71
const [ isReasonConfirmed , setIsReasonConfirmed ] = useState ( false ) ;
93
72
const [ shouldShowAddressError , setShouldShowAddressError ] = useState ( false ) ;
94
73
const [ shouldShowReasonError , setShouldShowReasonError ] = useState ( false ) ;
95
74
75
+ const physicalCard = cardList ?. [ cardID ] ;
76
+ const validateError = ErrorUtils . getLatestErrorMessageField ( physicalCard ) ;
77
+ const [ isValidateCodeActionModalVisible , setIsValidateCodeActionModalVisible ] = useState ( false ) ;
78
+
96
79
const prevIsLoading = usePrevious ( formData ?. isLoading ) ;
97
80
98
81
const { paddingBottom} = useStyledSafeAreaInsets ( ) ;
@@ -115,6 +98,16 @@ function ReportCardLostPage({
115
98
FormActions . setErrors ( ONYXKEYS . FORMS . REPORT_PHYSICAL_CARD_FORM , physicalCard ?. errors ?? { } ) ;
116
99
} , [ formData ?. isLoading , physicalCard ?. errors ] ) ;
117
100
101
+ const handleValidateCodeEntered = useCallback (
102
+ ( validateCode : string ) => {
103
+ if ( ! physicalCard ) {
104
+ return ;
105
+ }
106
+ CardActions . requestReplacementExpensifyCard ( physicalCard . cardID , reason ?. key as ReplacementReason , validateCode ) ;
107
+ } ,
108
+ [ physicalCard , reason ?. key ] ,
109
+ ) ;
110
+
118
111
if ( isEmptyObject ( physicalCard ) ) {
119
112
return < NotFoundPage /> ;
120
113
}
@@ -135,8 +128,17 @@ function ReportCardLostPage({
135
128
setShouldShowAddressError ( true ) ;
136
129
return ;
137
130
}
131
+ setIsValidateCodeActionModalVisible ( true ) ;
132
+ } ;
133
+
134
+ const sendValidateCode = ( ) => {
135
+ const primaryLogin = account ?. primaryLogin ?? '' ;
136
+
137
+ if ( loginList ?. [ primaryLogin ] ?. validateCodeSent ) {
138
+ return ;
139
+ }
138
140
139
- CardActions . requestReplacementExpensifyCard ( physicalCard . cardID , reason ?. key as ReplacementReason ) ;
141
+ requestValidateCodeAction ( ) ;
140
142
} ;
141
143
142
144
const handleOptionSelect = ( option : Option ) => {
@@ -189,6 +191,18 @@ function ReportCardLostPage({
189
191
isLoading = { formData ?. isLoading }
190
192
buttonText = { isDamaged ? translate ( 'reportCardLostOrDamaged.shipNewCardButton' ) : translate ( 'reportCardLostOrDamaged.deactivateCardButton' ) }
191
193
/>
194
+ < ValidateCodeActionModal
195
+ handleSubmitForm = { handleValidateCodeEntered }
196
+ sendValidateCode = { sendValidateCode }
197
+ validateError = { validateError }
198
+ clearError = { ( ) => {
199
+ CardActions . clearCardListErrors ( physicalCard . cardID ) ;
200
+ } }
201
+ onClose = { ( ) => setIsValidateCodeActionModalVisible ( false ) }
202
+ isVisible = { isValidateCodeActionModalVisible }
203
+ title = { translate ( 'cardPage.validateCardTitle' ) }
204
+ description = { translate ( 'cardPage.enterMagicCode' , { contactMethod : account ?. primaryLogin ?? '' } ) }
205
+ />
192
206
</ >
193
207
) : (
194
208
< >
@@ -215,14 +229,4 @@ function ReportCardLostPage({
215
229
216
230
ReportCardLostPage . displayName = 'ReportCardLostPage' ;
217
231
218
- export default withOnyx < ReportCardLostPageProps , ReportCardLostPageOnyxProps > ( {
219
- privatePersonalDetails : {
220
- key : ONYXKEYS . PRIVATE_PERSONAL_DETAILS ,
221
- } ,
222
- cardList : {
223
- key : ONYXKEYS . CARD_LIST ,
224
- } ,
225
- formData : {
226
- key : ONYXKEYS . FORMS . REPORT_PHYSICAL_CARD_FORM ,
227
- } ,
228
- } ) ( ReportCardLostPage ) ;
232
+ export default ReportCardLostPage ;
0 commit comments