diff --git a/client/src/routes/login.tsx b/client/src/routes/login.tsx index d01e624..d556e35 100644 --- a/client/src/routes/login.tsx +++ b/client/src/routes/login.tsx @@ -1,21 +1,40 @@ import { Link } from 'react-router-dom'; import logo from '@assets/images/logo.svg'; -import { useQuery, useMutation, useQueryClient, QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { useState } from 'react'; export default () => { - // Access the client - const queryClient = useQueryClient(); + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); - const mutation = useMutation(formData => { - return fetch('/api/auth/login', { - method: 'POST', - body: formData, - }); - }) - const onSubmit = event => { - event.preventDefault() - const formData = new FormData(event.target) - mutation.mutate({email: formData.get('email'), password: formData.get('password')}) + const { refetch } = useQuery( + ['login'], + async () => { + const response = await fetch('/api/auth/login', { + method: 'POST', + body: JSON.stringify({ email, password }), + mode: 'cors', + headers: { + 'Content-Type': 'application/json', + }, + }); + return response.json(); + }, + { + onSuccess: (data) => { + console.log(data); + }, + onError: (error) => { + console.log(error); + }, + enabled: false, + refetchOnWindowFocus: false, + } + ); + + const onSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + await refetch(); } return ( @@ -25,7 +44,7 @@ export default () => { Groupomania
-
+ onSubmit(e)}>
@@ -53,6 +74,8 @@ export default () => { required className="relative block w-full appearance-none rounded-lg border px-3 py-2 my-2 placeholder-grey-light focus:z-10 focus:border-red focus:outline-none focus:ring-red sm:text-sm" placeholder="Mot de passe" + value={password} + onChange={(e) => setPassword(e.target.value)} />