1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131import React, { useEffect, useRef, useState } from "react";
import styled from "styled-components";
import {
CarouselProvider,
Slider,
Slide,
ButtonNext,
ButtonBack,
} from "pure-react-carousel";
import "pure-react-carousel/dist/react-carousel.es.css";
import { Link } from "react-router-dom";
const HomeSliderBlock = styled.div`
width: 100%;
height: 100%;
display: flex;
position: relative;
`;
const Backdrop = styled.div`
position: absolute;
top: 0;
left: 0;
z-index: 0;
width: 100%;
height: 100%;
background: url(${(props) => props.bgUrl});
background-size: cover;
opacity: 0.8;
`;
const Info = styled.div`
position: absolute;
bottom: 100px;
left: 30px;
display: flex;
flex-direction: column;
@media (max-width: 1080px) {
bottom: 30px;
}
`;
const Title = styled.div`
color: white;
font-size: 60px;
font-weight: 500;
margin-bottom: 20px;
`;
const Button = styled(Link)`
padding: 10px 20px;
width: fit-content;
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
margin-top: 25px;
background: white;
color: black;
font-size: 20px;
`;
const HomeSlider = ({ nowPlaying, isMovie }) => {
const carousel = useRef();
const [handler, setHandler] = useState(null);
const settings = {
ref: carousel,
visibleSlides: 1,
totalSlides: nowPlaying.length,
naturalSlideWidth: 100,
naturalSlideHeight: 50,
infinite: true,
step: 1,
};
useEffect(() => {
if (handler) clearInterval(handler);
setHandler(
setInterval(() => {
if (carousel.current) {
const {
setStoreState,
getStoreState,
} = carousel.current.carouselStore;
const { currentSlide } = getStoreState();
const nextSlide =
currentSlide + 1 > nowPlaying.length ? 0 : currentSlide + 1;
setStoreState({
currentSlide: nextSlide,
});
}
}, 5000)
);
return () => {
clearInterval(handler);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<CarouselProvider {...settings}>
<Slider>
{nowPlaying.map((now, idx) => (
<Slide key={now.id} index={idx}>
<HomeSliderBlock>
<Backdrop
bgUrl={
now.backdrop_path &&
`https://image.tmdb.org/t/p/original${now.backdrop_path}`
}
/>
<Info>
<Title>
{isMovie ? now.original_title : now.original_name}
</Title>
<Button to={isMovie ? `movie/${now.id}` : `tv/${now.id}`}>
More
</Button>
</Info>
</HomeSliderBlock>
</Slide>
))}
</Slider>
</CarouselProvider>
);
};
export default HomeSlider;