2024-03-22 23:41:51 +01:00
|
|
|
import { Controller, useForm } from "react-hook-form"
|
|
|
|
import { StyleSheet } from "react-native"
|
|
|
|
import { Button, HelperText, TextInput } from "react-native-paper"
|
2024-03-15 12:34:53 +01:00
|
|
|
import { SafeAreaView } from "react-native-safe-area-context"
|
2024-03-22 23:41:51 +01:00
|
|
|
import { useMemo } from "react"
|
|
|
|
|
|
|
|
import type { UserRegisterData } from "@/domain/entities/User"
|
|
|
|
import { useAuthentication } from "@/presentation/react/contexts/Authentication"
|
2024-03-15 12:34:53 +01:00
|
|
|
|
|
|
|
const RegisterPage: React.FC = () => {
|
2024-03-22 23:41:51 +01:00
|
|
|
const { register, authenticationPresenter } = useAuthentication()
|
|
|
|
|
|
|
|
const { control, handleSubmit } = useForm<UserRegisterData>({
|
|
|
|
defaultValues: {
|
|
|
|
displayName: "",
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const onSubmit = async (data: unknown): Promise<void> => {
|
|
|
|
await authenticationPresenter.register(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
const helperMessage = useMemo(() => {
|
|
|
|
if (register.state === "error") {
|
2024-03-24 23:41:23 +01:00
|
|
|
if (register.errors.fields.includes("displayName")) {
|
2024-03-22 23:41:51 +01:00
|
|
|
return "Display Name is required."
|
|
|
|
}
|
2024-03-24 23:41:23 +01:00
|
|
|
if (register.errors.fields.includes("email")) {
|
2024-03-22 23:41:51 +01:00
|
|
|
return "Invalid email."
|
|
|
|
}
|
2024-03-24 23:41:23 +01:00
|
|
|
if (register.errors.fields.includes("password")) {
|
2024-03-22 23:41:51 +01:00
|
|
|
return "Password must be at least 6 characters."
|
|
|
|
}
|
|
|
|
return "Invalid credentials."
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (register.state === "success") {
|
|
|
|
// return "Success! Please verify your email."
|
|
|
|
// }
|
2024-03-15 12:34:53 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
return ""
|
2024-03-24 23:41:23 +01:00
|
|
|
}, [register.errors.fields, register.state])
|
2024-03-15 12:34:53 +01:00
|
|
|
|
|
|
|
return (
|
2024-03-24 23:41:23 +01:00
|
|
|
<SafeAreaView style={[styles.container]}>
|
2024-03-22 23:41:51 +01:00
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange, onBlur, value } }) => {
|
2024-03-15 12:34:53 +01:00
|
|
|
return (
|
2024-03-22 23:41:51 +01:00
|
|
|
<TextInput
|
|
|
|
placeholder="Display Name"
|
|
|
|
onBlur={onBlur}
|
|
|
|
onChangeText={onChange}
|
|
|
|
value={value}
|
2024-03-24 23:41:23 +01:00
|
|
|
style={[styles.input]}
|
2024-03-22 23:41:51 +01:00
|
|
|
mode="outlined"
|
2024-03-15 12:34:53 +01:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}}
|
2024-03-22 23:41:51 +01:00
|
|
|
name="displayName"
|
2024-03-15 12:34:53 +01:00
|
|
|
/>
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange, onBlur, value } }) => {
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
placeholder="Email"
|
|
|
|
onBlur={onBlur}
|
|
|
|
onChangeText={onChange}
|
|
|
|
value={value}
|
2024-03-24 23:41:23 +01:00
|
|
|
style={[styles.input]}
|
2024-03-22 23:41:51 +01:00
|
|
|
mode="outlined"
|
|
|
|
/>
|
|
|
|
)
|
2024-03-15 12:34:53 +01:00
|
|
|
}}
|
2024-03-22 23:41:51 +01:00
|
|
|
name="email"
|
2024-03-15 12:34:53 +01:00
|
|
|
/>
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
<Controller
|
|
|
|
control={control}
|
|
|
|
render={({ field: { onChange, onBlur, value } }) => {
|
|
|
|
return (
|
|
|
|
<TextInput
|
|
|
|
placeholder="Password"
|
|
|
|
onBlur={onBlur}
|
|
|
|
onChangeText={onChange}
|
|
|
|
value={value}
|
2024-03-24 23:41:23 +01:00
|
|
|
style={[styles.input]}
|
2024-03-22 23:41:51 +01:00
|
|
|
mode="outlined"
|
|
|
|
secureTextEntry
|
|
|
|
/>
|
|
|
|
)
|
2024-03-15 12:34:53 +01:00
|
|
|
}}
|
2024-03-22 23:41:51 +01:00
|
|
|
name="password"
|
2024-03-15 12:34:53 +01:00
|
|
|
/>
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
<HelperText
|
|
|
|
type={register.state === "error" ? "error" : "info"}
|
|
|
|
visible={register.state === "error" || register.state === "success"}
|
2024-03-24 23:41:23 +01:00
|
|
|
style={[styles.helperText]}
|
2024-03-22 23:41:51 +01:00
|
|
|
>
|
|
|
|
{helperMessage}
|
2024-03-15 12:34:53 +01:00
|
|
|
</HelperText>
|
2024-03-22 23:41:51 +01:00
|
|
|
|
2024-03-15 12:34:53 +01:00
|
|
|
<Button
|
|
|
|
mode="contained"
|
2024-05-20 15:05:34 +02:00
|
|
|
labelStyle={{ fontSize: 18 }}
|
2024-03-22 23:41:51 +01:00
|
|
|
onPress={handleSubmit(onSubmit)}
|
|
|
|
loading={register.state === "loading"}
|
|
|
|
disabled={register.state === "loading"}
|
2024-03-15 12:34:53 +01:00
|
|
|
>
|
|
|
|
Register
|
|
|
|
</Button>
|
|
|
|
</SafeAreaView>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
width: "80%",
|
2024-03-22 23:41:51 +01:00
|
|
|
marginBottom: 10,
|
2024-03-15 12:34:53 +01:00
|
|
|
},
|
2024-03-22 23:41:51 +01:00
|
|
|
helperText: {
|
|
|
|
fontSize: 18,
|
|
|
|
marginVertical: 20,
|
2024-03-15 12:34:53 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default RegisterPage
|