From b60f59a48e5a7c6cae328bdbe5b63ee6cb54ff25 Mon Sep 17 00:00:00 2001 From: haraldnilsen Date: Tue, 12 Sep 2023 09:39:11 +0200 Subject: [PATCH] :construction: WIP query params Co-authored-by: Sindre Kjelsrud --- src/App.tsx | 47 ++++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 23 deletions(-) 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 ☝️