diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index ed6a6ad..ca520fc 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -4,34 +4,52 @@ import axios from "axios"; export default function Home() { const navigate = useNavigate(); - const [games, setGames] = useState({}); + const [games, setGames] = useState([]); const [loading, setLoading] = useState(true); + const [name, setName] = useState(""); useEffect(() => { const token = localStorage.getItem("token"); if (!token) { - navigate("/login") + navigate("/login"); + return; } async function fetchGames() { setLoading(true); - const response = await axios.post("http://localhost:3000/api/v1/games/getall", { - token: token - }); - - setGames(response.data); - setLoading(false); + try { + const response = await axios.post("http://localhost:3000/api/v1/games/getall", { token }); + setGames(response.data); + } catch (error) { + console.error("Error fetching games:", error); + } finally { + setLoading(false); + } } fetchGames(); - }, []); + }, [navigate]); - if (loading) { - return
Loading...
+ const handleSearchChange = (event) => { + setName(event.target.value); + }; + + const filteredGames = games.filter((game) => + game.title.toLowerCase().includes(name.toLowerCase()) + ); + + function getCode(id) { + const alphabet = 'abcdefghijklmnopqrstuvwxyz'; + const base = alphabet.length; + let firstLetterIndex = Math.floor(id / base); + let secondLetterIndex = id % base; + + let firstLetter = alphabet[firstLetterIndex]; + let secondLetter = alphabet[secondLetterIndex]; + + return firstLetter + secondLetter; } - console.log(games); - return (
@@ -41,13 +59,21 @@ export default function Home() { navigate("/login"); }}>Logout
-
- { - games.map((game) => ( -
{game.title}
- )) - } -
+ Recherche : + {loading ? ( +
Loading...
+ ) : ( + filteredGames.map((game) => ( +
+ + {game.title} +
+ )) + )}
- ) + ); } diff --git a/src/pages/Register.jsx b/src/pages/Register.jsx index 9d90459..79717b3 100644 --- a/src/pages/Register.jsx +++ b/src/pages/Register.jsx @@ -6,7 +6,6 @@ export default function Register() { const navigate = useNavigate(); useEffect(() => { - if (localStorage.getItem("token")) { navigate("/") } @@ -14,8 +13,8 @@ export default function Register() { async function register() { const username = document.getElementById("username").value; - const name = document.getElementById("name").value; - const lastname = document.getElementById("lastname").value; + const name = document.getElementById("name").value; + const lastname = document.getElementById("lastname").value; const password = document.getElementById("password").value; if (!username || !name || !lastname || !password) {