🚧 back to working query params
Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
parent
20aaef67d7
commit
a4c06834e4
3 changed files with 9 additions and 8 deletions
|
@ -4,7 +4,7 @@ import q from 'qjuul'
|
||||||
import { MovieForm, Pagination, MovieTable, MovieModal } from './components'
|
import { MovieForm, Pagination, MovieTable, MovieModal } from './components'
|
||||||
import type { movieObject } from './types/movie'
|
import type { movieObject } from './types/movie'
|
||||||
import { fetchMovie } from './api/fetchMovie'
|
import { fetchMovie } from './api/fetchMovie'
|
||||||
import { useLocation } from 'react-router'
|
import { useLocation } from 'react-router-dom'
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [movies, setMovies] = useState<movieObject[]>([])
|
const [movies, setMovies] = useState<movieObject[]>([])
|
||||||
|
@ -22,6 +22,7 @@ function App() {
|
||||||
if (response.Response == 'True') {
|
if (response.Response == 'True') {
|
||||||
setMovies(response.Search)
|
setMovies(response.Search)
|
||||||
setTotalPages(Number(response.totalResults))
|
setTotalPages(Number(response.totalResults))
|
||||||
|
setCurrentPage(Number(page))
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@ import q from 'qjuul'
|
||||||
import { useNavigateToPage } from '../../util/navigate'
|
import { useNavigateToPage } from '../../util/navigate'
|
||||||
|
|
||||||
const MovieForm: React.FC = () => {
|
const MovieForm: React.FC = () => {
|
||||||
const [movieTitle, setMovieTitle] = useState('')
|
const searchParams = new URLSearchParams(window.location.search)
|
||||||
const [movieYear, setMovieYear] = useState('')
|
const [movieTitle, setMovieTitle] = useState(searchParams.get('title') || '')
|
||||||
const [movieType, setMovieType] = useState('')
|
const [movieYear, setMovieYear] = useState(searchParams.get('y') || '')
|
||||||
|
const [movieType, setMovieType] = useState(searchParams.get('type') || '')
|
||||||
const navigateToPage = useNavigateToPage()
|
const navigateToPage = useNavigateToPage()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -21,6 +22,7 @@ const MovieForm: React.FC = () => {
|
||||||
type="text"
|
type="text"
|
||||||
id="movieTitle"
|
id="movieTitle"
|
||||||
placeholder="Movie title"
|
placeholder="Movie title"
|
||||||
|
value={movieTitle.charAt(0).toUpperCase() + movieTitle.slice(1)}
|
||||||
onChange={(e) => setMovieTitle(e.target.value)}
|
onChange={(e) => setMovieTitle(e.target.value)}
|
||||||
className="border text-black border-gray-300 rounded-md p-2 focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
|
className="border text-black border-gray-300 rounded-md p-2 focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -3,15 +3,13 @@ import { useNavigate } from 'react-router'
|
||||||
export const useNavigateToPage = () => {
|
export const useNavigateToPage = () => {
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
||||||
return (title?: string, page: string = '1', type?: string, year?: string) => {
|
return (title?: string, type?: string, year?: string, page: string = '1') => {
|
||||||
if (!title) navigate('')
|
if (!title) navigate('')
|
||||||
else {
|
else {
|
||||||
let query = `?title=${title}&page=${page}`
|
let query = `?title=${title}&page=${page}`
|
||||||
|
|
||||||
if (type) query += `&type=${type}`
|
|
||||||
if (year) query += `&year=${year}`
|
if (year) query += `&year=${year}`
|
||||||
|
if (type) query += `&type=${type}`
|
||||||
console.log(query)
|
|
||||||
|
|
||||||
navigate(query)
|
navigate(query)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue