2023-08-28 16:29:48 +02:00
import { useEffect , useState } from 'react'
2023-08-28 15:40:33 +02:00
import './App.css'
2023-09-04 11:12:35 +02:00
import q from 'qjuul'
2023-09-04 12:00:13 +02:00
import { PaginationButton } from './components'
2023-09-04 15:37:45 +02:00
import Pagination from './components/Pagination'
2023-08-28 15:40:33 +02:00
function App() {
2023-09-04 11:12:35 +02:00
const API_MOVIE_KEY = 'd92949d8'
2023-09-04 10:04:23 +02:00
const [ movies , setMovies ] = useState ( [ ] )
2023-08-28 16:29:48 +02:00
const [ loading , setLoading ] = useState ( true )
2023-09-04 12:00:13 +02:00
const [ currentPage , setCurrentPage ] = useState ( 1 )
const [ totalPages , setTotalPages ] = useState ( 0 )
2023-08-28 16:29:48 +02:00
useEffect ( ( ) = > {
2023-09-05 13:19:25 +02:00
fetch (
` http://www.omdbapi.com/?apikey= ${ API_MOVIE_KEY } &s=spider-man&page= ${ currentPage } `
)
2023-08-28 16:29:48 +02:00
. then ( ( response ) = > response . json ( ) )
. then ( ( data ) = > {
2023-09-04 10:04:23 +02:00
setMovies ( data . Search )
2023-09-04 12:00:13 +02:00
setTotalPages ( data . totalResults )
2023-09-04 10:04:23 +02:00
} )
2023-08-28 16:29:48 +02:00
. then ( ( ) = > setLoading ( false ) )
. catch ( ( error ) = > console . log ( error ) )
2023-09-05 13:19:25 +02:00
} , [ currentPage ] )
2023-08-28 15:40:33 +02:00
2023-09-04 12:00:13 +02:00
const calculatePages = ( totalResults : number ) : number = > {
return Math . round ( totalResults / 10 )
}
2023-09-04 15:49:18 +02:00
const handlePageChange = ( pageNumber : number ) = > {
setCurrentPage ( pageNumber )
}
2023-08-28 15:40:33 +02:00
return (
< >
2023-09-05 14:04:27 +02:00
< q.div className = "flex flex-col justify-center items-center mx-auto w-3/4" >
< q.div className = "flex flex-col w-full items-center" >
2023-09-04 12:00:13 +02:00
< q.h1 > All movies < / q.h1 >
2023-09-05 14:04:27 +02:00
< q.div className = "flex" >
{ ! loading && totalPages && (
< Pagination
currentPage = { currentPage }
totalPages = { calculatePages ( totalPages ) }
handlePageChange = { handlePageChange }
/ >
) }
< / q.div >
2023-09-04 12:00:13 +02:00
< / q.div >
2023-09-04 10:04:23 +02:00
{ ! loading && movies ? (
2023-09-04 12:00:13 +02:00
< q.div >
2023-09-05 14:04:27 +02:00
< q.table className = "border-separate border-spacing-y-5" >
2023-09-04 12:00:13 +02:00
< q.tr >
< q.th > Poster < / q.th >
< q.th > Title < / q.th >
< q.th > Year < / q.th >
< / q.tr >
2023-09-04 11:12:35 +02:00
{ movies . map ( ( movie : any ) = > (
2023-09-05 14:04:27 +02:00
< q.tr className = "card rounded-md" >
< q.td className = "p-2" >
2023-09-04 12:00:13 +02:00
< q.img src = { movie . Poster } alt = { movie . Title } width = "100" / >
< / q.td >
< q.td > { movie . Title } < / q.td >
< q.td > { movie . Year } < / q.td >
< / q.tr >
2023-09-04 11:12:35 +02:00
) ) }
2023-09-04 12:00:13 +02:00
< / q.table >
< / q.div >
2023-09-04 10:04:23 +02:00
) : (
2023-09-04 12:00:13 +02:00
< q.h1 > Loading . . . < / q.h1 >
2023-09-04 10:04:23 +02:00
) }
2023-09-04 11:12:35 +02:00
< / q.div >
2023-08-28 15:40:33 +02:00
< / >
)
}
export default App