Files
old-kivi/web/lib/handlers.js

126 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-04-04 04:36:52 +02:00
const setMaxPrice = ({type, action}, component) => {
component.setState({
filters: {
...component.state.filters,
maxPrice: parseFloat(action.maxPrice),
dirty: true
}
})
}
2017-03-31 14:31:56 +02:00
const setMinPrice = ({type, action}, component) => {
component.setState({
filters: {
2017-04-04 03:00:04 +02:00
...component.state.filters,
2017-04-04 04:36:52 +02:00
minPrice: parseFloat(action.minPrice),
dirty: true
2017-03-31 14:31:56 +02:00
}
})
}
const viewListingDetails= ({type, action}, component) => {
component.setState({
listingDetails: true,
listingId: action.id,
2017-04-02 03:31:39 +02:00
descriptionExpanded: false,
imageIndex: 0
2017-03-31 14:31:56 +02:00
})
}
const listingsLoaded = ({type, action}, component) => {
const currentListings = new Map(); //component.state.listings;
2017-04-02 03:31:39 +02:00
for(const listing of action.listings) {
currentListings.set(listing.url, listing)
}
2017-03-31 14:31:56 +02:00
component.setState({
2017-04-02 03:31:39 +02:00
listings: currentListings
2017-03-31 14:31:56 +02:00
});
}
const expandDescription = ({type, action}, component) => {
component.setState({
descriptionExpanded: true
});
}
2017-04-02 03:31:39 +02:00
const prevImage = ({type, action}, component) => {
const index = component.state.imageIndex;
if (index > 0) {
component.setState({
imageIndex: index - 1
});
}
}
const nextImage = ({type, action}, component) => {
const index = component.state.imageIndex;
component.setState({
imageIndex: index + 1
});
}
const viewImage = ({type, action}, component) => {
component.setState({
imageIndex: action.index
});
}
const searchPlaceChanged = ({type, action}, component) => {
component.setState({
listingDetails: false
});
}
2017-04-04 03:00:04 +02:00
const setRooms = ({type, action}, component) => {
const prevRooms = (component.state.filters.rooms || {});
component.setState({
filters: {
...component.state.filters,
rooms: {
...prevRooms,
[action.rooms]: !prevRooms[action.rooms]
}
}
2017-04-04 04:36:52 +02:00
}, () => {
component.refreshListings();
});
}
const updateSearch = ({type, action}, component) => {
console.log('updating search');
component.setState({
filters: {
...component.state.filters,
dirty: false
}
}, () => {
component.refreshListings();
2017-04-04 03:00:04 +02:00
});
}
2017-03-31 14:31:56 +02:00
const handlers = {
'SET_MIN_PRICE': setMinPrice,
2017-04-04 04:36:52 +02:00
'SET_MAX_PRICE': setMaxPrice,
2017-03-31 14:31:56 +02:00
'LISTINGS_LOADED': listingsLoaded,
'EXPAND_DESCRIPTION': expandDescription,
2017-04-02 03:31:39 +02:00
'PREV_IMAGE': prevImage,
'NEXT_IMAGE': nextImage,
'VIEW_IMAGE': viewImage,
'SEARCH_PLACE_CHANGED': searchPlaceChanged,
2017-04-04 03:00:04 +02:00
'SET_ROOMS': setRooms,
2017-04-04 04:36:52 +02:00
'VIEW_LISTING_DETAILS': viewListingDetails,
'UPDATE_SEARCH': updateSearch
2017-03-31 14:31:56 +02:00
}
export const handleMessage = ({type, action}, component) => {
if (!handlers[type]) {
throw new `Unhandled message: ${type}`;
}
return handlers[type]({type, action}, component);
}