This commit is contained in:
Lukian LEIZOUR 2024-05-31 22:18:30 +02:00
parent 8d5a4f1451
commit a23fa2a709
2 changed files with 49 additions and 24 deletions

View file

@ -4,34 +4,52 @@ import axios from "axios";
export default function Home() { export default function Home() {
const navigate = useNavigate(); const navigate = useNavigate();
const [games, setGames] = useState({}); const [games, setGames] = useState([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [name, setName] = useState("");
useEffect(() => { useEffect(() => {
const token = localStorage.getItem("token"); const token = localStorage.getItem("token");
if (!token) { if (!token) {
navigate("/login") navigate("/login");
return;
} }
async function fetchGames() { async function fetchGames() {
setLoading(true); setLoading(true);
const response = await axios.post("http://localhost:3000/api/v1/games/getall", { try {
token: token const response = await axios.post("http://localhost:3000/api/v1/games/getall", { token });
}); setGames(response.data);
} catch (error) {
setGames(response.data); console.error("Error fetching games:", error);
setLoading(false); } finally {
setLoading(false);
}
} }
fetchGames(); fetchGames();
}, []); }, [navigate]);
if (loading) { const handleSearchChange = (event) => {
return <div>Loading...</div> 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 ( return (
<div> <div>
<div> <div>
@ -41,13 +59,21 @@ export default function Home() {
navigate("/login"); navigate("/login");
}}>Logout</button> }}>Logout</button>
</div> </div>
<div> Recherche : <input
{ type="text"
games.map((game) => ( value={name}
<div key={game.id}>{game.title}</div> onChange={handleSearchChange}
)) />
} {loading ? (
</div> <div>Loading...</div>
) : (
filteredGames.map((game) => (
<div key={game.id}>
<img src={`https://www.myludo.fr/img/jeux/1/300/${getCode(Math.floor(game.id / 1000))}/${game.id}.png`} />
{game.title}
</div>
))
)}
</div> </div>
) );
} }

View file

@ -6,7 +6,6 @@ export default function Register() {
const navigate = useNavigate(); const navigate = useNavigate();
useEffect(() => { useEffect(() => {
if (localStorage.getItem("token")) { if (localStorage.getItem("token")) {
navigate("/") navigate("/")
} }
@ -14,8 +13,8 @@ export default function Register() {
async function register() { async function register() {
const username = document.getElementById("username").value; const username = document.getElementById("username").value;
const name = document.getElementById("name").value; const name = document.getElementById("name").value;
const lastname = document.getElementById("lastname").value; const lastname = document.getElementById("lastname").value;
const password = document.getElementById("password").value; const password = document.getElementById("password").value;
if (!username || !name || !lastname || !password) { if (!username || !name || !lastname || !password) {