import { useLoginMutation } from "@/hooks/mutations/useLoginMutation" import { useAuth, useAuthLogin, useAuthLogout } from "@/hooks/useAuth" import { useMutation } from "@tanstack/react-query" import { useRouter } from "expo-router" import { useRef, useState } from "react" import { Appbar, Button, Dialog, Portal, Surface, Text, TextInput } from "react-native-paper" export default function Login() { const router = useRouter() const auth = useAuth() const authLogin = useAuthLogin() const authLogout = useAuthLogout() const isLoggedIn = auth.loggedIn const [name, setName] = useState(auth.name ?? "") const [password, setPassword] = useState("") const [errorDialogVisible, setErrorDialogVisible] = useState(false) const [errorTitle, setErrorTitle] = useState("") const [errorText, setErrorText] = useState("") const loginRef = useRef() const passwordRef = useRef() const hideErrorDialog = () => setErrorDialogVisible(false) const loginMutation = useLoginMutation({ authLogin, onPostSuccess: () => { if (router.canGoBack()) router.back() else router.navigate('/') }, onError: ({ response, error }) => { setErrorDialogVisible(true) if (response) { setErrorTitle(response.error) setErrorText(response.message) } else { setErrorTitle("Erreur") setErrorText(`Une erreur est survenue lors de la connexion : ${error}`) } } }) return ( {isLoggedIn && router.canGoBack() ? router.back()} /> : undefined} {isLoggedIn ? : undefined} passwordRef?.current.focus()} style={{ margin: 8 }} /> loginMutation.mutate({ name, password })} secureTextEntry={true} style={{ margin: 8 }} /> {errorTitle} {errorText} ) }