metamaps--metamaps/frontend/src/Metamaps/Views/ExploreMaps.js

112 lines
3.1 KiB
JavaScript
Raw Normal View History

/* global $ */
2016-09-23 00:16:18 +00:00
import React from 'react'
import ReactDOM from 'react-dom' // TODO ensure this isn't a double import
2016-09-23 00:05:26 +00:00
import Active from '../Active'
2016-10-21 21:42:21 +00:00
import GlobalUI from '../GlobalUI'
import Realtime from '../Realtime'
import Loading from '../Loading'
import Maps from '../../components/Maps'
2016-09-23 00:05:26 +00:00
const ExploreMaps = {
pending: false,
2016-10-24 13:42:26 +00:00
mapper: null,
setCollection: function (collection) {
var self = ExploreMaps
if (self.collection) {
self.collection.off('add', self.render)
self.collection.off('successOnFetch', self.handleSuccess)
self.collection.off('errorOnFetch', self.handleError)
}
self.collection = collection
self.collection.on('add', self.render)
self.collection.on('successOnFetch', self.handleSuccess)
self.collection.on('errorOnFetch', self.handleError)
},
2016-10-24 13:42:26 +00:00
render: function (cb) {
var self = ExploreMaps
if (!self.collection) return
var exploreObj = {
currentUser: Active.Mapper,
section: self.collection.id,
maps: self.collection,
juntoState: Realtime.juntoState,
moreToLoad: self.collection.page != 'loadedAll',
2016-10-24 13:42:26 +00:00
user: self.collection.id === 'mapper' ? self.mapper : null,
2016-10-21 21:42:21 +00:00
loadMore: self.loadMore,
pending: self.pending,
2016-10-21 21:42:21 +00:00
onStar: function (map) {
$.post('/maps/' + map.id + '/star')
map.set('star_count', map.get('star_count') + 1)
if (Metamaps.Stars) Metamaps.Stars.push({ user_id: Active.Mapper.id, map_id: map.id })
Metamaps.Maps.Starred.add(map)
GlobalUI.notifyUser('Map is now starred')
self.render()
},
onRequest: function (map) {
$.post({
url: `/maps/${map.id}/access_request`
})
GlobalUI.notifyUser('You will be notified by email if request accepted')
2016-10-21 21:42:21 +00:00
}
}
ReactDOM.render(
React.createElement(Maps, exploreObj),
document.getElementById('explore')
2016-10-21 23:10:28 +00:00
).resize()
if (cb) cb()
Loading.hide()
},
loadMore: function () {
var self = ExploreMaps
if (self.collection.page != "loadedAll") {
self.collection.getMaps()
self.pending = true
}
self.render()
},
handleSuccess: function (cb) {
var self = ExploreMaps
self.pending = false
if (self.collection && self.collection.id === 'mapper') {
self.fetchUserThenRender(cb)
} else {
self.render(cb)
Metamaps.Loading.hide()
}
},
handleError: function () {
console.log('error loading maps!') // TODO
Metamaps.Loading.hide()
},
fetchUserThenRender: function (cb) {
var self = ExploreMaps
2016-10-24 13:42:26 +00:00
if (self.mapper && self.mapper.id === self.collection.mapperId) {
self.render(cb)
return Metamaps.Loading.hide()
}
// first load the mapper object and then call the render function
$.ajax({
url: '/users/' + self.collection.mapperId + '/details.json',
success: function (response) {
2016-10-24 13:42:26 +00:00
self.mapper = response
self.render(cb)
Metamaps.Loading.hide()
},
error: function () {
self.render(cb)
Metamaps.Loading.hide()
}
})
}
}
export default ExploreMaps