2016-08-22 01:02:49 +00:00
|
|
|
import React, { Component, PropTypes } from 'react'
|
2016-10-22 04:15:10 +00:00
|
|
|
import { throttle } from 'lodash'
|
2016-09-24 16:02:42 +00:00
|
|
|
import Header from './Header'
|
|
|
|
import MapperCard from './MapperCard'
|
|
|
|
import MapCard from './MapCard'
|
2016-10-19 18:40:42 +00:00
|
|
|
|
2016-10-24 14:23:05 +00:00
|
|
|
// 220 wide + 16 padding on both sides
|
2016-10-19 18:40:42 +00:00
|
|
|
const MAP_WIDTH = 252
|
2016-10-23 20:12:07 +00:00
|
|
|
const MOBILE_VIEW_BREAKPOINT = 504
|
2016-10-24 14:23:05 +00:00
|
|
|
const MOBILE_VIEW_PADDING = 40
|
2016-10-20 22:09:20 +00:00
|
|
|
const MAX_COLUMNS = 4
|
2016-08-22 01:02:49 +00:00
|
|
|
|
|
|
|
class Maps extends Component {
|
2016-10-19 18:40:42 +00:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { mapsWidth: 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
window && window.addEventListener('resize', this.resize)
|
2016-10-22 04:15:10 +00:00
|
|
|
this.refs.maps.addEventListener('scroll', throttle(this.scroll, 500, { leading: true, trailing: false }))
|
2016-10-19 18:40:42 +00:00
|
|
|
this.resize()
|
|
|
|
}
|
2016-10-22 04:15:10 +00:00
|
|
|
|
2016-10-19 18:40:42 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
window && window.removeEventListener('resize', this.resize)
|
|
|
|
}
|
|
|
|
|
|
|
|
resize = () => {
|
2016-10-20 22:09:20 +00:00
|
|
|
const { maps, user, currentUser } = this.props
|
|
|
|
const numCards = maps.length + (user || currentUser ? 1 : 0)
|
2016-10-19 18:40:42 +00:00
|
|
|
const mapSpaces = Math.floor(document.body.clientWidth / MAP_WIDTH)
|
2016-10-23 20:12:07 +00:00
|
|
|
const mapsWidth = document.body.clientWidth <= MOBILE_VIEW_BREAKPOINT ?
|
|
|
|
document.body.clientWidth - MOBILE_VIEW_PADDING :
|
|
|
|
Math.min(MAX_COLUMNS, Math.min(numCards, mapSpaces)) * MAP_WIDTH
|
2016-10-20 22:09:20 +00:00
|
|
|
this.setState({ mapsWidth })
|
2016-10-19 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 04:15:10 +00:00
|
|
|
scroll = () => {
|
|
|
|
const { loadMore, moreToLoad, pending } = this.props
|
|
|
|
const { maps } = this.refs
|
|
|
|
if (moreToLoad && !pending && maps.scrollTop + maps.offsetHeight > maps.scrollHeight - 300 ) {
|
|
|
|
loadMore()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-22 01:02:49 +00:00
|
|
|
render = () => {
|
2016-10-23 15:44:20 +00:00
|
|
|
const { maps, currentUser, juntoState, pending, section, user, moreToLoad, loadMore, onStar, onRequest } = this.props
|
2016-10-19 18:40:42 +00:00
|
|
|
const style = { width: this.state.mapsWidth + 'px' }
|
2016-10-23 20:12:07 +00:00
|
|
|
const mobile = document && document.body.clientWidth <= MOBILE_VIEW_BREAKPOINT
|
2016-08-22 01:02:49 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2016-10-22 04:15:10 +00:00
|
|
|
<div id='exploreMaps' ref='maps'>
|
2016-10-19 18:40:42 +00:00
|
|
|
<div style={ style }>
|
2016-08-22 01:02:49 +00:00
|
|
|
{ user ? <MapperCard user={ user } /> : null }
|
2016-10-23 15:44:20 +00:00
|
|
|
{ currentUser && !user && !(pending && maps.length === 0) ? <div className="map newMap"><a href="/maps/new"><div className="newMapImage"></div><span>Create new map...</span></a></div> : null }
|
2016-10-23 20:12:07 +00:00
|
|
|
{ maps.models.map(map => <MapCard key={ map.id } map={ map } mobile={ mobile } juntoState={ juntoState } currentUser={ currentUser } onStar={ onStar } onRequest={ onRequest } />) }
|
2016-08-22 01:02:49 +00:00
|
|
|
<div className='clearfloat'></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<Header signedIn={ !!currentUser }
|
|
|
|
section={ section }
|
|
|
|
user={ user }
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Maps.propTypes = {
|
|
|
|
section: PropTypes.string.isRequired,
|
|
|
|
maps: PropTypes.object.isRequired,
|
2016-10-21 13:29:04 +00:00
|
|
|
juntoState: PropTypes.object.isRequired,
|
2016-08-22 01:02:49 +00:00
|
|
|
moreToLoad: PropTypes.bool.isRequired,
|
|
|
|
user: PropTypes.object,
|
|
|
|
currentUser: PropTypes.object,
|
2016-10-21 21:42:21 +00:00
|
|
|
loadMore: PropTypes.func,
|
2016-10-22 04:15:10 +00:00
|
|
|
pending: PropTypes.bool.isRequired,
|
2016-10-21 21:42:21 +00:00
|
|
|
onStar: PropTypes.func.isRequired,
|
|
|
|
onRequest: PropTypes.func.isRequired
|
2016-08-22 01:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Maps
|