import React from 'react';
import {findDOMNode} from 'react-dom';
import {formatPrice} from '../lib/helpers';
import {loadListing} from '../lib/api';
export default class Listings extends React.Component {
constructor(props) {
super(props);
this.handleScroll = (e) => {
const node = e.target;
const offset = node.scrollHeight - node.scrollTop - node.clientHeight;
if (this.props && this.props.loadingMore) {
return;
}
if (offset < 50) {
this.props.dispatch({type: 'LOAD_MORE_LISTINGS'});
}
}
}
onListingClick(id) {
loadListing(id).then(l => l.text()).then(l => {
this.props.dispatch({type: 'UPDATE_ROUTE', action: {
toDispatch: {
type: 'VIEW_LISTING_DETAILS', action: {
id,
listing: JSON.parse(l)
}
},
params: {
listingId: id
}
}});
this.props.dispatch({type: 'VIEW_LISTING_DETAILS', action: {
id,
listing: JSON.parse(l)
}});
});
}
onMouseEnter (id) {
this.props.dispatch({
type: 'ON_LISTING_MOUSE_OVER',
action: {
id
}
});
}
renderListings () {
const {listings = (new Map())} = this.props;
if (listings.size === 0) {
return (
Nema rezultata
Nema oglasa koji ispunjavaju vaše uslove pretrage.
)
}
const rendered = [];
for(const l of listings.values()) {
const {images} = l;
rendered.push(
{formatPrice(l.price)}
{l.rooms ? `${l.rooms} sobe, `: null}{l.size ? `${l.size}m2`: null}
Prije {l.time}
);
}
return rendered;
}
componentDidMount () {
this.attachScrollListener();
}
componentWillUnmount () {
this.removeScrollListener();
}
attachScrollListener () {
const listings = findDOMNode(this.refs.listings);
listings.parentNode.addEventListener('scroll', this.handleScroll);
}
removeScrollListener () {
const listings = findDOMNode(this.refs.listings);
listings.parentNode.removeEventListener('scroll', this.handleScroll);
}
onSortChange (e) {
this.props.dispatch({type: 'UPDATE_ROUTE', action: {
params: {
sort: e.target.value
}
}});
this.props.dispatch({type: 'SORT_CHANGE', action: {
sort: e.target.value
}});
}
render () {
const {listings = (new Map()), totalCount, sort} = this.props;
return (
{totalCount} rezultata
{this.renderListings()}
)
}
}