1
|
|
|
import { FC } from 'react'
|
2
|
|
|
import { useQuery } from 'react-query'
|
3
|
|
|
import { Helmet } from 'react-helmet'
|
4
|
|
|
import { fetchMoviesList } from './model'
|
5
|
|
|
import { Grid, LoadingTape, Thumb } from 'shared/ui'
|
6
|
|
|
import { IMAGE_THUMB } from 'shared/config/images'
|
7
|
|
|
|
8
|
|
|
import NoImage from 'shared/assets/images/no_image.jpg'
|
9
|
|
|
|
10
|
|
|
interface IProps {
|
11
|
|
|
currentPage: number
|
12
|
|
|
}
|
13
|
|
|
|
14
|
|
|
const MoviesCardsGrid: FC<IProps> = ({ currentPage }) => {
|
|
|
|
|
15
|
|
|
const { isLoading, error, data } = useQuery(
|
|
|
|
|
16
|
|
|
['moviesList', currentPage],
|
17
|
|
|
() => fetchMoviesList(currentPage),
|
18
|
|
|
{
|
19
|
|
|
keepPreviousData: true,
|
20
|
|
|
},
|
21
|
|
|
)
|
22
|
|
|
|
23
|
|
|
const IMAGE_URL = import.meta.env.APP_IMAGE_URL
|
24
|
|
|
|
25
|
|
|
const imageLinksPreload = data?.movies?.map(({ poster_path }) => ({
|
26
|
|
|
href: `${IMAGE_URL}/${IMAGE_THUMB.L}${poster_path}`,
|
27
|
|
|
rel: 'preload',
|
28
|
|
|
as: 'image',
|
29
|
|
|
}))
|
30
|
|
|
|
31
|
|
|
const renderedMoviesCards = data?.movies?.map(
|
32
|
|
|
({ genre_ids, id, title, poster_path, release_date, vote_average }) => {
|
33
|
|
|
const moviePoster = poster_path
|
34
|
|
|
? `${IMAGE_URL}/${IMAGE_THUMB.L}${poster_path}`
|
35
|
|
|
: NoImage
|
36
|
|
|
|
37
|
|
|
return (
|
38
|
|
|
<Thumb
|
|
|
|
|
39
|
|
|
clickable
|
|
|
|
|
40
|
|
|
key={id}
|
|
|
|
|
41
|
|
|
alt={title}
|
|
|
|
|
42
|
|
|
movieId={id}
|
|
|
|
|
43
|
|
|
title={title}
|
|
|
|
|
44
|
|
|
release={release_date}
|
|
|
|
|
45
|
|
|
genres={genre_ids.join(', ')}
|
|
|
|
|
46
|
|
|
rating={vote_average}
|
|
|
|
|
47
|
|
|
image={moviePoster}
|
|
|
|
|
48
|
|
|
isLazy
|
|
|
|
|
49
|
|
|
/>
|
50
|
|
|
)
|
51
|
|
|
},
|
52
|
|
|
)
|
53
|
|
|
|
54
|
|
|
return isLoading ? (
|
55
|
|
|
<LoadingTape />
|
|
|
|
|
56
|
|
|
) : (
|
57
|
|
|
<>
|
|
|
|
|
58
|
|
|
<Helmet link={imageLinksPreload} />
|
|
|
|
|
59
|
|
|
<Grid>{renderedMoviesCards}</Grid>
|
|
|
|
|
60
|
|
|
</>
|
61
|
|
|
)
|
62
|
|
|
}
|
63
|
|
|
|
64
|
|
|
export { MoviesCardsGrid }
|
65
|
|
|
|