diff --git a/lib/destination-tile/index.js b/lib/destination-tile/index.js index 4a44226..338a98c 100644 --- a/lib/destination-tile/index.js +++ b/lib/destination-tile/index.js @@ -4,65 +4,94 @@ import ReactPlayer from 'react-player'; import FadeImage from '../fade-image'; import striptags from 'striptags'; import defaultImage from '../../src/constants/default-image'; -import { VIEW_FILM } from '../../src/constants/actionTypes'; + +function findNodeOffsetFromWindowTop (node) { + if (!node) { // offsetParent of the body node is null + return 0; + } + return node.offsetTop + findNodeOffsetFromWindowTop(node.offsetParent); +} class DestinationTile extends Component { constructor () { super(); this.state = { - viewFilm: false + tileInView: false }; + this._scrollHandler = this._scrollHandler.bind(this); } - rawMarkup (value) { - return { __html: value }; + + componentDidMount () { + this._attachScrollListeners(); } - handleOnClick () { - this.setState({ - viewFilm: true - }); + _attachScrollListeners () { + ['scroll', 'resize'].map((e) => window.addEventListener(e, this._scrollHandler)); } - componentWillReceiveProps (nextProps) { - if ( - !this.props.filmInView && - nextProps.filmInView - ) { - this.props.dispatch({ - type: VIEW_FILM + _removeScrollListeners () { + ['scroll', 'resize'].map((e) => window.removeEventListener(e, this._scrollHandler)); + } + + findNodeOffset () { + const node = this._scrollView; + const nodeHeight = node.offsetHeight; // height of the element + const fromTop = findNodeOffsetFromWindowTop(node); // y displacement of the top of the node from the top of the window + return fromTop + nodeHeight - window.pageYOffset - window.innerHeight; // offset of the node from the bottom of the window + } + + _scrollHandler () { + const nodeOffsetFromWindowBottom = this.findNodeOffset(); + if (nodeOffsetFromWindowBottom < window.pageYOffset) { + this.setState({ + tileInView: true }); } } + componentDidUpdate () { + this._attachScrollListeners(); + } + + rawMarkup (value) { + return { __html: value }; + } + + componentWillUnmount () { + this._removeScrollListeners(); + } + render () { const { tile: { sections } } = this.props; const { image, title, text } = sections[0]; const imageSrc = image || defaultImage; const videoUrl = sections[0].videoUrl; - const tileMedia = videoUrl ? : ; + const tileMedia = videoUrl ? : ; let previewTextArray = text.trim() .split('') .slice(0, 292); const previewText = striptags(previewTextArray.join('').trim()) + '…'; return ( -
- { tileMedia } -
-
-
-
{title}
-
-
-
20 / 183
-
-
Dest.
-
Hotels
+
this._scrollView = ref}> +
+ { tileMedia } +
+
+
+
{title}
+
+
+
20 / 183
+
+
Dest.
+
Hotels
+
-
-
- {previewText} +
+ {previewText} +
@@ -74,7 +103,6 @@ export default DestinationTile; DestinationTile.propTypes = { tile: PropTypes.object, - filmInView: PropTypes.bool, dispatch: PropTypes.func, videoUrl: PropTypes.string }; diff --git a/lib/destination-tile/test/index.test.js b/lib/destination-tile/test/index.test.js index f4966f1..bd80f8d 100644 --- a/lib/destination-tile/test/index.test.js +++ b/lib/destination-tile/test/index.test.js @@ -21,7 +21,7 @@ describe('Component', function () { const children = wrapper.children().nodes; describe('', function () { it('should render our DestinationTile component', function (done) { - expect(children).to.have.length(2); + expect(children).to.have.length(1); done(); }); }); diff --git a/src/components/search-results/index.js b/src/components/search-results/index.js index 4b60dd7..9d3cb56 100644 --- a/src/components/search-results/index.js +++ b/src/components/search-results/index.js @@ -165,7 +165,6 @@ class SearchResults extends Component {
); } else if (item.tile.type === 'destination' && contentExists) { - console.log('destination tile', item.tile); return (
{this.removeButton(item)}