Router in a good shape

This commit is contained in:
Edin Dazdarevic
2017-04-09 19:34:08 +02:00
parent 562bbc638f
commit 1fdcc24827
7 changed files with 160 additions and 40 deletions

View File

@@ -24,15 +24,18 @@ class Main extends React.Component {
}
if (props.initialState) {
props.initialState.sort = props.initialState.sort || state.sort;
state.filters.rooms = props.initialState.rooms;
state.filters.category = props.initialState.category;
state.sort = props.initialState.sort || state.sort;
state.listingId = props.initialState.listingId;
state.listingDetails = true;
if (state.listingId) {
state.listingDetails = true;
}
}
this.state = state;
this.router = new Router(this);
this.router = new Router(this, props.initialState);
}
dispatch ({type, action = {}}) {
@@ -81,7 +84,6 @@ class Main extends React.Component {
map.setCenter(place.geometry.location);
map.setZoom(18);
}
console.log(map.getBounds());
this.dispatch({type: 'SEARCH_PLACE_CHANGED'});
});
@@ -97,13 +99,15 @@ class Main extends React.Component {
this.map = map;
map.addListener('zoom_changed', () => {
console.log('zoom_changed');
this.removeAllMarkers();
this.markers = [];
});
map.addListener('idle', () => {
console.log('idle');
this.dispatch({type: 'UPDATE_ROUTE', action: {params: {
bounds: map.getBounds().toUrlValue()
}}});
this.dispatch({type: 'MAP_IDLE'});
});
@@ -113,9 +117,7 @@ class Main extends React.Component {
if (this.state.listingId) {
console.log("IT ISSSSS");
loadListing(this.state.listingId).then(l => l.text()).then(l => {
console.log('listing loaded', l);
this.dispatch({type: 'VIEW_LISTING_DETAILS', action: {
id: this.state.listingId,
listing: JSON.parse(l)
@@ -126,7 +128,6 @@ class Main extends React.Component {
removeAllMarkers () {
if (this.markers) {
console.log('removeAllMarkers');
this.markers.forEach((m) => m.marker.setMap(null));
}
}
@@ -195,7 +196,6 @@ class Main extends React.Component {
})
.then(({body, totalCount}) => {
body.then(p => {
console.log('results_received: ', totalCount);
const data = JSON.parse(p);
const listingExists = (id) => {
@@ -213,7 +213,6 @@ class Main extends React.Component {
newMarkers.push(m);
}
});
console.log('markers_removed');
}
for(const [index, prop] of data.entries()) {
@@ -250,19 +249,36 @@ class Main extends React.Component {
});
marker.addListener('click', () => {
console.log('clicking...', prop._id);
// Maybe move out and call when popping state
if (this.state.listingId) {
const prevSelected = this.findMarker(this.state.listingId);
if (prevSelected) {
console.log('prevselected', prevSelected);
prevSelected.marker.setIcon(this.visitedMarkerIcon());
}
}
marker.setIcon(this.selectedMarkerIcon());
loadListing(prop._id).then(l => l.text()).then(l => {
console.log('listing loaded', l);
this.dispatch({type: 'UPDATE_ROUTE', action: {
toDispatch: {
type: 'VIEW_LISTING_DETAILS', action: {
id: prop._id,
listing: JSON.parse(l)
}
},
params: {
listingId: prop._id
}
}});
//this.dispatch({type: 'UPDATE_ROUTE', action: {type: 'VIEW_LISTING_DETAILS', action: {
//id: prop._id,
//listing: JSON.parse(l)
//}}});
this.dispatch({type: 'VIEW_LISTING_DETAILS', action: {
id: prop._id,
listing: JSON.parse(l)
@@ -293,7 +309,18 @@ class Main extends React.Component {
* Refreshes search
*/
refreshListings(more = false) {
console.log('refreshListings');
// TODO: move somewhere else
if (!more && this.state.listingId) {
loadListing(this.state.listingId).then(l => l.text()).then(l => {
this.dispatch({type: 'VIEW_LISTING_DETAILS', action: {
id: this.state.listingId,
listing: JSON.parse(l)
}});
});
}
if (!more) {
this.loadPins();
@@ -332,7 +359,6 @@ class Main extends React.Component {
})
.then(({body, totalCount}) => {
body.then(p => {
console.log('results_received: ', totalCount);
const data = JSON.parse(p);
this.dispatch({
@@ -437,9 +463,7 @@ class Main extends React.Component {
const children = [];
if (this.state.listingDetails) {
console.log('CURRENT LISTINGS', this.state.listings);
const listing = this.state.listing; //this.state.listings.get(this.state.listingId);
console.log(this.state);
children.push(<ListingDetails
listing={listing}
imageIndex={this.state.imageIndex}