From e9fa692c821fef0a5c36040dcf2a29a81660370b Mon Sep 17 00:00:00 2001 From: G Date: Tue, 7 May 2024 11:35:49 +0000 Subject: [PATCH] user can login with valid credentials and be redirected to the home screen --- src/screens/UserLoginScreen.tsx | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/src/screens/UserLoginScreen.tsx b/src/screens/UserLoginScreen.tsx index 105560f..854dabd 100644 --- a/src/screens/UserLoginScreen.tsx +++ b/src/screens/UserLoginScreen.tsx @@ -1,4 +1,6 @@ +import { useUserAuthenticationContext } from "@/contexts/UserAuthenticationContext"; import type { UnloggedUserStackScreenComponentProps } from "@/navigations/Types"; +import authenticateUser from "@/utils/requests/authenticateUser"; import Button from "@components/Button"; import ContainerBorderTopCurved from "@components/ContainerBorderTopCurved"; import Input from "@components/Input"; @@ -7,6 +9,8 @@ import Box from "@components/bases/Box"; import Text from "@components/bases/Text"; import { Fontisto } from "@expo/vector-icons"; import { containers } from "@styles/Commons"; +import { useMutation } from "@tanstack/react-query"; +import { useCallback, useState } from "react"; import { KeyboardAvoidingView, Platform, TouchableOpacity, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; @@ -14,12 +18,34 @@ const UserLoginScreen: UnloggedUserStackScreenComponentProps<"userLoginScreen"> navigation, }) => { const insets = useSafeAreaInsets(); + const { setAuthenticationData } = useUserAuthenticationContext(); + + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + + const authenticationMutation = useMutation({ + mutationFn: authenticateUser, + onSuccess: (data) => { + setAuthenticationData(data); + navigation.navigate("bottomTabs"); + }, + onError: (error) => { + console.error("error :: ", error); + }, + }); + + const submit = useCallback(() => { + authenticationMutation.mutate({ + username: email, + password: password, + }); + }, [email, password, authenticationMutation]); return ( @@ -48,8 +74,19 @@ const UserLoginScreen: UnloggedUserStackScreenComponentProps<"userLoginScreen"> - - + + @@ -57,7 +94,10 @@ const UserLoginScreen: UnloggedUserStackScreenComponentProps<"userLoginScreen"> variant={"full"} textVariants={"primary"} label="Se connecter" - onPress={() => navigation.navigate("bottomTabs")} + onPress={() => { + // navigation.navigate("bottomTabs"); + submit(); + }} />