🚧 still working on query params

Co-authored-by: Sindre Kjelsrud <kjelsrudsindre@gmail.com>
This commit is contained in:
haraldnilsen 2023-09-11 13:26:43 +02:00
parent a87cfe4d7b
commit cc2979bace
3 changed files with 41 additions and 25 deletions

View file

@ -1,12 +1,12 @@
import { useEffect, useState } from 'react' import { useEffect, useState, FormEvent } from 'react'
import './App.css' import './App.css'
import q from 'qjuul' 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 { useLocation, useNavigate } from 'react-router-dom' import { useLocation, useNavigate } from 'react-router-dom'
import { fetchMovie } from './api/fetchMovie'
function App() { function App() {
const API_MOVIE_KEY = 'd92949d8'
const [movies, setMovies] = useState<movieObject[]>([]) const [movies, setMovies] = useState<movieObject[]>([])
const [loading, setLoading] = useState(true) const [loading, setLoading] = useState(true)
const [currentPage, setCurrentPage] = useState(1) const [currentPage, setCurrentPage] = useState(1)
@ -28,13 +28,13 @@ function App() {
useEffect(() => { useEffect(() => {
handleMovieSubmit() handleMovieSubmit()
}, [currentPage, title, type, year]) }, [title, type, year])
const handleMovieSubmit = ({ const handleMovieSubmit = async ({
event, event,
pageNumber, pageNumber,
}: { }: {
event?: any event?: FormEvent
pageNumber?: number pageNumber?: number
} = {}) => { } = {}) => {
if (event) event.preventDefault() if (event) event.preventDefault()
@ -50,26 +50,22 @@ function App() {
setLoading(false) setLoading(false)
} }
// navigate( navigate(
// movieYear && movieType movieYear && movieType
// ? `?title=${movieTitle}&type=${movieType}&y=${movieYear}&page=${currentPage}` ? `?title=${movieTitle}&type=${movieType}&y=${movieYear}&page=${pageNum}`
// : movieYear : movieYear
// ? `?title=${movieTitle}&y=${movieYear}&page=${currentPage}` ? `?title=${movieTitle}&y=${movieYear}&page=${pageNum}`
// : movieYear && movieType : movieYear && movieType
// ? `?title=${movieTitle}&type=${movieType}&page=${currentPage}` ? `?title=${movieTitle}&type=${movieType}&page=${pageNum}`
// : `?title=${movieTitle}&page=${currentPage}` : `?title=${movieTitle}&page=${pageNum}`
// )
fetch(
`http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=${title}&type=${type}&y=${year}&page=${pageNum}`
) )
.then((response) => response.json())
.then((data) => { const response = await fetchMovie(title, type, year, pageNum)
setMovies(data.Search) if (response.Response === 'True') {
setTotalPages(data.totalResults) setMovies(response.Search)
}) setTotalPages(response.totalResults)
.then(() => setLoading(false)) setLoading(false)
.catch((error) => console.log(error)) }
} }
const calculatePages = (totalResults: number): number => { const calculatePages = (totalResults: number): number => {

20
src/api/fetchMovie.ts Normal file
View file

@ -0,0 +1,20 @@
const API_MOVIE_KEY = import.meta.env.VITE_MOVIE_API_KEY
export const fetchMovie = async (
title: string,
type: string,
year: string,
pageNum: number
) => {
const response = await fetch(
`http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=${title}&type=${type}&y=${year}&page=${pageNum}`
)
.then((response) => response.json())
.then((data) => {
return data
})
.catch((error) => {
console.error('Error:', error)
})
return response
}

View file

@ -23,7 +23,7 @@ const Pagination: React.FC<PaginationProps> = ({
setShowRightDots(currentPage < totalPages - 3 ? true : false) setShowRightDots(currentPage < totalPages - 3 ? true : false)
} }
if (!showLeftDots && !showRightDots) { if (!showLeftDots && !showRightDots) {
setShowingNumbers(Array.from(Array(totalPages).keys())) setShowingNumbers(Array.from(Array(totalPages).keys()).map((n) => n + 1))
} }
if (!showLeftDots && showRightDots) { if (!showLeftDots && showRightDots) {
setShowingNumbers([1, 2, 3, 4, 5]) setShowingNumbers([1, 2, 3, 4, 5])