This commit is contained in:
Lukian LEIZOUR 2024-06-02 18:26:51 +02:00
parent 4808822058
commit 7f5e401900

View file

@ -11,6 +11,7 @@ export default function Home() {
const [games, setGames] = useState([]); const [games, setGames] = useState([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
const [name, setName] = useState(""); const [name, setName] = useState("");
const [currentPage, setCurrentPage] = useState(1);
useEffect(() => { useEffect(() => {
const tokenLocal = localStorage.getItem("token"); const tokenLocal = localStorage.getItem("token");
@ -45,6 +46,18 @@ export default function Home() {
game.title.toLowerCase().includes(name.toLowerCase()) game.title.toLowerCase().includes(name.toLowerCase())
); );
const itemsPerPage = 10;
const totalPages = Math.ceil(filteredGames.length / itemsPerPage);
function handlePageChange(newPage) {
if (newPage >= 1 && newPage <= totalPages) {
setCurrentPage(newPage);
window.scrollTo(0, 0);
}
}
const currentGames = filteredGames.slice((currentPage - 1) * itemsPerPage, currentPage * itemsPerPage);
function getCode(id) { function getCode(id) {
const alphabet = 'abcdefghijklmnopqrstuvwxyz'; const alphabet = 'abcdefghijklmnopqrstuvwxyz';
const base = alphabet.length; const base = alphabet.length;
@ -71,25 +84,43 @@ export default function Home() {
value={name} value={name}
onChange={handleSearchChange} onChange={handleSearchChange}
/> />
<div className="pagination">
<button onClick={() => handlePageChange(currentPage - 1)} disabled={currentPage === 1}>
Previous
</button>
<span>Page {currentPage} of {totalPages}</span>
<button onClick={() => handlePageChange(currentPage + 1)} disabled={currentPage === totalPages}>
Next
</button>
</div>
{loading ? ( {loading ? (
<div>Loading...</div> <div>Loading...</div>
) : ( ) : (
filteredGames.map((game) => ( currentGames.map((game) => (
<div className='game'> <div className='game'>
<img src={`https://www.myludo.fr/img/jeux/1/300/${getCode(Math.floor(game.id / 1000))}/${game.id}.png`} /> <img src={`https://www.myludo.fr/img/jeux/1/300/${getCode(Math.floor(game.id / 1000))}/${game.id}.png`} />
<h1 className='game-title'>{game.title}</h1> <h1 className='game-title'>{game.title}</h1>
<div className='helpers'> <div className='helpers'>
{(JSON.parse(game.helpers).map((helper) => ( {(JSON.parse(game.helpers).map((helper) => (
helper === user.id ? () => {} : ( helper === user.username ? () => { } : (
<div className='helper'> <div className='helper'>
<h2 className='helper-title'>{helper}</h2> <h2 className='helper-title'>{helper}</h2>
</div> </div>
))))} ))))}
</div> </div>
<HelpButton gameid={game.id} helpingprop={JSON.parse(game.helpers).includes(user.id)} token={token} /> <HelpButton gameid={game.id} helpingprop={JSON.parse(game.helpers).includes(user.id)} token={token} />
</div> </div>
)) ))
)} )}
<div className="pagination">
<button onClick={() => handlePageChange(currentPage - 1)} disabled={currentPage === 1}>
Previous
</button>
<span>Page {currentPage} of {totalPages}</span>
<button onClick={() => handlePageChange(currentPage + 1)} disabled={currentPage === totalPages}>
Next
</button>
</div>
</div> </div>
); );
} }