working query params from url - not form

Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
haraldnilsen 2023-09-12 11:53:12 +02:00
parent 18c85a662a
commit cd63407d03
4 changed files with 33 additions and 49 deletions

View file

@ -17,23 +17,24 @@ function App() {
const location = useLocation() const location = useLocation()
useEffect(() => { useEffect(() => {
const handleFetchMovie = async () => {
const response = await fetchMovie(title, page, type, year)
if (response.Response == 'True') {
setMovies(response.Search)
setTotalPages(Number(response.totalResults))
setLoading(false)
}
}
const searchParams = new URLSearchParams(location.search) const searchParams = new URLSearchParams(location.search)
const title = searchParams.get('title') || '' const title = searchParams.get('title') || ''
const page = searchParams.get('page') || '' const page = searchParams.get('page') || ''
const type = searchParams.get('type') || '' const type = searchParams.get('type') || ''
const year = searchParams.get('y') || '' const year = searchParams.get('y') || ''
const handleFetchMovie = async () => {
const response = await fetchMovie(title, page, type, year)
if (response.Response == 'true') {
setMovies(response.Search)
setTotalPages(Number(response.totalResults))
setLoading(false)
}
}
if (title) { if (title) {
handleFetchMovie() handleFetchMovie()
} else {
setLoading(false)
} }
}, []) }, [])
@ -78,16 +79,9 @@ function App() {
<MovieModal {...{ setModalOpen, modalMovie, modalOpen }} /> <MovieModal {...{ setModalOpen, modalMovie, modalOpen }} />
<q.div className="flex flex-col w-full items-center"> <q.div className="flex flex-col w-full items-center">
<q.h1 className="py-4">All movies</q.h1> <q.h1 className="py-4">All movies</q.h1>
<MovieForm <MovieForm />
{...{
handleMovieSubmit,
setMovieTitle,
setMovieType,
setMovieYear,
}}
/>
<q.div className="flex pt-10"> <q.div className="flex pt-10">
{!loading && totalPages && ( {!loading && movies.length > 0 && (
<Pagination <Pagination
currentPage={currentPage} currentPage={currentPage}
totalPages={calculatePages(totalPages)} totalPages={calculatePages(totalPages)}
@ -97,7 +91,7 @@ function App() {
</q.div> </q.div>
{loading ? ( {loading ? (
<q.h1>Loading...</q.h1> <q.h1>Loading...</q.h1>
) : movies ? ( ) : movies.length > 0 ? (
<MovieTable {...{ handleModalOpen, sortHandler, movies }} /> <MovieTable {...{ handleModalOpen, sortHandler, movies }} />
) : ( ) : (
<q.h2>Find a list of movies by searching above </q.h2> <q.h2>Find a list of movies by searching above </q.h2>

View file

@ -1,6 +1,6 @@
import { movieResponse } from '../types/movie' import { movieResponse } from '../types/movie'
const API_MOVIE_KEY = 'd92949d8' const API_MOVIE_KEY = import.meta.env.VITE_API_MOVIE_KEY
export const fetchMovie = async ( export const fetchMovie = async (
title: string, title: string,
@ -8,7 +8,7 @@ export const fetchMovie = async (
type?: string, type?: string,
year?: string year?: string
): Promise<movieResponse> => { ): Promise<movieResponse> => {
let query = `http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}s=${title}&page=${page}` let query = `http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=${title}&page=${page}`
if (type) query += `&type=${type}` if (type) query += `&type=${type}`
if (year) query += `&year=${year}` if (year) query += `&year=${year}`

View file

@ -1,24 +1,17 @@
import type { movieObject } from '../../types/movie' import { useState } from 'react'
import Modal from 'react-modal'
import q from 'qjuul' import q from 'qjuul'
import { useNavigateToPage } from '../../util/navigate'
interface MovieFormProps { const MovieForm: React.FC = () => {
handleMovieSubmit: (event: any) => void const [movieTitle, setMovieTitle] = useState('')
setMovieTitle: (title: string) => void const [movieYear, setMovieYear] = useState('')
setMovieYear: (year: string) => void const [movieType, setMovieType] = useState('')
setMovieType: (type: string) => void const navigateToPage = useNavigateToPage()
}
const MovieForm: React.FC<MovieFormProps> = ({
handleMovieSubmit,
setMovieTitle,
setMovieType,
setMovieYear,
}) => {
return ( return (
<q.form <q.form
className="flex flex-col gap-3 card p-4 rounded-lg w-full lg:px-14" className="flex flex-col gap-3 card p-4 rounded-lg w-full lg:px-14"
onSubmit={handleMovieSubmit} onSubmit={() => navigateToPage(movieTitle, movieYear, movieType)}
> >
<q.label>Choose a movie title:</q.label> <q.label>Choose a movie title:</q.label>
<q.input <q.input

View file

@ -1,13 +1,9 @@
import { useNavigate } from 'react-router' import { useNavigate } from 'react-router'
const navigate = useNavigate() export const useNavigateToPage = () => {
const navigate = useNavigate()
export const navigateToPage = ( return (title?: string, page: string = '1', type?: string, year?: string) => {
title?: string,
page: string = '1',
type?: string,
year?: string
) => {
if (!title) navigate('') if (!title) navigate('')
else { else {
let query = `?title=${title}&page=${page}` let query = `?title=${title}&page=${page}`
@ -17,4 +13,5 @@ export const navigateToPage = (
navigate(query) navigate(query)
} }
}
} }