File tree Expand file tree Collapse file tree 2 files changed +11
-12
lines changed
netflix-create-react-vite-app/src Expand file tree Collapse file tree 2 files changed +11
-12
lines changed Original file line number Diff line number Diff line change 11import React , { useEffect } from 'react' ;
22export const useFetchMovies = (
33 url : string ,
4- setMovies : React . Dispatch < React . SetStateAction < MovieResult [ ] > > ,
5- setLoading : React . Dispatch < React . SetStateAction < boolean > > ,
6- setError : React . Dispatch < React . SetStateAction < string | null > >
4+ setMovies : Dispatch < SetStateAction < MovieResult [ ] > > ,
5+ setLoading : Dispatch < SetStateAction < boolean > > ,
6+ setError : Dispatch < SetStateAction < string | null > >
77) => {
88 useEffect ( ( ) => {
99 const fetchMovies = async ( ) : Promise < void > => {
1010 try {
1111 const response = await fetch ( url ) ;
12+ if ( ! response . ok ) {
13+ throw new Error ( 'Network response was not ok' ) ;
14+ }
1215 const { results } = await response . json ( ) ;
1316 setMovies ( results ) ;
14- } catch ( err ) {
17+ } catch ( error ) {
1518 setError ( 'Failed to fetch movies. Please try again later.' ) ;
1619 } finally {
1720 setLoading ( false ) ;
Original file line number Diff line number Diff line change 11import React , { useState , useEffect , useCallback } from 'react' ;
22import { useTranslation } from 'react-i18next' ;
33import { MainContainer } from './homepage-styles' ;
4- import Card from '../../components/card/card' ;
54import NavbarHeader from '../../components/navbarmenu/navbarheader/navbarHeader' ;
6- import { discoverMovieUrl , imageUrl } from '../../utils/api' ;
5+ import { discoverMovieUrl } from '../../utils/api' ;
76import Spinner from '../../components/spinner/spinner' ;
87import { useFetchMovies } from '../../hooks/useFetchMovies' ;
98import { MovieList } from '../../components/movie-list/movieList' ;
@@ -31,7 +30,6 @@ const Homepage = () => {
3130 setSearchQuery ( newSearchQuery ) ;
3231
3332 if ( ! newSearchQuery ) {
34- setSearchQuery ( '' ) ;
3533 setSearchMovies ( [ ] ) ;
3634 }
3735 } ,
@@ -42,18 +40,16 @@ const Homepage = () => {
4240 < >
4341 < NavbarHeader value = { searchQuery } onChange = { handleSearch } />
4442 < MainContainer aria-label = { t ( 'movie-listings' ) } >
45- { loading && (
43+ { loading ? (
4644 < div >
4745 < Spinner />
4846 < p > { t ( 'loading' ) } </ p >
4947 </ div >
50- ) }
51- { error && (
48+ ) : error ? (
5249 < div >
5350 < p > Error: { error } </ p >
5451 </ div >
55- ) }
56- { ! loading && ! error && (
52+ ) : (
5753 < MovieList
5854 movies = { searchMovies . length > 0 ? searchMovies : results }
5955 />
You can’t perform that action at this time.
0 commit comments