This repository has been archived on 2024-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
Cinemateket/src/App.tsx

78 lines
2.3 KiB
TypeScript
Raw Normal View History

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'
import { PaginationButton } from './components'
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'
const [movies, setMovies] = useState([])
const [loading, setLoading] = useState(true)
const [currentPage, setCurrentPage] = useState(1)
const [totalPages, setTotalPages] = useState(0)
useEffect(() => {
fetch(
`http://www.omdbapi.com/?apikey=${API_MOVIE_KEY}&s=spider-man&page=${currentPage}`
)
.then((response) => response.json())
.then((data) => {
setMovies(data.Search)
setTotalPages(data.totalResults)
})
.then(() => setLoading(false))
.catch((error) => console.log(error))
}, [currentPage])
2023-08-28 15:40:33 +02:00
const calculatePages = (totalResults: number): number => {
return Math.round(totalResults / 10)
}
const handlePageChange = (pageNumber: number) => {
setCurrentPage(pageNumber)
}
2023-08-28 15:40:33 +02:00
return (
<>
<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">
<q.h1>All movies</q.h1>
<q.div className="flex pt-2">
{!loading && totalPages && (
<Pagination
currentPage={currentPage}
totalPages={calculatePages(totalPages)}
handlePageChange={handlePageChange}
/>
)}
</q.div>
</q.div>
{!loading && movies ? (
<q.div>
<q.table className="border-separate border-spacing-y-5">
<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) => (
<q.tr className="card rounded-md">
<q.td className="p-2">
<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
))}
</q.table>
</q.div>
) : (
<q.h1>Loading...</q.h1>
)}
2023-09-04 11:12:35 +02:00
</q.div>
2023-08-28 15:40:33 +02:00
</>
)
}
export default App