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

138 lines
3.8 KiB
JavaScript
Raw Normal View History

/* global $ */
2016-09-22 20:16:18 -04:00
import React from 'react'
import ReactDOM from 'react-dom' // TODO ensure this isn't a double import
2016-09-23 08:05:26 +08:00
import Active from '../Active'
2016-11-07 15:25:08 -05:00
import DataModel from '../DataModel'
2016-10-21 17:42:21 -04:00
import GlobalUI from '../GlobalUI'
2017-03-11 01:49:27 -05:00
import { ReactApp } from '../GlobalUI'
import Realtime from '../Realtime'
import Loading from '../Loading'
import Maps from '../../components/Maps'
2016-09-23 08:05:26 +08:00
const ExploreMaps = {
pending: false,
2016-10-24 13:42:26 +00:00
mapper: null,
2017-03-11 01:49:27 -05:00
updateFromPath: function(path) {
const self = ExploreMaps
const test = path.split('/')[1]
const section = path.split('/')[2]
const id = path.split('/')[3]
if (test === 'explore') {
const capitalize = section.charAt(0).toUpperCase() + section.slice(1)
self.setCollection(DataModel.Maps[capitalize])
} else if (test === '') {
self.setCollection(DataModel.Maps.Active)
}
if (id) {
if (self.collection.mapperId !== id) {
// empty the collection if we are trying to load the maps
// collection of a different mapper than we had previously
self.collection.reset()
self.collection.page = 1
self.render()
}
self.collection.mapperId = id
}
if (self.collection.length === 0) {
Loading.show()
self.pending = true
self.collection.getMaps()
} else {
id ? self.fetchUserThenRender() : self.render()
}
},
2016-11-07 15:25:08 -05:00
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)
},
2017-03-11 01:49:27 -05:00
render: function() {
var self = ExploreMaps
2017-03-11 01:49:27 -05:00
ReactApp.render()
Loading.hide()
},
2016-11-07 15:25:08 -05:00
loadMore: function() {
var self = ExploreMaps
2016-11-07 15:25:08 -05:00
if (self.collection.page !== 'loadedAll') {
self.collection.getMaps()
self.pending = true
}
self.render()
},
2017-03-11 01:49:27 -05:00
handleSuccess: function() {
var self = ExploreMaps
self.pending = false
if (self.collection && self.collection.id === 'mapper') {
2017-03-11 01:49:27 -05:00
self.fetchUserThenRender()
} else {
2017-03-11 01:49:27 -05:00
self.render()
}
},
2016-11-07 15:25:08 -05:00
handleError: function() {
console.log('error loading maps!') // TODO
2016-11-07 15:25:08 -05:00
Loading.hide()
},
2016-11-07 15:25:08 -05:00
fetchUserThenRender: function(cb) {
var self = ExploreMaps
2016-10-24 13:42:26 +00:00
if (self.mapper && self.mapper.id === self.collection.mapperId) {
2017-03-11 01:49:27 -05:00
self.render()
return
2016-10-24 13:42:26 +00:00
}
// first load the mapper object and then call the render function
$.ajax({
url: '/users/' + self.collection.mapperId + '/details.json',
2016-11-07 15:25:08 -05:00
success: function(response) {
2016-10-24 13:42:26 +00:00
self.mapper = response
2017-03-11 01:49:27 -05:00
self.render()
},
2016-11-07 15:25:08 -05:00
error: function() {
2017-03-11 01:49:27 -05:00
self.render()
}
})
2017-03-11 01:49:27 -05:00
},
onStar: function(map) {
$.post('/maps/' + map.id + '/star')
map.set('star_count', map.get('star_count') + 1)
if (DataModel.Stars) DataModel.Stars.push({ user_id: Active.Mapper.id, map_id: map.id })
DataModel.Maps.Starred.add(map)
GlobalUI.notifyUser('Map is now starred')
ReactApp.render()
},
onRequest: function(map) {
$.post({
url: `/maps/${map.id}/access_request`
})
GlobalUI.notifyUser('You will be notified by email if request accepted')
},
onMapFollow: function(map) {
const isFollowing = map.isFollowedBy(Active.Mapper)
$.post({
url: `/maps/${map.id}/${isFollowing ? 'un' : ''}follow`
})
if (isFollowing) {
GlobalUI.notifyUser('You are no longer following this map')
Active.Mapper.unfollowMap(map.id)
} else {
GlobalUI.notifyUser('You are now following this map')
Active.Mapper.followMap(map.id)
}
ReactApp.render()
}
}
export default ExploreMaps