Files
old-kivi/web/src/components/Listings.js

215 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-04-11 10:43:05 +02:00
import React from 'react'
import {findDOMNode} from 'react-dom'
import {formatPrice, listingImageUrl} from '../lib/helpers'
import {loadListing} from '../lib/api'
2016-11-07 11:17:48 +01:00
export default class Listings extends React.Component {
2017-04-11 10:43:05 +02:00
constructor (props) {
super(props)
2017-04-07 04:02:01 +02:00
2017-04-14 02:00:38 +02:00
this.handleTouchMove = e => {
const node = e.target
const offset = node.scrollHeight - node.scrollTop - node.clientHeight
console.log('HANDLE TOUCH MOVE OFFSET', offset)
}
2017-04-11 10:43:05 +02:00
this.handleScroll = e => {
2017-04-14 02:00:38 +02:00
console.log('scrolling');
2017-04-11 10:43:05 +02:00
const node = e.target
const offset = node.scrollHeight - node.scrollTop - node.clientHeight
2017-04-07 04:02:01 +02:00
2017-04-07 04:50:46 +02:00
if (this.props && this.props.loadingMore) {
2017-04-11 10:43:05 +02:00
return
2017-04-07 04:50:46 +02:00
}
2017-04-07 04:02:01 +02:00
if (offset < 50) {
2017-04-11 10:43:05 +02:00
this.props.dispatch({type: 'LOAD_MORE_LISTINGS'})
2017-04-07 04:02:01 +02:00
}
}
}
2017-04-11 10:43:05 +02:00
onListingClick (id) {
2017-04-08 00:06:06 +02:00
loadListing(id).then(l => l.text()).then(l => {
2017-04-11 10:43:05 +02:00
this.props.dispatch({
type: 'UPDATE_ROUTE',
action: {
toDispatch: {
type: 'VIEW_LISTING_DETAILS',
action: {
id,
listing: JSON.parse(l)
}
},
params: {
listingId: id
2017-04-09 19:34:08 +02:00
}
}
2017-04-11 10:43:05 +02:00
})
2017-04-09 19:34:08 +02:00
2017-04-11 10:43:05 +02:00
this.props.dispatch({
type: 'VIEW_LISTING_DETAILS',
action: {
id,
listing: JSON.parse(l)
}
})
})
}
2017-04-06 01:11:51 +02:00
onMouseEnter (id) {
this.props.dispatch({
type: 'ON_LISTING_MOUSE_OVER',
action: {
id
}
2017-04-11 10:43:05 +02:00
})
2017-04-06 01:11:51 +02:00
}
renderListings () {
2017-04-11 10:43:05 +02:00
const {listings = new Map()} = this.props
2017-04-06 02:16:33 +02:00
if (listings.size === 0) {
2017-04-11 10:43:05 +02:00
return (
<div className="listings-no-results">
<i className="fa fa-frown-o fa-frown-o-custom" aria-hidden="true" />
<h4>Nema rezultata</h4>
<h5>
Nema oglasa koji ispunjavaju vaše uslove pretrage.
</h5>
</div>
)
2017-04-06 02:16:33 +02:00
}
2017-04-11 10:43:05 +02:00
const rendered = []
2017-04-11 10:43:05 +02:00
for (const l of listings.values()) {
const {images} = l
rendered.push(
<div
key={l._id}
2017-04-06 01:11:51 +02:00
onMouseEnter={this.onMouseEnter.bind(this, l._id)}
className="property-list-item"
2017-04-11 10:43:05 +02:00
onClick={this.onListingClick.bind(this, l._id)}
>
<div className="pli-image">
2017-04-11 10:43:05 +02:00
<img src={listingImageUrl(images[0])} alt="" />
</div>
<div className="pli-details">
2017-04-05 00:14:30 +02:00
<div className="price">{formatPrice(l.price)}</div>
2017-04-11 10:43:05 +02:00
<div className="description">
{l.rooms ? `${l.rooms} sobe, ` : null}
{l.size ? `${l.size}m2` : null}
</div>
<div className="address">
<div className="street">
{l.address}
</div>
<div className="location">
{l.location}
</div>
</div>
2017-04-08 02:11:54 +02:00
<div className="hours-ago">Prije {l.time}</div>
</div>
</div>
2017-04-11 10:43:05 +02:00
)
2016-11-15 00:03:59 +01:00
}
2017-04-11 10:43:05 +02:00
return rendered
2016-11-15 00:03:59 +01:00
}
2017-04-07 04:02:01 +02:00
componentDidMount () {
2017-04-11 10:43:05 +02:00
this.attachScrollListener()
2017-04-07 04:02:01 +02:00
}
componentWillUnmount () {
2017-04-11 10:43:05 +02:00
this.removeScrollListener()
2017-04-07 04:02:01 +02:00
}
attachScrollListener () {
2017-04-11 10:43:05 +02:00
const listings = findDOMNode(this.refs.listings)
listings.parentNode.addEventListener('scroll', this.handleScroll)
2017-04-14 02:00:38 +02:00
listings.parentNode.addEventListener('touchmove', this.handleTouchMove)
2017-04-07 04:02:01 +02:00
}
removeScrollListener () {
2017-04-11 10:43:05 +02:00
const listings = findDOMNode(this.refs.listings)
listings.parentNode.removeEventListener('scroll', this.handleScroll)
2017-04-14 02:00:38 +02:00
listings.parentNode.removeEventListener('touchmove', this.handleTouchMove)
2017-04-07 04:02:01 +02:00
}
2017-04-08 02:11:54 +02:00
onSortChange (e) {
2017-04-11 10:43:05 +02:00
this.props.dispatch({
type: 'UPDATE_ROUTE',
action: {
params: {
sort: e.target.value
}
2017-04-09 19:34:08 +02:00
}
2017-04-11 10:43:05 +02:00
})
2017-04-09 19:34:08 +02:00
2017-04-11 10:43:05 +02:00
this.props.dispatch({
type: 'SORT_CHANGE',
action: {
sort: e.target.value
}
})
2017-04-08 02:11:54 +02:00
}
2017-04-07 04:02:01 +02:00
2017-04-17 12:02:02 +02:00
onFiltersClick (e) {
e.preventDefault()
this.props.dispatch({
type: 'OPEN_FILTERS'
})
}
2017-04-17 15:17:45 +02:00
resultsString (count) {
if (count == "1") {
return "rezultat"
}
return "rezultata"
}
2017-04-07 04:02:01 +02:00
render () {
2017-04-11 10:43:05 +02:00
const {listings = new Map(), totalCount, sort} = this.props
2016-11-07 11:17:48 +01:00
return (
2017-04-07 04:02:01 +02:00
<div ref="listings" className="listings">
2016-11-09 15:14:16 +01:00
<div className="listings-header">
2016-11-15 00:03:59 +01:00
<div className="select-container">
<div className="select-group">
2017-04-08 02:11:54 +02:00
<select
value={sort}
onChange={this.onSortChange.bind(this)}
name="listings-type"
2017-04-11 10:43:05 +02:00
id="listings-type"
>
2017-04-08 02:11:54 +02:00
<option value="relevance">Relevantno</option>
<option value="newest">Najnovije</option>
<option value="price-min">Cijena: od najmanje</option>
<option value="price-max">Cijena: od najvece</option>
2016-11-15 00:03:59 +01:00
</select>
</div>
2016-11-09 15:14:16 +01:00
</div>
<div className="listings-count">
2017-04-17 15:17:45 +02:00
{totalCount} {this.resultsString(totalCount)}
2016-11-09 15:14:16 +01:00
</div>
2017-04-17 12:02:02 +02:00
<div
onClick={this.onFiltersClick.bind(this)}
className="listings-filter">
Filteri
</div>
2016-11-09 15:14:16 +01:00
</div>
2016-11-10 18:03:17 +01:00
<div className="listings-items">
{this.renderListings()}
2016-11-10 18:03:17 +01:00
</div>
2017-04-11 10:43:05 +02:00
</div>
)
2016-11-07 11:17:48 +01:00
}
}