Files
old-kivi/web/lib/handlers.js
2017-04-02 04:49:22 +02:00

83 lines
1.8 KiB
JavaScript

const setMinPrice = ({type, action}, component) => {
component.setState({
filters: {
minPrice: parseFloat(action.minPrice) || 0
}
})
}
const viewListingDetails= ({type, action}, component) => {
component.setState({
listingDetails: true,
listingId: action.id,
descriptionExpanded: false,
imageIndex: 0
})
}
const listingsLoaded = ({type, action}, component) => {
const currentListings = new Map(); //component.state.listings;
for(const listing of action.listings) {
currentListings.set(listing.url, listing)
}
component.setState({
listings: currentListings
});
}
const expandDescription = ({type, action}, component) => {
component.setState({
descriptionExpanded: true
});
}
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
});
}
const handlers = {
'SET_MIN_PRICE': setMinPrice,
'LISTINGS_LOADED': listingsLoaded,
'EXPAND_DESCRIPTION': expandDescription,
'PREV_IMAGE': prevImage,
'NEXT_IMAGE': nextImage,
'VIEW_IMAGE': viewImage,
'SEARCH_PLACE_CHANGED': searchPlaceChanged,
'VIEW_LISTING_DETAILS': viewListingDetails
}
export const handleMessage = ({type, action}, component) => {
if (!handlers[type]) {
throw new `Unhandled message: ${type}`;
}
return handlers[type]({type, action}, component);
}