diff --git a/src/App.tsx b/src/App.tsx index a6c9932..dd3ef2d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,10 +1,10 @@ -import { useEffect, useState, FormEvent } from 'react' -import './App.css' -import q from 'qjuul' import { MovieForm, Pagination, MovieTable, MovieModal } from './components' -import type { movieObject } from './types/movie' import { useLocation, useNavigate } from 'react-router-dom' +import { useEffect, useState, FormEvent } from 'react' +import type { movieObject } from './types/movie' import { fetchMovie } from './api/fetchMovie' +import q from 'qjuul' +import './App.css' function App() { const [movies, setMovies] = useState([]) @@ -46,25 +46,26 @@ function App() { pageNum = Number(page) } - if (!movieTitle && !movieType && !movieYear) { + if (!title && !type && !year) { + navigate('') setLoading(false) - } + } else { + navigate( + movieYear && movieType + ? `?title=${movieTitle}&type=${movieType}&y=${movieYear}&page=${pageNum}` + : movieYear + ? `?title=${movieTitle}&y=${movieYear}&page=${pageNum}` + : movieYear && movieType + ? `?title=${movieTitle}&type=${movieType}&page=${pageNum}` + : `?title=${movieTitle}&page=${pageNum}` + ) - navigate( - movieYear && movieType - ? `?title=${movieTitle}&type=${movieType}&y=${movieYear}&page=${pageNum}` - : movieYear - ? `?title=${movieTitle}&y=${movieYear}&page=${pageNum}` - : movieYear && movieType - ? `?title=${movieTitle}&type=${movieType}&page=${pageNum}` - : `?title=${movieTitle}&page=${pageNum}` - ) - - const response = await fetchMovie(title, type, year, pageNum) - if (response.Response === 'True') { - setMovies(response.Search) - setTotalPages(response.totalResults) - setLoading(false) + const response = await fetchMovie(title, type, year, pageNum) + if (response.Response === 'True') { + setMovies(response.Search) + setTotalPages(response.totalResults) + setLoading(false) + } } } @@ -120,7 +121,7 @@ function App() { }} /> - {!loading && totalPages && ( + {!loading && totalPages > 0 && ( {loading ? ( Loading... - ) : movies ? ( + ) : movies.length > 0 ? ( ) : ( Find a list of movies by searching above ☝️