Skip to content

Commit

Permalink
set a delay of 1 sec. for Loading
Browse files Browse the repository at this point in the history
with setTimeout
  • Loading branch information
danretegan committed Feb 12, 2024
1 parent 965ec66 commit cc34413
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/pages/Cast.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const Cast = () => {
} catch (error) {
console.error(error);
} finally {
setLoading(false); // Setează loading pe false indiferent de rezultat
// Simulăm o întârziere de 1 secundă înainte de a marca încărcarea ca fiind completă
setTimeout(() => {
setLoading(false);
}, 1000);
}
};

Expand Down
10 changes: 8 additions & 2 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ import { fetchTrendingMovies } from '../services/api';

const Home = () => {
const [trendingMovies, setTrendingMovies] = useState([]);
const [loading, setLoading] = useState(true);

useEffect(() => {
const fetchMovies = async () => {
try {
const movies = await fetchTrendingMovies();
setTrendingMovies(movies);
// Așteaptă 1 secundă înainte de a actualiza starea cu filmele
setTimeout(() => {
setTrendingMovies(movies);
setLoading(false);
}, 1000);
} catch (error) {
console.error('Error fetching trending movies:', error);
setLoading(false);
}
};

Expand All @@ -21,8 +27,8 @@ const Home = () => {
return (
<div>
<h2>Trending today:</h2>
{loading ? <div>Loading...</div> : <MovieList films={trendingMovies} />}
{/* Utilizarea MovieList pentru afișarea listei de filme */}
<MovieList films={trendingMovies} />
</div>
);
};
Expand Down
4 changes: 4 additions & 0 deletions src/pages/Movies.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const Movies = () => {
const handleSearchSubmit = async query => {
try {
setLoading(true);

// Introduce o întârziere de 3 secunde
await new Promise(resolve => setTimeout(resolve, 1000));

const movies = await handleSearch(query);
setSearchResults(movies);
} catch (error) {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Reviews.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const Reviews = () => {
} catch (error) {
console.error(error);
} finally {
setLoading(false); // Setează loading pe false indiferent de rezultat
// Simulăm o întârziere de 1 secundă înainte de a marca încărcarea ca fiind completă
setTimeout(() => {
setLoading(false);
}, 1000);
}
};

Expand Down
10 changes: 8 additions & 2 deletions src/pages/movieDetails/MovieDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import Loader from '../../components/Loader';
const MovieDetails = () => {
const { movieId } = useParams();
const [movieDetails, setMovieDetails] = useState(null);
const [loading, setLoading] = useState(true);
const navigate = useNavigate();

useEffect(() => {
const fetchDetails = async () => {
try {
const details = await fetchMovieDetails(movieId);
setMovieDetails(details);
// Așteaptă 1 secundă înainte de a actualiza starea cu detaliile filmului
setTimeout(() => {
setMovieDetails(details);
setLoading(false);
}, 1000);
} catch (error) {
console.error('Error fetching movie details:', error);
setLoading(false);
}
};

fetchDetails();
}, [movieId]);

if (!movieDetails) {
if (loading) {
return <Loader />;
}

Expand Down

0 comments on commit cc34413

Please sign in to comment.