-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
403 additions
and
696 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,192 +1,237 @@ | ||
import { throttle } from 'lodash'; | ||
import React from 'react'; | ||
|
||
import { ReactComponent as LeftArrow } from '../assets/svg/back.svg'; | ||
import { ReactComponent as RightArrow } from '../assets/svg/forward.svg'; | ||
|
||
// import CarouselLoading from "./CarouselLoading"; | ||
|
||
class Carousel extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
offset: 0, | ||
pos: 0, | ||
init: false, | ||
width: false, | ||
inView: false, | ||
}; | ||
|
||
this.carouselRef = React.createRef(); | ||
this.wrapper = React.createRef(); | ||
this.next = this.next.bind(this); | ||
this.prev = this.prev.bind(this); | ||
this.init = this.init.bind(this); | ||
this.scroll = throttle(this.scroll.bind(this), 1000); | ||
this.isInViewport = throttle(this.isInViewport.bind(this), 1000); | ||
} | ||
|
||
componentDidMount() { | ||
let page = document.querySelectorAll('.page-wrap')[0]; | ||
page.scrollTop = 0; | ||
window.scrollTo(0, 0); | ||
this.init(); | ||
window.addEventListener('resize', this.init); | ||
page.addEventListener('scroll', this.isInViewport); | ||
this.isInViewport(); | ||
import { useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { Link, useHistory } from 'react-router-dom'; | ||
|
||
import { ReactComponent as Chevron } from '../assets/svg/chevron.svg'; | ||
import { storePosition } from '../services/position.service'; | ||
import cards from '../styles/components/card.module.scss'; | ||
import carousel from '../styles/components/carousel.module.scss'; | ||
import typo from '../styles/components/typography.module.scss'; | ||
// import { useRouter } from 'next/router'; | ||
import Card from './card'; | ||
|
||
const mapStateToProps = (state) => { | ||
return { | ||
redux_pos: state.pos.pages, | ||
}; | ||
}; | ||
|
||
function Carousel({ | ||
data, | ||
title, | ||
type = 'movie', | ||
id, | ||
redux_pos, | ||
link, | ||
newNotification, | ||
}) { | ||
const [waitForScroll, setWaitForScroll] = useState(true); | ||
const [end, setEnd] = useState(false); | ||
const [start, setStart] = useState(false); | ||
const placeholderRow = []; | ||
const rowId = id; | ||
// const router = useRouter(); | ||
const track = useRef(null); | ||
const history = useHistory(); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
placeholderRow.push( | ||
<div className={cards.wrap} key={`${rowId}_${i}`}> | ||
<div | ||
className={ | ||
type === 'company' || type === 'request' | ||
? cards.placeholder__wide | ||
: cards.placeholder | ||
} | ||
></div> | ||
</div>, | ||
); | ||
} | ||
|
||
componentDidUpdate(prevProps) { | ||
if (this.props !== prevProps) { | ||
this.init(); | ||
} else if (this.state.inView && !this.state.init) { | ||
this.init(); | ||
useLayoutEffect(() => { | ||
if (!track || !track.current || !data || data.length === 0) return; | ||
const el = track.current; | ||
updateControls(); | ||
let bounce = false; | ||
function checkScroll() { | ||
clearTimeout(bounce); | ||
updateControls(); | ||
bounce = setTimeout(() => { | ||
storePosition(history.location.pathname, false, { | ||
[id]: { scroll: el.scrollLeft }, | ||
}); | ||
}, 500); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
window.removeEventListener('resize', this.init); | ||
window.removeEventListener('resize', this.isInViewport); | ||
} | ||
|
||
init() { | ||
if (!this.state.inView) { | ||
return; | ||
function updateControls() { | ||
if ( | ||
track.current.scrollLeft + track.current.offsetWidth >= | ||
track.current.scrollWidth | ||
) { | ||
setEnd(true); | ||
} else { | ||
setEnd(false); | ||
} | ||
if (track.current.scrollLeft < 1) { | ||
setStart(true); | ||
} else { | ||
setStart(false); | ||
} | ||
} | ||
let carousel = this.carouselRef.current; | ||
let wrapper = this.wrapper.current; | ||
let cards = carousel.getElementsByClassName('card'); | ||
let exampleCard = cards[0]; | ||
let style = exampleCard | ||
? exampleCard.currentStyle || window.getComputedStyle(exampleCard) | ||
: null; | ||
let cardWidth = exampleCard | ||
? exampleCard.offsetWidth + parseFloat(style.marginRight) | ||
: 0; | ||
let wrapperWidth = wrapper.offsetWidth; | ||
let cardsPerView = Math.floor(wrapperWidth / cardWidth); | ||
let max = carousel.scrollWidth - carousel.offsetWidth; | ||
|
||
this.setState({ | ||
cardsPerView: cardsPerView, | ||
wrapperWidth: wrapperWidth, | ||
cardWidth: cardWidth, | ||
init: true, | ||
width: carousel.offsetWidth, | ||
max: max, | ||
}); | ||
} | ||
el.addEventListener('scroll', checkScroll); | ||
|
||
isInViewport() { | ||
if (this.state.inView) { | ||
let page = document.querySelectorAll('.page-wrap')[0]; | ||
page.removeEventListener('scroll', this.isInViewport); | ||
return () => { | ||
el.removeEventListener('scroll', checkScroll); | ||
}; | ||
}, [history, track, id, data]); | ||
|
||
useEffect(() => { | ||
if ( | ||
!track || | ||
!track.current || | ||
!data || | ||
data.length === 0 || | ||
!waitForScroll | ||
) | ||
return; | ||
const current = history.location.pathname; | ||
const reduxPage = redux_pos[current]; | ||
if (reduxPage) { | ||
const carousels = reduxPage.carousels; | ||
if (!carousels) return; | ||
const thisCarousel = carousels[id]; | ||
if (thisCarousel) { | ||
track.current.scrollLeft = thisCarousel.scroll; | ||
} | ||
} | ||
let carousel = this.wrapper.current; | ||
if (!carousel) return; | ||
const top = carousel.getBoundingClientRect().top; | ||
const wH = window.innerHeight; | ||
if (top <= wH * 1.5) { | ||
this.setState({ | ||
inView: true, | ||
}); | ||
} else { | ||
this.setState({ | ||
inView: this.state.inView ? true : false, | ||
}); | ||
} | ||
} | ||
setWaitForScroll(false); | ||
}, [history.location.pathname, track, data, id, redux_pos, waitForScroll]); | ||
|
||
scroll() { | ||
let carousel = this.carouselRef.current; | ||
if (!carousel) return; | ||
let position = carousel.scrollLeft; //+ carousel.offsetWidth; | ||
let max = carousel.scrollWidth - carousel.offsetWidth; | ||
this.setState({ | ||
width: carousel.offsetWidth, | ||
pos: position, | ||
max: max, | ||
}); | ||
} | ||
|
||
next() { | ||
let carousel = this.carouselRef.current; | ||
let scrollAmount = this.state.cardWidth * this.state.cardsPerView; | ||
let start = carousel.scrollLeft; | ||
let movement = | ||
Math.floor((start + scrollAmount) / this.state.cardWidth) * | ||
this.state.cardWidth; | ||
carousel.scrollTo({ | ||
function scrollBack() { | ||
if (!track || !track.current || !data || data.length === 0) return; | ||
track.current.scroll({ | ||
top: 0, | ||
left: movement, | ||
left: track.current.scrollLeft - track.current.offsetWidth * 0.8, | ||
behavior: 'smooth', | ||
}); | ||
} | ||
|
||
prev() { | ||
let carousel = this.carouselRef.current; | ||
let scrollAmount = this.state.cardWidth * this.state.cardsPerView; | ||
let start = carousel.scrollLeft; | ||
let movement = | ||
Math.floor((start - scrollAmount) / this.state.cardWidth) * | ||
this.state.cardWidth; | ||
carousel.scrollTo({ | ||
function scrollForward() { | ||
if (!track || !track.current || !data || data.length === 0) return; | ||
track.current.scroll({ | ||
top: 0, | ||
left: movement, | ||
left: track.current.scrollLeft + track.current.offsetWidth * 0.8, | ||
behavior: 'smooth', | ||
}); | ||
} | ||
|
||
render() { | ||
const childrenWithProps = React.Children.map( | ||
this.props.children, | ||
(child) => { | ||
if (React.isValidElement(child)) { | ||
return React.cloneElement(child, { | ||
pos: this.state.pos, | ||
width: this.state.width ? this.state.width : 0, | ||
}); | ||
} | ||
return child; | ||
}, | ||
); | ||
return ( | ||
return ( | ||
<div className="container"> | ||
<div | ||
className={`carousel--wrap ${ | ||
this.state.inView ? 'visible' : 'not-visible' | ||
className={`${carousel.wrap} ${ | ||
type === 'request' ? carousel.wrap__nospacing : '' | ||
}`} | ||
ref={this.wrapper} | ||
> | ||
<div className="carousel--controls"> | ||
{link ? ( | ||
<p className={typo.carousel_title}> | ||
{title ? ( | ||
<Link to={link}> | ||
{title} | ||
<span className={typo.carousel_title__icon}></span> | ||
</Link> | ||
) : ( | ||
'Loading...' | ||
)} | ||
</p> | ||
) : ( | ||
<p className={typo.carousel_title}>{title ? title : 'Loading...'}</p> | ||
)} | ||
<div className={carousel.controls}> | ||
<div | ||
className={`carousel--controls--item carousel--prev ${ | ||
this.state.pos > 0 ? '' : 'disabled' | ||
className={`${carousel.controls__prev} ${ | ||
start ? carousel.controls__disabled : '' | ||
}`} | ||
onClick={this.prev} | ||
onClick={scrollBack} | ||
> | ||
<LeftArrow /> | ||
<Chevron /> | ||
</div> | ||
<div | ||
className={`carousel--controls--item carousel--next ${ | ||
this.state.pos < this.state.max ? '' : 'disabled' | ||
className={`${carousel.controls__next} ${ | ||
end ? carousel.controls__disabled : '' | ||
}`} | ||
onClick={this.next} | ||
onClick={scrollForward} | ||
> | ||
<RightArrow /> | ||
<Chevron /> | ||
</div> | ||
</div> | ||
<div | ||
className={`carousel`} | ||
ref={this.carouselRef} | ||
onScroll={this.scroll} | ||
className={`${carousel.track} ${ | ||
type === 'request' ? carousel.track__nospacing : '' | ||
} carousel-store`} | ||
id={rowId} | ||
ref={track} | ||
> | ||
<div className="carousel--inner">{childrenWithProps}</div> | ||
{data && data.length > 0 | ||
? data.map((item, i) => { | ||
if (item === 'watched') return null; | ||
|
||
if (type === 'request') { | ||
return ( | ||
<Card | ||
key={`${rowId}__carousel__${item.id}__${i}`} | ||
title={item.title} | ||
poster={item.poster} | ||
logo={item.logo} | ||
video={false} | ||
type={type} | ||
id={item.id} | ||
requestType={item.requestType} | ||
/> | ||
); | ||
} | ||
const video = | ||
item.videos && item.videos.results.length > 0 | ||
? item.videos.results[0].key | ||
: false; | ||
const date = | ||
type === 'movie' | ||
? item.release_date | ||
: type === 'tv' | ||
? item.first_air_date | ||
: ''; | ||
const poster = | ||
type === 'movie' || type === 'tv' | ||
? item.poster_path | ||
: type === 'people' | ||
? item.profile_path | ||
: item.logo_path; | ||
if (typeof item === 'string' || typeof item === 'number') | ||
item = { id: item, load: true }; | ||
|
||
return ( | ||
<Card | ||
key={`${rowId}__carousel__${item.id}__${i}`} | ||
title={type === 'movie' ? item.title : item.name} | ||
poster={poster} | ||
video={video} | ||
year={date ? new Date(date).getFullYear() : false} | ||
type={type} | ||
id={item.id} | ||
character={item.character} | ||
credit={item.credit} | ||
name={item.name} | ||
item={item} | ||
load={item.load} | ||
newNotification={newNotification} | ||
/> | ||
); | ||
}) | ||
: placeholderRow} | ||
</div> | ||
</div> | ||
); | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
export default Carousel; | ||
export default connect(mapStateToProps)(Carousel); |
Oops, something went wrong.