Show only what's visible in the current result set

This commit is contained in:
Edin Dazdarevic
2017-04-05 15:11:21 +02:00
parent 15dc596f3d
commit e3e5b4ac96
3 changed files with 78 additions and 22 deletions

View File

@@ -20,7 +20,10 @@ export default class Listings extends React.Component {
const {images} = l; const {images} = l;
rendered.push( rendered.push(
<div className="property-list-item" onClick={this.onListingClick.bind(this, l.url)}> <div
key={l._id}
className="property-list-item"
onClick={this.onListingClick.bind(this, l.url)}>
<div className="pli-image"> <div className="pli-image">
<img src={images[0]} alt=""></img> <img src={images[0]} alt=""></img>
</div> </div>

View File

@@ -21,7 +21,6 @@ class Main extends React.Component {
} }
dispatch ({type, action = {}}) { dispatch ({type, action = {}}) {
console.log('DISPATCH', this);
handleMessage({type, action}, this); handleMessage({type, action}, this);
} }
@@ -33,10 +32,11 @@ class Main extends React.Component {
streetViewControl: false, streetViewControl: false,
mapTypeControl: false mapTypeControl: false
}); });
const marker = new google.maps.Marker({
position: uluru, //const marker = new google.maps.Marker({
map: map //position: uluru,
}); //map: map
//});
var control = document.createElement('div'); var control = document.createElement('div');
control.classList.add('filters-btn-toggle'); control.classList.add('filters-btn-toggle');
@@ -81,11 +81,25 @@ class Main extends React.Component {
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control); map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);
this.map = map; this.map = map;
map.addListener('zoom_changed', () => {
console.log('zoom_changed');
this.removeAllMarkers();
this.markers = [];
});
map.addListener('idle', () => { map.addListener('idle', () => {
console.log('idle');
this.refreshListings(); this.refreshListings();
}); });
} }
removeAllMarkers () {
if (this.markers) {
console.log('removeAllMarkers');
this.markers.forEach((m) => m.marker.setMap(null));
}
}
onCloseClick(e) { onCloseClick(e) {
if (this.state.mapClicked) { if (this.state.mapClicked) {
setTimeout(() => { setTimeout(() => {
@@ -98,7 +112,11 @@ class Main extends React.Component {
}); });
} }
/*
* Refreshes search
*/
refreshListings() { refreshListings() {
console.log('refreshListings');
const map = this.map; const map = this.map;
const { const {
rooms, rooms,
@@ -109,8 +127,9 @@ class Main extends React.Component {
category category
} = this.state.filters; } = this.state.filters;
const bounds = map.getBounds();
const properties = loadProperties({ const properties = loadProperties({
bounds: map.getBounds().toUrlValue(), bounds: bounds.toUrlValue(),
rooms, rooms,
minSize, minSize,
maxSize, maxSize,
@@ -119,30 +138,62 @@ class Main extends React.Component {
category category
}); });
properties.then(p=> p.text()).then(p => { const markerExists = (id) => {
return (this.markers || []).findIndex(m => m.id === id) !== -1;
}
properties.then(p => p.text()).then(p => {
const data = JSON.parse(p); const data = JSON.parse(p);
console.log('props', data) console.log('results_received');
const listingExists = (id) => {
return data.findIndex(l => l._id === id) !== -1
};
const newMarkers = [];
if (this.markers) {
this.markers.forEach((m) => {
if (!listingExists(m.id)) {
m.marker.setMap(null);
} else {
newMarkers.push(m);
}
});
console.log('markers_removed');
}
for(const [index, prop] of data.entries()) { for(const [index, prop] of data.entries()) {
const myLatLng = {lat: prop.loc[0], lng: prop.loc[1]}; const myLatLng = {lat: prop.loc[0], lng: prop.loc[1]};
const marker = new google.maps.Marker({ if (!markerExists(p._id)) {
position: myLatLng, const marker = new google.maps.Marker({
map: map, position: myLatLng,
title: prop.title map: map,
}); title: prop.title
});
marker.addListener('click', () => { marker.addListener('click', () => {
console.log('clicking...') console.log('clicking...')
this.dispatch({type: 'VIEW_LISTING_DETAILS', action: { this.dispatch({type: 'VIEW_LISTING_DETAILS', action: {
id: prop.url id: prop.url
}}) }})
}); });
newMarkers.push({
marker,
id: prop._id
});
}
} }
this.dispatch({ this.dispatch({
type: 'LISTINGS_LOADED', type: 'LISTINGS_LOADED',
action: { action: {
listings: data listings: data,
newMarkers
} }
}); });
}) })

View File

@@ -52,7 +52,7 @@ const viewListingDetails = ({ type, action }, component) => {
}; };
const listingsLoaded = ({ type, action }, component) => { const listingsLoaded = ({ type, action }, component) => {
const currentListings = new Map(); //component.state.listings; const currentListings = new Map();
for (const listing of action.listings) { for (const listing of action.listings) {
currentListings.set(listing.url, listing); currentListings.set(listing.url, listing);
@@ -60,6 +60,8 @@ const listingsLoaded = ({ type, action }, component) => {
component.setState({ component.setState({
listings: currentListings listings: currentListings
}, () => {
component.markers = action.newMarkers;
}); });
}; };