the return of the infinite scroll (#795)

This commit is contained in:
Connor Turland 2016-10-22 00:15:10 -04:00 committed by GitHub
parent f8556c30a5
commit cbc8e6cdd4
2 changed files with 27 additions and 15 deletions

View file

@ -13,6 +13,7 @@ import Maps from '../../components/Maps'
*/ */
const ExploreMaps = { const ExploreMaps = {
pending: false,
setCollection: function (collection) { setCollection: function (collection) {
var self = ExploreMaps var self = ExploreMaps
@ -35,8 +36,8 @@ const ExploreMaps = {
cb = mapperObj cb = mapperObj
mapperObj = null mapperObj = null
} }
var exploreObj = { var exploreObj = {
currentUser: Active.Mapper, currentUser: Active.Mapper,
section: self.collection.id, section: self.collection.id,
maps: self.collection, maps: self.collection,
@ -44,6 +45,7 @@ const ExploreMaps = {
moreToLoad: self.collection.page != 'loadedAll', moreToLoad: self.collection.page != 'loadedAll',
user: mapperObj, user: mapperObj,
loadMore: self.loadMore, loadMore: self.loadMore,
pending: self.pending,
onStar: function (map) { onStar: function (map) {
$.post('/maps/' + map.id + '/star') $.post('/maps/' + map.id + '/star')
map.set('star_count', map.get('star_count') + 1) map.set('star_count', map.get('star_count') + 1)
@ -56,48 +58,51 @@ const ExploreMaps = {
$.post({ $.post({
url: `/maps/${map.id}/access_request` url: `/maps/${map.id}/access_request`
}) })
GlobalUI.notifyUser('You will be notified by email if request accepted') GlobalUI.notifyUser('You will be notified by email if request accepted')
} }
} }
ReactDOM.render( ReactDOM.render(
React.createElement(Maps, exploreObj), React.createElement(Maps, exploreObj),
document.getElementById('explore') document.getElementById('explore')
).resize() ).resize()
if (cb) cb() if (cb) cb()
Metamaps.Loading.hide()
}, },
loadMore: function () { loadMore: function () {
var self = ExploreMaps var self = ExploreMaps
if (self.collection.page != "loadedAll") { if (self.collection.page != "loadedAll") {
self.collection.getMaps() self.collection.getMaps()
self.pending = true
} }
else self.render() self.render()
}, },
handleSuccess: function (cb) { handleSuccess: function (cb) {
var self = ExploreMaps var self = ExploreMaps
self.pending = false
if (self.collection && self.collection.id === 'mapper') { if (self.collection && self.collection.id === 'mapper') {
self.fetchUserThenRender(cb) self.fetchUserThenRender(cb)
} else { } else {
self.render(cb) self.render(cb)
Metamaps.Loading.hide()
} }
}, },
handleError: function () { handleError: function () {
console.log('error loading maps!') // TODO console.log('error loading maps!') // TODO
Metamaps.Loading.hide()
}, },
fetchUserThenRender: function (cb) { fetchUserThenRender: function (cb) {
var self = ExploreMaps var self = ExploreMaps
// first load the mapper object and then call the render function // first load the mapper object and then call the render function
$.ajax({ $.ajax({
url: '/users/' + self.collection.mapperId + '/details.json', url: '/users/' + self.collection.mapperId + '/details.json',
success: function (response) { success: function (response) {
self.render(response, cb) self.render(response, cb)
Metamaps.Loading.hide()
}, },
error: function () { error: function () {
self.render(cb) self.render(cb)
Metamaps.Loading.hide()
} }
}) })
} }

View file

@ -1,4 +1,5 @@
import React, { Component, PropTypes } from 'react' import React, { Component, PropTypes } from 'react'
import { throttle } from 'lodash'
import Header from './Header' import Header from './Header'
import MapperCard from './MapperCard' import MapperCard from './MapperCard'
import MapCard from './MapCard' import MapCard from './MapCard'
@ -15,9 +16,10 @@ class Maps extends Component {
componentDidMount() { componentDidMount() {
window && window.addEventListener('resize', this.resize) window && window.addEventListener('resize', this.resize)
this.refs.maps.addEventListener('scroll', throttle(this.scroll, 500, { leading: true, trailing: false }))
this.resize() this.resize()
} }
componentWillUnmount() { componentWillUnmount() {
window && window.removeEventListener('resize', this.resize) window && window.removeEventListener('resize', this.resize)
} }
@ -30,22 +32,26 @@ class Maps extends Component {
this.setState({ mapsWidth }) this.setState({ mapsWidth })
} }
scroll = () => {
const { loadMore, moreToLoad, pending } = this.props
const { maps } = this.refs
if (moreToLoad && !pending && maps.scrollTop + maps.offsetHeight > maps.scrollHeight - 300 ) {
loadMore()
}
}
render = () => { render = () => {
const { maps, currentUser, juntoState, section, user, moreToLoad, loadMore, onStar, onRequest } = this.props const { maps, currentUser, juntoState, section, user, moreToLoad, loadMore, onStar, onRequest } = this.props
const style = { width: this.state.mapsWidth + 'px' } const style = { width: this.state.mapsWidth + 'px' }
return ( return (
<div> <div>
<div id='exploreMaps'> <div id='exploreMaps' ref='maps'>
<div style={ style }> <div style={ style }>
{ user ? <MapperCard user={ user } /> : null } { user ? <MapperCard user={ user } /> : null }
{ currentUser && !user ? <div className="map newMap"><a href="/maps/new"><div className="newMapImage"></div><span>Create new map...</span></a></div> : null } { currentUser && !user ? <div className="map newMap"><a href="/maps/new"><div className="newMapImage"></div><span>Create new map...</span></a></div> : null }
{ maps.models.map(map => <MapCard key={ map.id } map={ map } juntoState={ juntoState } currentUser={ currentUser } onStar={ onStar } onRequest={ onRequest } />) } { maps.models.map(map => <MapCard key={ map.id } map={ map } juntoState={ juntoState } currentUser={ currentUser } onStar={ onStar } onRequest={ onRequest } />) }
<div className='clearfloat'></div> <div className='clearfloat'></div>
{!moreToLoad ? null : [
<button className="button loadMore" onClick={ loadMore }>load more</button>,
<div className='clearfloat'></div>
]}
</div> </div>
</div> </div>
<Header signedIn={ !!currentUser } <Header signedIn={ !!currentUser }
@ -65,6 +71,7 @@ Maps.propTypes = {
user: PropTypes.object, user: PropTypes.object,
currentUser: PropTypes.object, currentUser: PropTypes.object,
loadMore: PropTypes.func, loadMore: PropTypes.func,
pending: PropTypes.bool.isRequired,
onStar: PropTypes.func.isRequired, onStar: PropTypes.func.isRequired,
onRequest: PropTypes.func.isRequired onRequest: PropTypes.func.isRequired
} }