diff --git a/src/App.tsx b/src/App.tsx index 47d49ef..2a2d6b2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,6 +5,7 @@ import { MovieForm, Pagination, MovieTable, MovieModal } from './components' import type { movieObject } from './types/movie' import { fetchMovie } from './api/fetchMovie' import { useLocation } from 'react-router-dom' +import { useNavigateToPage } from './util/navigate' function App() { const [movies, setMovies] = useState([]) @@ -13,12 +14,18 @@ function App() { const [totalPages, setTotalPages] = useState(0) const [modalOpen, setModalOpen] = useState(false) const [modalMovie, setModalMovie] = useState(null) + const navigateToPage = useNavigateToPage() + const searchParams = new URLSearchParams(window.location.search) + const [movieTitle, setMovieTitle] = useState(searchParams.get('title') || '') + const [movieYear, setMovieYear] = useState(searchParams.get('year') || '') + const [movieType, setMovieType] = useState(searchParams.get('type') || '') const [sortAscending, setSortAscending] = useState(true) const location = useLocation() useEffect(() => { const handleFetchMovie = async () => { const response = await fetchMovie(title, page, type, year) + console.log(year) if (response.Response == 'True') { setMovies(response.Search) setTotalPages(Number(response.totalResults)) @@ -30,7 +37,7 @@ function App() { const title = searchParams.get('title') || '' const page = searchParams.get('page') || '' const type = searchParams.get('type') || '' - const year = searchParams.get('y') || '' + const year = searchParams.get('year') || '' if (title) { handleFetchMovie() @@ -45,6 +52,7 @@ function App() { const handlePageChange = (pageNumber: number) => { setCurrentPage(pageNumber) + navigateToPage(movieTitle, movieType, movieYear, pageNumber.toString()) } const handleModalOpen = (movie: movieObject) => { @@ -79,8 +87,23 @@ function App() { - All movies - + navigateToPage()} + className="py-8 hover:cursor-pointer text-red-800 text-5xl font-bold" + co="rgb(220 38 38)" + > + Cinemateket + + {!loading && movies.length > 0 && ( response.json()) diff --git a/src/components/MovieForm/index.tsx b/src/components/MovieForm/index.tsx index 5dd5ee3..044542b 100644 --- a/src/components/MovieForm/index.tsx +++ b/src/components/MovieForm/index.tsx @@ -1,12 +1,23 @@ -import { useState } from 'react' import q from 'qjuul' import { useNavigateToPage } from '../../util/navigate' -const MovieForm: React.FC = () => { - const searchParams = new URLSearchParams(window.location.search) - const [movieTitle, setMovieTitle] = useState(searchParams.get('title') || '') - const [movieYear, setMovieYear] = useState(searchParams.get('y') || '') - const [movieType, setMovieType] = useState(searchParams.get('type') || '') +interface MovieFormInterface { + movieTitle: string + movieType: string + movieYear: string + setMovieTitle: (title: string) => void + setMovieType: (type: string) => void + setMovieYear: (year: string) => void +} + +const MovieForm: React.FC = ({ + movieTitle, + movieType, + movieYear, + setMovieTitle, + setMovieType, + setMovieYear, +}) => { const navigateToPage = useNavigateToPage() return ( @@ -14,7 +25,7 @@ const MovieForm: React.FC = () => { className="flex flex-col gap-3 card p-4 rounded-lg w-full lg:px-14" onSubmit={(e) => { e.preventDefault() - navigateToPage(movieTitle, movieYear, movieType) + navigateToPage(movieTitle, movieType, movieYear) }} > Choose a movie title: @@ -28,8 +39,9 @@ const MovieForm: React.FC = () => { /> Choose year movie was made: (OPTIONAL) setMovieYear(e.target.value)} + value={movieYear} > {/* Option for year 1923-2023 */} All years @@ -44,8 +56,9 @@ const MovieForm: React.FC = () => { Choose type: (OPTIONAL) setMovieType(e.target.value)} + value={movieType} > All types Movies diff --git a/src/util/navigate.ts b/src/util/navigate.ts index 5bbb435..74520c6 100644 --- a/src/util/navigate.ts +++ b/src/util/navigate.ts @@ -4,12 +4,14 @@ export const useNavigateToPage = () => { const navigate = useNavigate() return (title?: string, type?: string, year?: string, page: string = '1') => { - if (!title) navigate('') - else { + if (!title) { + navigate('') + window.location.reload() + } else { let query = `?title=${title}&page=${page}` - if (year) query += `&year=${year}` if (type) query += `&type=${type}` + if (year) query += `&year=${year}` navigate(query) }