1
1
import { View, StyleSheet, ScrollView } from "react-native";
2
- import React from "react";
2
+ import { useState } from "react";
3
3
import { Button, Input, Text } from "@rneui/themed";
4
4
import { COLORS } from "../../constants";
5
+ import useAuth from "../../hooks/useAuth";
5
6
6
7
const Register = ({ navigation }) => {
8
+ // Variables
9
+ const { register, loading } = useAuth();
10
+ const [firstName, setFirstName] = useState({
11
+ value: null,
12
+ errorMessage: null,
13
+ });
14
+ const [lastName, setLastName] = useState({ value: null, errorMessage: null });
15
+ const [email, setEmail] = useState({ value: null, errorMessage: null });
16
+ const [password, setPassword] = useState({ value: null, errorMessage: null });
17
+ const [confirmPassword, setConfirmPassword] = useState({
18
+ value: null,
19
+ errorMessage: null,
20
+ });
21
+
22
+ // Event handlers
7
23
const handleLoginPress = () => {
8
24
navigation.navigate("Login");
9
25
};
26
+ const handleRegisterPress = async () => {
27
+ setFirstName({ ...firstName, errorMessage: null });
28
+ setLastName({ ...lastName, errorMessage: null });
29
+ setEmail({ ...email, errorMessage: null });
30
+ setPassword({ ...password, errorMessage: null });
31
+ setConfirmPassword({ ...confirmPassword, errorMessage: null });
32
+ if (!firstName.value) {
33
+ setFirstName({
34
+ ...firstName,
35
+ errorMessage: "Please fill out all fields.",
36
+ });
37
+ }
38
+ if (!lastName.value) {
39
+ setLastName({
40
+ ...lastName,
41
+ errorMessage: "Please fill out all fields.",
42
+ });
43
+ }
44
+ if (!email.value) {
45
+ setEmail({ ...email, errorMessage: "Please fill out all fields." });
46
+ }
47
+ if (!password.value) {
48
+ setPassword({
49
+ ...password,
50
+ errorMessage: "Please fill out all fields.",
51
+ });
52
+ }
53
+ if (!confirmPassword.value) {
54
+ setConfirmPassword({
55
+ ...confirmPassword,
56
+ errorMessage: "Please fill out all fields.",
57
+ });
58
+ }
59
+
60
+ if (
61
+ !(
62
+ firstName.value &&
63
+ lastName.value &&
64
+ email.value &&
65
+ password.value &&
66
+ confirmPassword.value
67
+ )
68
+ ) {
69
+ return;
70
+ }
71
+
72
+ if (password.value != confirmPassword.value) {
73
+ setPassword({
74
+ ...password,
75
+ errorMessage: "Password and confirm password do not match.",
76
+ });
77
+ setConfirmPassword({
78
+ ...confirmPassword,
79
+ errorMessage: "Password and confirm password do not match.",
80
+ });
81
+ return;
82
+ }
83
+
84
+ try {
85
+ const userData = {
86
+ firstName: firstName.value,
87
+ lastName: lastName.value,
88
+ email: email.value,
89
+ password: password.value,
90
+ confirmPassword: confirmPassword.value,
91
+ };
92
+ const user = await register(userData);
93
+ navigation.navigate("Login");
94
+ } catch (error) {
95
+ console.error(
96
+ `Error while registering user: Status: ${error.response?.status} Message: ${error.response?.data?.message}
97
+ `
98
+ );
99
+ }
100
+ };
10
101
return (
11
102
<ScrollView
12
103
style={{ width: "100%" }}
@@ -22,17 +113,52 @@ const Register = ({ navigation }) => {
22
113
<View
23
114
style={{ width: "100%", alignItems: "center", paddingVertical: 40 }}
24
115
>
25
- <Input label={"First Name"} labelStyle={styles.inputLabel} />
26
- <Input label={"Last Name"} labelStyle={styles.inputLabel} />
27
- <Input label={"Email"} labelStyle={styles.inputLabel} />
28
- <Input label={"Password"} labelStyle={styles.inputLabel} />
29
- <Input label={"Confirm Password"} labelStyle={styles.inputLabel} />
116
+ <Input
117
+ label={"First Name"}
118
+ labelStyle={styles.inputLabel}
119
+ value={firstName.value}
120
+ errorMessage={firstName.errorMessage}
121
+ onChangeText={(value) => setFirstName({ ...firstName, value })}
122
+ />
123
+ <Input
124
+ label={"Last Name"}
125
+ labelStyle={styles.inputLabel}
126
+ value={lastName.value}
127
+ errorMessage={lastName.errorMessage}
128
+ onChangeText={(value) => setLastName({ ...lastName, value })}
129
+ />
130
+ <Input
131
+ label={"Email"}
132
+ labelStyle={styles.inputLabel}
133
+ value={email.value}
134
+ errorMessage={email.errorMessage}
135
+ onChangeText={(value) => setEmail({ ...email, value })}
136
+ />
137
+ <Input
138
+ label={"Password"}
139
+ labelStyle={styles.inputLabel}
140
+ value={password.value}
141
+ errorMessage={password.errorMessage}
142
+ onChangeText={(value) => setPassword({ ...password, value })}
143
+ secureTextEntry
144
+ />
145
+ <Input
146
+ label={"Confirm Password"}
147
+ labelStyle={styles.inputLabel}
148
+ value={confirmPassword.value}
149
+ errorMessage={confirmPassword.errorMessage}
150
+ onChangeText={(value) =>
151
+ setConfirmPassword({ ...confirmPassword, value })
152
+ }
153
+ secureTextEntry
154
+ />
30
155
31
156
<Button
32
157
color="primary"
33
158
title={"Register"}
34
159
size="lg"
35
- containerStyle={{ width: 200, borderRadius: 5 }}
160
+ containerStyle={{ width: 200, borderRadius: 5, marginTop: 20 }}
161
+ onPress={handleRegisterPress}
36
162
/>
37
163
</View>
38
164
<Text style={{ paddingVertical: 10 }}>
0 commit comments