Skip to content

Instantly share code, notes, and snippets.

@lilrogalski
Created August 30, 2017 06:25
Show Gist options
  • Save lilrogalski/cf695d3912b67081ae89bdd3bd1d40e9 to your computer and use it in GitHub Desktop.
Save lilrogalski/cf695d3912b67081ae89bdd3bd1d40e9 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import ReactCSSTransition from 'react-addons-css-transition-group'
import cn from 'classnames'
import style from './style.css'
import buttons from '../App/buttons.css'
export default class HypnoView extends Component {
constructor () {
super()
this.state = {
playing: true,
currentPhoto: [],
index: 0,
interval: 400
}
}
componentDidMount () {
console.log('hypnoview mounted')
if (this.props.photos.length === 0) {
setTimeout(() => this.cyclePhotos(), 500)
} else {
this.setState({ playing: true })
this.cyclePhotos()
}
}
componentWillUnmount () {
clearTimeout(this.timeout)
this.timeout = null
}
cyclePhotos () {
const i = this.state.index
const photo = this.props.photos[i]
const nextIndex = (i + 1) % this.props.photos.length
const next = this.props.photos[nextIndex]
const currentPhoto = photo
this.setState({ currentPhoto })
this.timeout = setTimeout(() => {
if (this.state.playing) {
this.setState({
currentPhoto: next,
index: nextIndex
})
this.cyclePhotos()
}
}, this.state.interval)
}
changeInterval = (e) => {
const interval = e.target.value
this.setState({ interval })
}
clickOverlay = (e) => {
console.log('click overlay')
if (this.state.playing) {
this.stopHypno()
} else {
this.resumeHypno()
}
}
clickSearch = () => {
this.search.focus()
this.search.select()
}
resumeHypno () {
this.setState({ playing: true })
this.cyclePhotos()
}
stopHypno () {
this.setState({
playing: false
})
}
goBack = (e) => {
e.preventDefault()
this.stepPhoto(-1)
}
goForward = (e) => {
e.preventDefault()
this.stepPhoto(1)
}
stepPhoto (dir) {
const cur = this.state.index
const len = this.props.photos.length - 1
const i = (cur === 0 && dir === -1) ? len : (cur + dir) % len
const photo = this.props.photos[i]
this.setState({
currentPhoto: photo,
index: i
})
}
render () {
const currentImg = (
<img
key={this.state.index}
src={this.state.currentPhoto.url}
className={style.hypno_img}
/>
)
const bgClass = cn(
style.bg_overlay,
this.state.playing && style.hidden
)
return (
<div className={style.hypno_wrap} >
<div className={bgClass} onClick={this.clickOverlay}>
<form onSubmit={this.props.submitForm} onClick={e => e.stopPropagation()}>
<input
onClick={this.clickSearch}
ref={search => { this.search = search }}
className={style.search_input}
placeholder="@instagram username"
type="text"
value={this.props.username}
onChange={this.props.changeInput}
/>
<a
href=""
onClick={this.props.clickLogOut}
className={cn(buttons.btn, buttons.logout)}
>
Log Out
</a>
</form>
<div className={style.controls}>
<div className={style.controls_section}>
<span className={style.speed}>{this.state.interval}ms</span>
<input
className={style.slider}
type="range"
min="100"
max="2000"
step="100"
value={this.state.interval}
onChange={this.changeInterval}
/>
</div>
<div className={style.controls_section} onClick={e => e.stopPropagation()}>
<a href="#" className={style.arrow} onClick={this.goBack}>&laquo; </a>
<a
href={this.state.currentPhoto.link}
target="_blank"
className={style.controls_link}
>
view on instagram
</a>
<a href="#" className={style.arrow} onClick={this.goForward}> &raquo;</a>
</div>
</div>
</div>
<ReactCSSTransition
transitionName="cross-fade"
transitionEnterTimeout={200}
transitionLeaveTimeout={200}>
{currentImg}
</ReactCSSTransition>
</div>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment