diff --git a/src/components/DisplayMovieRow.js b/src/components/DisplayMovieRow.js index 495b774..99443b5 100644 --- a/src/components/DisplayMovieRow.js +++ b/src/components/DisplayMovieRow.js @@ -1,86 +1,58 @@ -import React, { Component } from 'react'; -import { Swiper, SwiperSlide } from 'swiper/react'; -import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper'; +import React, { Component } from 'react' +import { Swiper, SwiperSlide } from 'swiper/react' +import SwiperCore, { Navigation, Pagination, Scrollbar, A11y } from 'swiper' + +import { useViewport } from '../hooks/useViewport' // install Swiper components -SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]); +SwiperCore.use([Navigation, Pagination, Scrollbar, A11y]) -export default class DisplayMovieRow extends Component { - constructor(props) { - super(props); - this.state = { - width: window.innerWidth, - }; - } +const DisplayMovieRow = ({ title, isNetflixMovies, movies }) => { + const [windowDimensions] = useViewport() + const { width } = windowDimensions - componentDidMount() { - window.addEventListener('resize', this.handleResize); - } + return ( + <> +

{title}

+ = 1378 ? 4 : width >= 998 ? 3 : width >= 625 ? 2 : 2 + } + breakpoints={{ + 1378: { + slidesPerView: 5, + slidesPerGroup: 5, + }, + 998: { + slidesPerView: 4, + slidesPerGroup: 4, + }, + 625: { + slidesPerView: 3, + slidesPerGroup: 3, + }, + 0: { + slidesPerView: 2, + slidesPerGroup: 2, + }, + }} + preventClicksPropagation={true} + preventClicks={true} + scrollbar={{ draggable: false, hide: true }} + slideToClickedSlide={false} + pagination={{ clickable: true }} + > + {movies && + movies.map((movie, idx) => { + let movieImageUrl = isNetflixMovies + ? `https://image.tmdb.org/t/p/original/${movie.poster_path}` + : `https://image.tmdb.org/t/p/w500/${movie.backdrop_path}` - componentWillUnMount() { - window.addEventListener('resize', this.handleResize); - } - - handleResize = (e) => { - this.setState({ width: window.innerWidth }); - }; - - render() { - const { width } = this.state; - let netflixUrl = false; - if ( - this.props.url === - `/discover/tv?api_key=${process.env.API_KEY}&with_networks=213` - ) { - netflixUrl = true; - } - - return ( - <> -

{this.props.title}

- = 1378 ? 4 : width >= 998 ? 3 : width >= 625 ? 2 : 2 - } - breakpoints={{ - 1378: { - slidesPerView: 5, - slidesPerGroup: 5, - }, - 998: { - slidesPerView: 4, - slidesPerGroup: 4, - }, - 625: { - slidesPerView: 3, - slidesPerGroup: 3, - }, - 0: { - slidesPerView: 2, - slidesPerGroup: 2, - }, - }} - preventClicksPropagation={true} - preventClicks={true} - scrollbar={{ draggable: false, hide: true }} - slideToClickedSlide={false} - pagination={{ clickable: true }} - > - {this.props.movies && this.props.movies.map((movie, idx) => { - let movieImageUrl = - 'https://image.tmdb.org/t/p/w500/' + movie.backdrop_path; - if ( - this.props.url === - `/discover/tv?api_key=${process.env.API_KEY}&with_networks=213` - ) { - movieImageUrl = - 'https://image.tmdb.org/t/p/original/' + movie.poster_path; - } if (movie.poster_path && movie.backdrop_path !== null) { return ( - ); + ) } })} - - - ); - } +
+ + ) } + +export default DisplayMovieRow diff --git a/src/components/MainContent.js b/src/components/MainContent.js index 6bc7ebc..a8b4353 100644 --- a/src/components/MainContent.js +++ b/src/components/MainContent.js @@ -130,6 +130,7 @@ const MainContent = () => {
diff --git a/src/hooks/useViewport.js b/src/hooks/useViewport.js new file mode 100644 index 0000000..2c129eb --- /dev/null +++ b/src/hooks/useViewport.js @@ -0,0 +1,39 @@ +import { useState, useEffect } from 'react' + +const height = + window.innerHeight || + document.documentElement.clientHeight || + document.body.clientHeight +const width = + window.innerWidth || + document.documentElement.clientWidth || + document.body.clientWidth + +export const useViewport = () => { + const [windowDimensions, setWindowDimensions] = useState({ height, width }) + + const deriveWindowDimensions = () => { + const height = + window.innerHeight || + document.documentElement.clientHeight || + document.body.clientHeight + + const width = + window.innerWidth || + document.documentElement.clientWidth || + document.body.clientWidth + + setWindowDimensions({ height, width }) + } + + useEffect(() => { + deriveWindowDimensions() + window.addEventListener('resize', deriveWindowDimensions) + + return () => { + window.removeEventListener('resize', deriveWindowDimensions) + } + }, []) + + return [windowDimensions] +}