generated from WildCodeSchool/create-js-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d6585c6
commit 2913d73
Showing
4 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,70 @@ | ||
import { useRef } from "react"; | ||
import { | ||
useLoaderData, | ||
useOutletContext, | ||
useRevalidator, | ||
} from "react-router-dom"; | ||
|
||
function Home() { | ||
return "hello from home"; | ||
const items = useLoaderData(); | ||
|
||
const titleRef = useRef(); | ||
|
||
const { user } = useOutletContext(); | ||
|
||
const revalidator = useRevalidator(); | ||
|
||
// Gestionnaire de soumission du formulaire | ||
const handleSubmit = async (event) => { | ||
event.preventDefault(); | ||
|
||
try { | ||
// Appel à l'API pour demander une connexion | ||
const response = await fetch( | ||
`${import.meta.env.VITE_BACKEND_URL}/api/items`, | ||
{ | ||
method: "post", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify({ | ||
title: titleRef.current.value, | ||
userId: user.id, | ||
}), | ||
} | ||
); | ||
|
||
// Recharge la page si la création réussit | ||
if (response.status === 201) { | ||
revalidator.revalidate(); | ||
} else { | ||
// Log des détails de la réponse en cas d'échec | ||
console.info(response); | ||
} | ||
} catch (err) { | ||
// Log des erreurs possibles | ||
console.error(err); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{user != null && ( | ||
<form onSubmit={handleSubmit}> | ||
<div> | ||
{/* Champ pour l'title */} | ||
<label htmlFor="title">title</label>{" "} | ||
<input ref={titleRef} type="text" id="title" /> | ||
</div> | ||
{/* Bouton de soumission du formulaire */} | ||
<button type="submit">Send</button> | ||
</form> | ||
)} | ||
<ul> | ||
{items.map(({ id, title }) => ( | ||
<li key={id}>{title}</li> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
} | ||
|
||
export default Home; |