Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import Link from 'next/link'
  3. import Router from 'next/router'
  4. import YouTube from 'react-youtube'
  5. import applyLayout from '../modules/next-layouts'
  6. import ContentLayout from '../components/layout'
  7. import PropTypes from 'prop-types'
  8. import Head from 'next/head'
  9. import Footer from '../components/footer'
  10. import layout from "../static/css-modules/layout.css"
  11. import styles from "../static/css-modules/style.css"
  12. import CSS from "../static/css-modules/works.css"
  13. import Videos from "../api/videos.json"
  14.  
  15. class Works extends React.Component {
  16.     static layout = ContentLayout
  17.     videoSettings = {
  18.         height: '400px',
  19.         width: '100%',
  20.         playerVars: { // https://developers.google.com/youtube/player_parameters
  21.             autoplay: 0,
  22.             rel: 0
  23.         }
  24.     }
  25.     constructor(props){
  26.         super(props)
  27.         let selectedVideo, ogData
  28.  
  29.         if(typeof props.query.id === "undefined"){
  30.             selectedVideo = 0
  31.             ogData = {
  32.                 "fb_tag_title":         "OP Works",
  33.                 "fb_tag_description":   "We deliver uniquely tailored messages to hundreds of millions of people through a wide network of Ocean Pulser influencers. The messages are hand-crafted to effectively reach each target group.",
  34.                 "fb_tag_image":         "/static/og_pages/work.jpg"
  35.             }
  36.         }else{
  37.             selectedVideo = Videos.findIndex((item, index) =>(
  38.                 item.video === props.query.id
  39.             ))
  40.             ogData = {
  41.                 "fb_tag_title":         selectedVideo.title,
  42.                 "fb_tag_description":   selectedVideo.title,
  43.                 "fb_tag_image":         "/static/work/og/"+selectedVideo.thumb
  44.             }
  45.         }
  46.  
  47.         this.state = {
  48.             defaultVideo: selectedVideo,
  49.             initVideoPlayer: false,
  50.             selectedVideo: selectedVideo,
  51.             videoEvent: null,
  52.             videos: Videos.slice(0, 6),
  53.             listState: null,
  54.             start: 0,
  55.             end: 5,
  56.             //facebook open graph settings
  57.             ...ogData
  58.         }
  59.        
  60.         this.playerReady = this.playerReady.bind(this)
  61.     }
  62.     static async getInitialProps ({ pathname, query }) {
  63.         return {pathname, query}
  64.     }
  65.     componentDidMount(){
  66.         //wait for the page to load then show videos to achieve nicer transition
  67.         setTimeout(() => this.setState({ listState: CSS.animateIn }), 200 )
  68.  
  69.         //wait until transitions are finished before showing youtube video as otherwise it would cause lag
  70.         setTimeout(() => this.setState({ initVideoPlayer: true }), 1000 )
  71.     }
  72.     playerReady(event) {
  73.         this.setState({videoEvent: event.target})
  74.     }
  75.     navigateVideos(direction){
  76.         //hide old videos
  77.         this.setState({ listState: CSS.animateOut })
  78.        
  79.         let start = this.state.start
  80.         let end = this.state.end
  81.  
  82.         if(direction === "next"){
  83.             if(end + 6 < Videos.length - 1){
  84.                 start += 6
  85.                 end +=  7
  86.             }else{
  87.                 start = Videos.length - 6
  88.                 end = Videos.length
  89.             }
  90.  
  91.         }else{
  92.             if(start - 6 > 0){
  93.                 start -= 6
  94.                 end -=  6
  95.             }else{
  96.                 start = 0
  97.                 end = 6
  98.             }
  99.         }
  100.  
  101.         setTimeout(() => (
  102.             this.setState({
  103.                 videos: Videos.slice(start, end),
  104.                 listState: CSS.animateIn,
  105.                 start: start,
  106.                 end: end
  107.             })
  108.         ), 1100 )
  109.     }
  110.     selectVideo(index){
  111.         this.setState({selectedVideo: index})
  112.         this.state.videoEvent.loadVideoById(Videos[index].video, 0, "large")
  113.         window.history.pushState("", "", "/works/v/"+Videos[index].video)
  114.         // Router.push("/works?v="+Videos[index].video, "/works/v/"+Videos[index].video, { shallow: true })
  115.     }
  116.     generateDelay(){
  117.         return Math.floor(Math.random() * (0 - 1000) + 1000)
  118.     }
  119.     render () {
  120.         return (
  121.             <React.Fragment>
  122.                 <Head>
  123.                     <title>OP Works</title>
  124.                     <meta property='og:video'       content={'https://www.youtube.com/watch?v='+Videos[this.state.selectedVideo].video} />
  125.                     <meta property="og:title"       content={this.state.fb_tag_title} />
  126.                     <meta property="og:description" content="We deliver uniquely tailored messages to hundreds of millions of people through a wide network of Ocean Pulser influencers. The messages are hand-crafted to effectively reach each target group." />
  127.                     <meta property="og:type"        content="website" />
  128.                     <meta property="og:image"       content={this.state.fb_tag_image} />
  129.                 </Head>
  130.  
  131.                 <div id="leftCol" className={this.context.pageStatus}>
  132.                     <ul className={CSS.videos}>
  133.                     {this.state.videos.map((video, index) =>{
  134.                        
  135.                         return (
  136.                             <li
  137.                                 className={
  138.                                     (this.state.selectedVideo === index ? CSS.selectedVideo : null)+' '+
  139.                                     this.state.listState
  140.                                 }
  141.                                 key={index}
  142.                                 style={{ transitionDelay: this.generateDelay()+"ms" }}
  143.                                 onClick={()=> this.selectVideo(index)}
  144.                             >
  145.                                 <div
  146.                                     className={CSS.background}
  147.                                     style={{ backgroundImage: "url(/static/work/"+video.thumb+")"  }}
  148.                                 ></div>
  149.                             </li>
  150.                         )
  151.                     })}
  152.                     </ul>
  153.  
  154.                     {this.state.start > 0 &&
  155.                         <div className={CSS.navigate+' '+CSS.prev} onClick={() => this.navigateVideos("prev")}></div>
  156.                     }
  157.                     {this.state.end < Videos.length &&
  158.                         <div className={CSS.navigate+' '+CSS.next} onClick={() => this.navigateVideos("next")}></div>
  159.                     }
  160.                 </div>
  161.                 <div id="rightCol" className={CSS.contentWrapper+' '+layout.contentPadding+' '+this.context.pageStatus}>
  162.                     <section className={CSS.content} >
  163.                         <h1>Works</h1>
  164.  
  165.                         <p className={CSS.message}>We deliver uniquely tailored messages to hundreds of millions of people through a wide network of Ocean Pulser influencers. The messages are hand-crafted to effectively reach each target group.</p>
  166.  
  167.                         {this.state.initVideoPlayer &&
  168.                             <YouTube
  169.                                 videoId={Videos[this.state.defaultVideo].video}
  170.                                 opts={this.videoSettings}
  171.                                 onReady={this.playerReady}
  172.                             />
  173.                         }
  174.                         <p className={CSS.title}>{Videos[this.state.selectedVideo].title}</p>
  175.                     </section>
  176.                 </div>
  177.                 <Footer />
  178.             </React.Fragment>
  179.         )
  180.     }
  181. }
  182.  
  183. Works.contextTypes = {
  184.     pageStatus: PropTypes.string
  185. }
  186.  
  187. export default applyLayout(Works)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement