import React from 'react'
import {findDOMNode} from 'react-dom'
import {formatPrice, listingImageUrl} from '../lib/helpers'
import {loadListing} from '../lib/api'
export default class Listings extends React.Component {
constructor (props) {
super(props)
this.handleTouchMove = e => {
const node = e.target
const offset = node.scrollHeight - node.scrollTop - node.clientHeight
console.log('HANDLE TOUCH MOVE OFFSET', offset)
}
this.handleScroll = e => {
console.log('scrolling');
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)
listings.parentNode.addEventListener('touchmove', this.handleTouchMove)
}
removeScrollListener () {
const listings = findDOMNode(this.refs.listings)
listings.parentNode.removeEventListener('scroll', this.handleScroll)
listings.parentNode.removeEventListener('touchmove', this.handleTouchMove)
}
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
}
})
}
onFiltersClick (e) {
e.preventDefault()
this.props.dispatch({
type: 'OPEN_FILTERS'
})
}
resultsString (count) {
if (count == "1") {
return "rezultat"
}
return "rezultata"
}
render () {
const {listings = new Map(), totalCount, sort} = this.props
return (
{totalCount} {this.resultsString(totalCount)}
Filteri
{this.renderListings()}
)
}
}