commit
This commit is contained in:
parent
8d5a4f1451
commit
a23fa2a709
2 changed files with 49 additions and 24 deletions
|
@ -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);
|
setGames(response.data);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching games:", error);
|
||||||
|
} finally {
|
||||||
setLoading(false);
|
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>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>
|
||||||
</div>
|
);
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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("/")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue