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 type { UserLoginData } from "@/domain/entities/User"
|
|
|
|
import { useAuthentication } from "@/presentation/react/contexts/Authentication"
|
|
|
|
|
2024-03-15 12:34:53 +01:00
|
|
|
const LoginPage: React.FC = () => {
|
2024-03-22 23:41:51 +01:00
|
|
|
const { login, authenticationPresenter } = useAuthentication()
|
2024-03-15 12:34:53 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
const { control, handleSubmit } = useForm<UserLoginData>({
|
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
password: "",
|
|
|
|
},
|
|
|
|
})
|
2024-03-15 12:34:53 +01:00
|
|
|
|
2024-03-22 23:41:51 +01:00
|
|
|
const onSubmit = async (data: unknown): Promise<void> => {
|
|
|
|
await authenticationPresenter.login(data)
|
|
|
|
}
|
2024-03-15 12:34:53 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeAreaView style={styles.container}>
|
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}
|
|
|
|
style={styles.input}
|
|
|
|
mode="outlined"
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}}
|
|
|
|
name="email"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<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="Password"
|
|
|
|
onBlur={onBlur}
|
|
|
|
onChangeText={onChange}
|
|
|
|
value={value}
|
|
|
|
style={styles.input}
|
|
|
|
mode="outlined"
|
|
|
|
secureTextEntry
|
2024-03-15 12:34:53 +01:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}}
|
2024-03-22 23:41:51 +01:00
|
|
|
name="password"
|
|
|
|
/>
|
|
|
|
|
|
|
|
<HelperText
|
|
|
|
type="error"
|
|
|
|
visible={login.state === "error"}
|
|
|
|
style={styles.helperText}
|
2024-03-15 12:34:53 +01:00
|
|
|
>
|
2024-03-22 23:41:51 +01:00
|
|
|
Invalid credentials.
|
2024-03-15 12:34:53 +01:00
|
|
|
</HelperText>
|
2024-03-22 23:41:51 +01:00
|
|
|
|
|
|
|
<Button
|
|
|
|
mode="contained"
|
|
|
|
onPress={handleSubmit(onSubmit)}
|
|
|
|
loading={login.state === "loading"}
|
|
|
|
disabled={login.state === "loading"}
|
|
|
|
>
|
|
|
|
Login
|
|
|
|
</Button>
|
2024-03-15 12:34:53 +01:00
|
|
|
</SafeAreaView>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
},
|
|
|
|
input: {
|
|
|
|
width: "80%",
|
|
|
|
marginBottom: 10,
|
|
|
|
},
|
2024-03-22 23:41:51 +01:00
|
|
|
helperText: {
|
|
|
|
fontSize: 18,
|
|
|
|
marginVertical: 20,
|
2024-03-15 12:34:53 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export default LoginPage
|