-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerifyOTP.js
246 lines (235 loc) · 6.6 KB
/
VerifyOTP.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import React, { useState, useEffect } from 'react';
import { View, Text, ImageBackground, StyleSheet, TouchableOpacity, ActivityIndicator, Image, KeyboardAvoidingView } from 'react-native';
import OTPTextView from 'react-native-otp-textinput';
import axios from 'axios';
import { Bubbles } from 'react-native-loader';
import { requestOtp, verifyOtp } from './redux/actions/authActions';
import { useDispatch, useSelector } from 'react-redux';
const VerifyOTP = ({ navigation, route }) => {
const dispatch = useDispatch();
const [otp, setOtp] = useState('');
const [seconds, setSeconds] = useState(60);
const [formattedTime, setFormattedTime] = useState('00:00');
const { params } = route;
const loginMethod = params?.loginMethod || '';
const mobile = params?.mobile || '';
const email = params?.email || '';
const [loading, setLoading] = useState(false); // State for controlling the loading indicator
const getOTP = () => {
dispatch(requestOtp(selectedTab === 'Mobile' ? 'mobile' : 'email', selectedTab === 'Mobile' ? mobileValue : emailValue));
};
// handleRegister function
const handleRegister = () => {
setLoading(true);
dispatch(verifyOtp(email, otp));
};
// Check if params exist and then extract loginMethod
useEffect(() => {
const timer = setInterval(() => {
if (seconds > 0) {
setSeconds(seconds - 1);
}
}, 1000);
return () => clearInterval(timer);
}, [seconds]);
useEffect(() => {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
setFormattedTime(
`${minutes < 10 ? '0' : ''}${minutes}:${remainingSeconds < 10 ? '0' : ''
}${remainingSeconds}`
);
}, [seconds]);
const handleBackPress = () => {
navigation.goBack(); // Navigate back to the previous screen
};
const handleVerify = () => {
// Handle logic for getting OTP
handleRegister();
};
const handleChange = (otp) => {
setOtp(otp);
};
const getAnOTP = () => {
// Handle logic for getting OTP
getOTP();
console.log('Get OTP button pressed');
};
return (
<View style={{ position: 'relative', flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#006C97' }}>
{loading && <Bubbles size={10} color="#FFF" />}
<ImageBackground
resizeMode="stretch"
source={require('./bg.png')}
style={styles.curveBackGroundWhite}
/>
<KeyboardAvoidingView style={styles.textContainer}>
<TouchableOpacity onPress={handleBackPress}>
<Image
source={require('./back.png')} // Add your image here
style={styles.backIcon}
resizeMode="contain"
/>
</TouchableOpacity>
<Text style={styles.text}>Verify OTP</Text>
</KeyboardAvoidingView>
<View style={styles.welcomtextContainer}>
<Text style={styles.welcomeText}>
{loginMethod == 'email'
? 'Verification code has been sent to the email address associated with your account ${email}'
: 'A one-time password (OTP) has been sent to the mobile number ending in ${mobile}'}
</Text>
</View>
<View style={styles.viewContainer}>
<Text style={styles.headerText}>Enter OTP</Text>
<View style={styles.viewTextInputContainer}>
<OTPTextView
handleTextChange={handleChange}
containerStyle={{ marginTop: 20 }}
textInputStyle={{ borderColor: 'gray', borderWidth: 2, borderRadius: 30, }}
inputCount={6}
tintColor="#439771"
keyboardType="numeric"
/>
</View>
<TouchableOpacity style={[styles.button, otp.length === 6 && styles.buttonActive]} onPress={handleVerify}>
<Text style={styles.buttonText}>Verify</Text>
</TouchableOpacity>
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-end', marginTop: 15, width: '100%' }}>
<Image
source={require('./timer.png')} // Add your timer icon image here
style={{ width: 32, height: 32, marginRight: 5 }}
resizeMode="contain"
/>
<Text style={{ fontSize: 16, marginRight: 5 }}>{formattedTime}</Text>
</View>
</View>
<View style={styles.containerBottom}>
<Text style={styles.textAccount}>Didn’t receive OTP ?</Text>
<TouchableOpacity style={styles.buttonRegister} onPress={getAnOTP}>
<Text style={styles.buttonRegisterText}>Resend OTP</Text>
</TouchableOpacity>
</View>
</View>
);
}
export default VerifyOTP;
const styles = StyleSheet.create({
container: {
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
paddingHorizontal: 10,
marginTop: 20,
},
containerBottom: {
position: 'absolute',
bottom: 0,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
marginBottom: 30,
},
button: {
width: '100%',
backgroundColor: '#888888',
marginTop: 40,
paddingVertical: 15,
alignItems: 'center',
borderRadius: 5,
},
buttonActive: {
width: '100%',
backgroundColor: '#006C97',
marginTop: 40,
paddingVertical: 15,
alignItems: 'center',
borderRadius: 5,
},
buttonRegister: {
marginLeft: 10,
alignItems: 'center',
},
buttonText: {
color: 'white',
fontSize: 16,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginTop: 10,
paddingHorizontal: 10,
width: '100%',
borderRadius: 5,
},
viewTextInputContainer: {
flex: 1,
flexDirection: 'row',
height: 50,
width: '100%',
},
text: {
color: '#FFFFFF',
fontSize: 28,
fontWeight: 'bold',
height: 50,
},
textAccount: {
color: '#0D0E11',
fontSize: 16,
height: 20,
},
headerText: {
color: '#000000',
fontSize: 16,
marginTop: 5,
textAlign: 'left',
width: '100%',
},
curveBackGroundWhite: {
top: 200,
height: "98%",
width: "100%",
},
buttonRegisterText: {
color: '#006C97',
fontSize: 16,
marginTop: -3,
},
viewContainer: {
flex: 1,
position: 'absolute',
top: 320,
height: 220,
alignItems: 'center',
width: '95%',
},
textContainer: {
flex: 1,
flexDirection: 'row',
position: 'absolute',
top: 80,
height: 50,
left: 20,
},
welcomtextContainer: {
flex: 1,
position: 'absolute',
top: 130,
height: 50,
left: 20,
},
welcomeText: {
color: '#FFFFFF',
fontSize: 14,
marginTop: 5,
marginEnd: 20,
},
backIcon: {
height: 20,
width: 20,
marginRight: 30,
marginTop: 5,
},
});