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 }) => { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | const { isLoading, error, data } = useQuery( |
||
0 ignored issues
–
show
|
|||
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 |
||
0 ignored issues
–
show
|
|||
39 | clickable |
||
0 ignored issues
–
show
|
|||
40 | key={id} |
||
0 ignored issues
–
show
|
|||
41 | alt={title} |
||
0 ignored issues
–
show
|
|||
42 | movieId={id} |
||
0 ignored issues
–
show
|
|||
43 | title={title} |
||
0 ignored issues
–
show
|
|||
44 | release={release_date} |
||
0 ignored issues
–
show
|
|||
45 | genres={genre_ids.join(', ')} |
||
0 ignored issues
–
show
|
|||
46 | rating={vote_average} |
||
0 ignored issues
–
show
|
|||
47 | image={moviePoster} |
||
0 ignored issues
–
show
|
|||
48 | isLazy |
||
0 ignored issues
–
show
|
|||
49 | /> |
||
50 | ) |
||
51 | }, |
||
52 | ) |
||
53 | |||
54 | return isLoading ? ( |
||
55 | <LoadingTape /> |
||
0 ignored issues
–
show
|
|||
56 | ) : ( |
||
57 | <> |
||
0 ignored issues
–
show
|
|||
58 | <Helmet link={imageLinksPreload} /> |
||
0 ignored issues
–
show
|
|||
59 | <Grid>{renderedMoviesCards}</Grid> |
||
0 ignored issues
–
show
|
|||
60 | </> |
||
61 | ) |
||
62 | } |
||
63 | |||
64 | export { MoviesCardsGrid } |
||
65 |