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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/**
* Author : Ashm Walia
* Purpose : Add a new screen to allow the user to enter first and last name
*/
import React, {useState, useRef} from 'react';
import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import {
View,
Text,
StyleSheet,
StatusBar,
Alert,
Platform,
TouchableOpacity,
KeyboardAvoidingView,
ActivityIndicator,
} from 'react-native';
import {OnboardingStackParams} from '../../routes';
import {
ArrowButton,
RegistrationWizard,
TaggInput,
TermsConditions,
Background,
LoadingIndicator,
} from '../../components';
import {passwordRegex, usernameRegex, REGISTER_ENDPOINT} from '../../constants';
import AsyncStorage from '@react-native-community/async-storage';
import {BackgroundGradientType} from '../../types';
import {
ERROR_DOUBLE_CHECK_CONNECTION,
ERROR_REGISTRATION,
ERROR_SOMETHING_WENT_WRONG_REFRESH,
} from '../../constants/strings';
type RegistrationScreenThreeRouteProp = RouteProp<
OnboardingStackParams,
'RegistrationThree'
>;
type RegistrationScreenThreeNavigationProp = StackNavigationProp<
OnboardingStackParams,
'RegistrationThree'
>;
interface RegistrationThreeProps {
route: RegistrationScreenThreeRouteProp;
navigation: RegistrationScreenThreeNavigationProp;
}
/**
* Registration screen 3 for username, password, and terms and conditions
* @param navigation react-navigation navigation object
*/
const RegistrationThree: React.FC<RegistrationThreeProps> = ({
route,
navigation,
}) => {
// refs for changing focus
const usernameRef = useRef();
const passwordRef = useRef();
const confirmRef = useRef();
const registrationName = route.params;
const fname: string = registrationName!.firstName;
const lname: string = registrationName!.lastName;
const phone: string = registrationName!.phone;
const email: string = registrationName!.email;
/**
* Handles focus change to the next input field.
* @param field key for field to move focus onto
*/
const handleFocusChange = (field: string): void => {
switch (field) {
case 'username':
const usernameField: any = usernameRef.current;
usernameField.focus();
break;
case 'password':
const passwordField: any = passwordRef.current;
passwordField.focus();
break;
case 'confirm':
const confirmField: any = confirmRef.current;
confirmField.focus();
break;
default:
return;
}
};
// registration form state
const [form, setForm] = useState({
phone: '',
username: '',
password: '',
confirm: '',
isValidPhone: false,
isValidUsername: false,
isValidPassword: false,
passwordsMatch: false,
tcAccepted: false,
attemptedSubmit: false,
});
/*
* Handles changes to the username field value and verifies the input by updating state and running a validation function.
*/
const handleUsernameUpdate = (username: string) => {
let isValidUsername: boolean = usernameRegex.test(username);
setForm({
...form,
username,
isValidUsername,
});
};
/*
* Handles changes to the password field value and verifies the input by updating state and running a validation function.
*/
const handlePasswordUpdate = (password: string) => {
let isValidPassword: boolean = passwordRegex.test(password);
let passwordsMatch: boolean = form.password === form.confirm;
setForm({
...form,
password,
isValidPassword,
passwordsMatch,
});
};
/*
* Handles changes to the confirm password field value and verifies the input by updating state and running a validation function.
*/
const handleConfirmUpdate = (confirm: string) => {
let passwordsMatch: boolean = form.password === confirm;
setForm({
...form,
confirm,
passwordsMatch,
});
};
/**
* Handles changes to the terms and conditions accepted boolean.
* @param tcAccepted the boolean to set the terms and conditions value to
*/
const handleTcUpdate = (tcAccepted: boolean) => {
setForm({
...form,
tcAccepted,
});
};
/**
* Handles a click on the "next" arrow button by sending an API request to the backend and displaying the appropriate response.
*/
const handleRegister = async () => {
if (!form.attemptedSubmit) {
setForm({
...form,
attemptedSubmit: true,
});
}
try {
if (form.isValidUsername && form.isValidPassword && form.passwordsMatch) {
if (form.tcAccepted) {
let registerResponse = await fetch(REGISTER_ENDPOINT, {
method: 'POST',
body: JSON.stringify({
first_name: fname,
last_name: lname,
email: email,
phone_number: phone,
username: form.username,
password: form.password,
}),
});
let statusCode = registerResponse.status;
let data = await registerResponse.json();
const userId: string = data.UserID;
if (statusCode === 201) {
try {
await AsyncStorage.setItem('token', data.token);
/*
* Skipping navigation to Checkpoint for alpha
* navigation.navigate('Checkpoint', { userId: userId, username: form.username });
*/
navigation.navigate('ProfileOnboarding', {
userId: userId,
username: form.username,
});
} catch (err) {
console.log(err);
}
} else if (statusCode === 409) {
Alert.alert(ERROR_REGISTRATION(data));
} else {
Alert.alert(ERROR_SOMETHING_WENT_WRONG_REFRESH);
}
} else {
Alert.alert(
'Terms and conditions',
'You must first agree to the terms and conditions.',
);
}
} else {
setForm({...form, attemptedSubmit: false});
setTimeout(() => setForm({...form, attemptedSubmit: true}));
}
} catch (error) {
Alert.alert(ERROR_REGISTRATION(ERROR_DOUBLE_CHECK_CONNECTION));
return {
name: 'Registration error',
description: error,
};
}
};
const Footer = () => (
<View style={styles.footer}>
<ArrowButton
direction="backward"
onPress={() => navigation.navigate('RegistrationTwo', {phone: phone})}
/>
<TouchableOpacity onPress={handleRegister}>
<ArrowButton
direction="forward"
disabled={
!(
form.isValidUsername &&
form.isValidPassword &&
form.passwordsMatch &&
form.tcAccepted
)
}
onPress={handleRegister}
/>
</TouchableOpacity>
</View>
);
return (
<Background
style={styles.container}
gradientType={BackgroundGradientType.Light}>
<StatusBar barStyle="light-content" />
<RegistrationWizard style={styles.wizard} step="five" />
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}>
<View>
<Text style={styles.formHeader}>SIGN UP</Text>
</View>
<TaggInput
accessibilityHint="Enter a username."
accessibilityLabel="Username input field."
placeholder="Username"
autoCompleteType="username"
textContentType="username"
autoCapitalize="none"
returnKeyType="next"
onChangeText={handleUsernameUpdate}
onSubmitEditing={() => handleFocusChange('password')}
blurOnSubmit={false}
ref={usernameRef}
valid={form.isValidUsername}
invalidWarning={
'Username must be at least 6 characters and contain only alphanumerics.'
}
attemptedSubmit={form.attemptedSubmit}
width={280}
/>
<TaggInput
accessibilityHint="Enter a password."
accessibilityLabel="Password input field."
placeholder="Password"
autoCompleteType="password"
textContentType="oneTimeCode"
returnKeyType="next"
onChangeText={handlePasswordUpdate}
onSubmitEditing={() => handleFocusChange('confirm')}
blurOnSubmit={false}
secureTextEntry
ref={passwordRef}
valid={form.isValidPassword}
invalidWarning={
'Password must be at least 8 characters & contain at least one of a-z, A-Z, 0-9, and a special character.'
}
attemptedSubmit={form.attemptedSubmit}
width={280}
/>
<TaggInput
accessibilityHint={'Re-enter your password.'}
accessibilityLabel={'Password confirmation input field.'}
placeholder={'Confirm Password'}
autoCompleteType="password"
textContentType="oneTimeCode"
returnKeyType={form.tcAccepted ? 'go' : 'default'}
onChangeText={handleConfirmUpdate}
onSubmitEditing={handleRegister}
secureTextEntry
ref={confirmRef}
valid={form.passwordsMatch}
invalidWarning={'Passwords must match.'}
attemptedSubmit={form.attemptedSubmit}
width={280}
/>
<LoadingIndicator />
<TermsConditions
style={styles.tc}
accepted={form.tcAccepted}
onChange={handleTcUpdate}
/>
</KeyboardAvoidingView>
<Footer />
</Background>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
wizard: {
...Platform.select({
ios: {
top: 50,
},
android: {
bottom: 40,
},
}),
},
formHeader: {
color: '#fff',
fontSize: 30,
fontWeight: '600',
marginBottom: '16%',
},
tc: {
marginVertical: '5%',
top: '8%',
},
load: {
top: '5%',
},
footer: {
width: '100%',
flexDirection: 'row',
justifyContent: 'space-around',
...Platform.select({
ios: {
bottom: '20%',
},
android: {
bottom: '10%',
},
}),
},
});
export default RegistrationThree;
|