-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
128 lines (123 loc) · 3.84 KB
/
index.tsx
File metadata and controls
128 lines (123 loc) · 3.84 KB
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
import { ThemedText } from "@/components/themed-text";
import { ThemedView } from "@/components/themed-view";
import { router } from "expo-router";
import { useState } from "react";
import { KeyboardAvoidingView, Platform, ScrollView, View } from "react-native";
import { useAuthContext } from "@/hooks/use-auth-context";
import { Controller, useForm } from "react-hook-form";
import { ErrorMessage } from "@/components/ErrorMessage";
import { PageRedirectButton } from "@/components/PageRedirectButton";
import { Button } from "@/components/Button";
import { AuthFormInput } from "@/components/AuthFormInput";
import { Dropdown } from "@/components/Dropdown";
type SignupFormData = {
name: string;
email: string;
username: string;
password: string;
language_preference: string;
profile_picture_s3_key: string | undefined;
};
export default function SignupScreen() {
const [errorText, setErrorText] = useState("");
const { signup } = useAuthContext();
const { control, handleSubmit } = useForm<SignupFormData>({
defaultValues: {
name: "",
email: "",
username: "",
password: "",
language_preference: "",
profile_picture_s3_key: undefined,
},
});
const onSubmit = (formData: SignupFormData) => {
if (
formData.name === "" ||
formData.email === "" ||
formData.username === "" ||
formData.password === "" ||
formData.language_preference === ""
) {
setErrorText("Missing a required field");
} else {
signup(
formData.name,
formData.email,
formData.username,
formData.password,
formData.language_preference,
formData.profile_picture_s3_key,
setErrorText,
);
}
};
const handleGoToLogIn = () => {
router.push("/(auth)/login");
};
return (
<ThemedView className="flex-1">
<KeyboardAvoidingView
behavior={Platform.OS === "ios" ? "padding" : "height"}
className="flex-1"
>
<ScrollView
contentContainerStyle={{ flexGrow: 1 }}
keyboardShouldPersistTaps="handled"
>
<View className="flex-1 items-center justify-center px-6 gap-4">
<ThemedText type="title" className="text-3xl font-bold mb-8">
Sign Up
</ThemedText>
<AuthFormInput
control={control}
name="name"
placeholder="Full Name"
autoCapitalize="none"
/>
<AuthFormInput
control={control}
name="email"
placeholder="Email"
keyboardType="email-address"
autoCapitalize="none"
/>
<AuthFormInput
control={control}
name="username"
placeholder="Username"
autoCapitalize="none"
/>
<AuthFormInput
control={control}
name="password"
placeholder="Password"
secureTextEntry={true}
/>
<Controller
control={control}
name="language_preference"
render={({ field: { onChange, value } }) => (
<Dropdown
value={value}
onChange={onChange}
options={[
{ label: "English", value: "en" },
{ label: "Thai", value: "th" },
]}
placeholder="Select a language..."
/>
)}
/>
<Button label="Sign Up" onPress={handleSubmit(onSubmit)} />
<PageRedirectButton
label="Already have an account? Log in"
onPress={handleGoToLogIn}
/>
<ErrorMessage message={errorText} />
</View>
</ScrollView>
</KeyboardAvoidingView>
</ThemedView>
);
}