Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ajout loader et modif pour affichage personnages avec images #27

Merged
merged 6 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/pictures/hammer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/components/loader.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '../style/loader.scss';

export default function Loader() {
return (
<div className='load'>
<img src='/pictures/hammer.png' alt='' /> <p> Loading..... </p>
</div>
);
}
36 changes: 28 additions & 8 deletions src/components/marvel-characters.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useState } from 'react';
import Card from '../components/card';
import Modal from '../components/modal';
import Profile from '../components/profile';
import Loader from './loader';

const publicKey = import.meta.env.VITE_PUBLIC_KEY;

Expand All @@ -12,25 +13,42 @@ export default function MarvelCharacters({ addToFavorites }) {
const [isModalOpen, setIsModalOpen] = useState(false);
const [comics, setComics] = useState([]);
const [currentPage, setCurrentPage] = useState(0);
const charactersPerPage = 20;
const [loading, setLoading] = useState(true);
const [err, setError] = useState(null);
const charactersPerPage = 15;

useEffect(() => {
const apiComics = `https://gateway.marvel.com/v1/public/characters?apikey=${publicKey}&limit=100`;
const apiComics = `https://gateway.marvel.com/v1/public/characters?apikey=${publicKey}&limit=40`;

fetch(apiComics)
.then((res) => {
if (!res.ok) throw new Error('Erreur réseau');
return res.json();
})
.then((data) => {
const randomCharacters = data.data.results.sort(() => Math.random() - 1);
setMarvelCharacter(randomCharacters);
const filteredCharacters = data.data.results.filter(
(marvel) =>
marvel.thumbnail &&
marvel.thumbnail.path &&
marvel.thumbnail.extension &&
!marvel.thumbnail.path.includes('image_not_available'),
);
setLoading(false);
setMarvelCharacter(filteredCharacters);
})
// eslint-disable-next-line no-console
.catch((err) => console.error(err));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [MarvelCharacters]);

.catch((err) => {
setError(err);
setLoading(false);
});
}, []);

if (loading)
return (
<div>
<Loader />
</div>
);
const handleAddToFavorites = (character) => {
addToFavorites(character);
};
Expand Down Expand Up @@ -84,6 +102,8 @@ export default function MarvelCharacters({ addToFavorites }) {
)
.slice(currentPage * charactersPerPage, (currentPage + 1) * charactersPerPage);

if (err) return <div>Error: {err.message}</div>;

return (
<div className='characters-comics'>
<div className='pagination'>
Expand Down
29 changes: 29 additions & 0 deletions src/style/loader.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@use 'settings' as *;

@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

.load {
display: flex;
flex-direction: column;
align-items: center;

p {
padding-bottom: 20px;
color: white;
font-size: 50px;

font-family: $font-family;
}
img {
width: 150px;
animation: spin 1s linear infinite;
padding-top: 20px;
}
}