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-06 10:50:19 +02:00
|
|
|
|
import { MovieForm, Pagination, MovieTable, MovieModal } from './components'
|
2023-09-05 15:54:36 +02:00
|
|
|
|
import type { movieObject } from './types/movie'
|
2023-09-12 11:10:57 +02:00
|
|
|
|
import { fetchMovie } from './api/fetchMovie'
|
2023-09-12 13:22:16 +02:00
|
|
|
|
import { useLocation } from 'react-router-dom'
|
2023-08-28 15:40:33 +02:00
|
|
|
|
|
|
|
|
|
function App() {
|
2023-09-06 07:33:28 +02:00
|
|
|
|
const [movies, setMovies] = useState<movieObject[]>([])
|
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-09-05 15:54:36 +02:00
|
|
|
|
const [modalOpen, setModalOpen] = useState(false)
|
|
|
|
|
const [modalMovie, setModalMovie] = useState<movieObject | null>(null)
|
2023-09-06 07:33:28 +02:00
|
|
|
|
const [sortAscending, setSortAscending] = useState(true)
|
2023-09-12 11:10:57 +02:00
|
|
|
|
const location = useLocation()
|
2023-09-05 16:25:10 +02:00
|
|
|
|
|
2023-08-28 16:29:48 +02:00
|
|
|
|
useEffect(() => {
|
2023-09-12 11:10:57 +02:00
|
|
|
|
const handleFetchMovie = async () => {
|
|
|
|
|
const response = await fetchMovie(title, page, type, year)
|
2023-09-12 11:53:12 +02:00
|
|
|
|
if (response.Response == 'True') {
|
2023-09-12 11:10:57 +02:00
|
|
|
|
setMovies(response.Search)
|
|
|
|
|
setTotalPages(Number(response.totalResults))
|
2023-09-12 13:22:16 +02:00
|
|
|
|
setCurrentPage(Number(page))
|
2023-09-12 11:10:57 +02:00
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-12 11:53:12 +02:00
|
|
|
|
const searchParams = new URLSearchParams(location.search)
|
|
|
|
|
const title = searchParams.get('title') || ''
|
|
|
|
|
const page = searchParams.get('page') || ''
|
|
|
|
|
const type = searchParams.get('type') || ''
|
|
|
|
|
const year = searchParams.get('y') || ''
|
2023-09-12 11:10:57 +02:00
|
|
|
|
|
|
|
|
|
if (title) {
|
|
|
|
|
handleFetchMovie()
|
2023-09-12 11:53:12 +02:00
|
|
|
|
} else {
|
|
|
|
|
setLoading(false)
|
2023-09-12 11:10:57 +02:00
|
|
|
|
}
|
2023-09-12 12:04:41 +02:00
|
|
|
|
}, [location.search])
|
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-09-05 15:54:36 +02:00
|
|
|
|
const handleModalOpen = (movie: movieObject) => {
|
|
|
|
|
setModalOpen(true)
|
|
|
|
|
setModalMovie(movie)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-06 07:33:28 +02:00
|
|
|
|
const sortHandler = (sortType: string) => {
|
|
|
|
|
let sortedMovies: movieObject[] = []
|
|
|
|
|
|
|
|
|
|
if (sortType === 'title') {
|
|
|
|
|
sortedMovies = [...movies].sort((m1, m2) =>
|
|
|
|
|
sortAscending
|
|
|
|
|
? m1.Title.localeCompare(m2.Title)
|
|
|
|
|
: m2.Title.localeCompare(m1.Title)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if (sortType === 'year') {
|
|
|
|
|
sortedMovies = [...movies].sort((m1, m2) =>
|
|
|
|
|
sortAscending
|
|
|
|
|
? m1.Year.localeCompare(m2.Year)
|
|
|
|
|
: m2.Year.localeCompare(m1.Year)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setSortAscending(!sortAscending)
|
|
|
|
|
setMovies(sortedMovies)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 15:40:33 +02:00
|
|
|
|
return (
|
|
|
|
|
<>
|
2023-09-06 10:53:59 +02:00
|
|
|
|
<q.div className="flex flex-col justify-center items-center mx-auto w-3/4 md:w-3/5 lg:w-2/4">
|
2023-09-06 07:56:46 +02:00
|
|
|
|
<MovieModal {...{ setModalOpen, modalMovie, modalOpen }} />
|
2023-09-05 14:04:27 +02:00
|
|
|
|
<q.div className="flex flex-col w-full items-center">
|
2023-09-05 17:07:23 +02:00
|
|
|
|
<q.h1 className="py-4">All movies</q.h1>
|
2023-09-12 11:53:12 +02:00
|
|
|
|
<MovieForm />
|
2023-09-05 17:07:23 +02:00
|
|
|
|
<q.div className="flex pt-10">
|
2023-09-12 11:53:12 +02:00
|
|
|
|
{!loading && movies.length > 0 && (
|
2023-09-05 14:04:27 +02:00
|
|
|
|
<Pagination
|
|
|
|
|
currentPage={currentPage}
|
|
|
|
|
totalPages={calculatePages(totalPages)}
|
|
|
|
|
handlePageChange={handlePageChange}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</q.div>
|
2023-09-06 11:09:23 +02:00
|
|
|
|
{loading ? (
|
|
|
|
|
<q.h1>Loading...</q.h1>
|
2023-09-12 11:53:12 +02:00
|
|
|
|
) : movies.length > 0 ? (
|
2023-09-06 07:56:46 +02:00
|
|
|
|
<MovieTable {...{ handleModalOpen, sortHandler, movies }} />
|
2023-09-05 17:07:23 +02:00
|
|
|
|
) : (
|
2023-09-06 11:09:23 +02:00
|
|
|
|
<q.h2>Find a list of movies by searching above ☝️ </q.h2>
|
2023-09-05 17:07:23 +02:00
|
|
|
|
)}
|
2023-09-04 12:00:13 +02:00
|
|
|
|
</q.div>
|
2023-09-04 11:12:35 +02:00
|
|
|
|
</q.div>
|
2023-08-28 15:40:33 +02:00
|
|
|
|
</>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default App
|