attempt to use react query

This commit is contained in:
Guillaume Dorce 2022-09-02 17:53:43 +02:00
parent 30076c8d3e
commit e0939994b8
4 changed files with 65 additions and 8 deletions

View File

@ -9,6 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"@tanstack/react-query": "^4.2.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0"

View File

@ -1,6 +1,7 @@
lockfileVersion: 5.4
specifiers:
'@tanstack/react-query': ^4.2.3
'@types/react': ^18.0.17
'@types/react-dom': ^18.0.6
'@vitejs/plugin-react': ^2.0.1
@ -15,6 +16,7 @@ specifiers:
vite: ^3.0.7
dependencies:
'@tanstack/react-query': 4.2.3_biqbaboplfbrettd7655fr4n2y
react: 18.2.0
react-dom: 18.2.0_react@18.2.0
react-router-dom: 6.3.0_biqbaboplfbrettd7655fr4n2y
@ -374,6 +376,29 @@ packages:
fastq: 1.13.0
dev: true
/@tanstack/query-core/4.2.3:
resolution: {integrity: sha512-zdt5lYWs1dZaA3IxJbCgtAfHZJScRZONpiLL7YkeOkrme5MfjQqTpjq7LYbzpyuwPOh2Jx68le0PLl57JFv5hQ==}
dev: false
/@tanstack/react-query/4.2.3_biqbaboplfbrettd7655fr4n2y:
resolution: {integrity: sha512-JLaMOxoJTkiAu7QpevRCt2uI/0vd3E8K/rSlCuRgWlcW5DeJDFpDS5kfzmLO5MOcD97fgsJRrDbxDORxR1FdJA==}
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
react-native: '*'
peerDependenciesMeta:
react-dom:
optional: true
react-native:
optional: true
dependencies:
'@tanstack/query-core': 4.2.3
'@types/use-sync-external-store': 0.0.3
react: 18.2.0
react-dom: 18.2.0_react@18.2.0
use-sync-external-store: 1.2.0_react@18.2.0
dev: false
/@types/prop-types/15.7.5:
resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}
dev: true
@ -396,6 +421,10 @@ packages:
resolution: {integrity: sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==}
dev: true
/@types/use-sync-external-store/0.0.3:
resolution: {integrity: sha512-EwmlvuaxPNej9+T4v5AuBPJa2x2UOJVdjCtDHgcDqitUeOtjnJKJ+apYjVcAoBEMjKW1VVFGZLUb5+qqa09XFA==}
dev: false
/@vitejs/plugin-react/2.0.1_vite@3.0.7:
resolution: {integrity: sha512-uINzNHmjrbunlFtyVkST6lY1ewSfz/XwLufG0PIqvLGnpk2nOIOa/1CACTDNcKi1/RwaCzJLmsXwm1NsUVV/NA==}
engines: {node: ^14.18.0 || >=16.0.0}
@ -1298,6 +1327,14 @@ packages:
picocolors: 1.0.0
dev: true
/use-sync-external-store/1.2.0_react@18.2.0:
resolution: {integrity: sha512-eEgnFxGQ1Ife9bzYs6VLi8/4X6CObHMw9Qr9tPY43iKwsPw8xE8+EFsf/2cFZ5S3esXgpWgtSCtLNS41F+sKPA==}
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
dependencies:
react: 18.2.0
dev: false
/util-deprecate/1.0.2:
resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==}
dev: true

View File

@ -1,9 +1,13 @@
import { Outlet } from "react-router-dom";
import { Outlet } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
// Create a client
const queryClient = new QueryClient();
export default () => {
return (
<>
<QueryClientProvider client={queryClient}>
<Outlet />
</>
</QueryClientProvider>
);
}
};

View File

@ -1,8 +1,23 @@
import { Link } from 'react-router-dom';
import logo from '@assets/images/logo.svg';
import { useQuery, useMutation, useQueryClient, QueryClient, QueryClientProvider } from '@tanstack/react-query';
export default () => {
// Access the client
const queryClient = useQueryClient();
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')})
}
return (
<>
<div className="flex flex-col min-h-full items-center justify-center py-12 px-4 bg-grey-dark sm:px-6 lg:px-8">
@ -10,14 +25,14 @@ export default () => {
<img className="mx-auto h-20 pb-2 w-auto" src={logo} alt="Groupomania" />
</div>
<div className="w-full max-w-md bg-grey rounded-lg p-5">
<form className="m-6 mb-3" action="#" method="POST">
<form className="m-6 mb-3" action="#" method="POST" onSubmit={onSubmit}>
<div className="-space-y-px rounded-md shadow-sm">
<div>
<label htmlFor="email-address" className="sr-only">
<label htmlFor="email" className="sr-only">
Adresse e-mail
</label>
<input
id="email-address"
id="email"
name="email"
type="email"
autoComplete="email"