Row.js
2.1 KB
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
import React, {useState, useEffect} from 'react';
import YouTube from 'react-youtube';
import axios from '../axios';
import '../styles/Row.css';
import movieTrailer from 'movie-trailer'
const base_url = "https://image.tmdb.org/t/p/original/";
function Row({title, fetchUrl, isLargeRow}){
const [movies, setMovies] = useState([]);
const [trailerUrl, setTrailerUrl] = useState();
useEffect(() => {
async function fetchData(){
var page = String(Math.floor(Math.random() * 10) + 1)
fetchUrl = fetchUrl + (isLargeRow ? page : "");
console.log(fetchUrl);
const request = await axios.get(fetchUrl);
setMovies(request.data.results);
return request;
}
fetchData();
}, [fetchUrl])
const opts = {
height: "390",
width: "100%",
playerVars:{
autoplay:1,
},
}
const handleClick = (movie) => {
movieTrailer(movie?.name || movie?.title || movie?.original_title)
.then((url) => {
console.log(url);
const urlParams = new URLSearchParams(new URL(url).search);
if(trailerUrl === urlParams.get('v')){
setTrailerUrl('');
}else{
setTrailerUrl(urlParams.get('v'));
}
})
.catch((error) => console.log(error));
}
return(
<div className='row'>
<div style={{marginLeft:25}}>
<h1>{title}</h1>
</div>
<div className='row_posters'>
{movies.map(movie => (
<img
key={movie.id}
onClick={() => handleClick(movie)}
className={`row_poster ${isLargeRow && 'row_posterLarge'}`}
src={`${base_url}${isLargeRow ? movie.poster_path : movie.poster_path}`}
alt={movie.name}
/>
))}
</div>
{trailerUrl && <YouTube videoId={trailerUrl} opts={opts}/>}
</div>
)
}
export default Row;