flatisfy/flatisfy/web/js_src/store/getters.js

78 lines
2.6 KiB
JavaScript
Raw Normal View History

2021-01-29 19:24:46 +01:00
import { findFlatGPS, costFilter } from '../tools'
export default {
allFlats: state => state.flats,
flat: (state, getters) => id => state.flats.find(flat => flat.id === id),
isLoading: state => state.loading > 0,
postalCodesFlatsBuckets: (state, getters) => filter => {
const postalCodeBuckets = {}
state.flats.forEach(flat => {
if (!filter || filter(flat)) {
const postalCode = flat.flatisfy_postal_code.postal_code
if (!postalCodeBuckets[postalCode]) {
postalCodeBuckets[postalCode] = {
'name': flat.flatisfy_postal_code.name,
'flats': []
}
}
postalCodeBuckets[postalCode].flats.push(flat)
}
})
return postalCodeBuckets
},
flatsMarkers: (state, getters) => (router, filter) => {
const markers = []
state.flats.forEach(flat => {
if (filter && filter(flat)) {
const gps = findFlatGPS(flat)
if (gps) {
2021-01-15 16:10:23 +01:00
const previousMarker = markers.find(
marker => marker.gps[0] === gps[0] && marker.gps[1] === gps[1]
)
2021-01-15 16:10:23 +01:00
if (previousMarker) {
2021-01-26 14:44:24 +01:00
// randomize position a bit
// gps[0] += (Math.random() - 0.5) / 500
// gps[1] += (Math.random() - 0.5) / 500
}
2021-01-15 16:10:23 +01:00
const href = router.resolve({ name: 'details', params: { id: flat.id }}).href
2021-01-29 19:24:46 +01:00
const cost = flat.cost
? costFilter(flat.cost, flat.currency)
: ''
2021-01-15 16:10:23 +01:00
markers.push({
'title': '',
2021-01-18 10:40:47 +01:00
'content': '<a href="' + href + '">' + flat.title + '</a>' + cost,
2021-01-21 10:36:00 +01:00
'gps': gps,
'flat_id': flat.id
2021-01-15 16:10:23 +01:00
})
}
}
})
return markers
},
allTimeToPlaces: state => {
2017-06-20 13:37:22 +02:00
const places = {}
Object.keys(state.timeToPlaces).forEach(constraint => {
2017-06-20 13:37:22 +02:00
const constraintTimeToPlaces = state.timeToPlaces[constraint]
Object.keys(constraintTimeToPlaces).forEach(name => {
places[name] = constraintTimeToPlaces[name]
2017-06-20 13:37:22 +02:00
})
})
return places
},
2017-06-20 13:37:22 +02:00
timeToPlaces: (state, getters) => (constraintName) => {
return state.timeToPlaces[constraintName]
},
metadata: state => state.metadata
}