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

76 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-03-31 14:31:56 +02:00
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,
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) => {
2017-04-02 03:31:39 +02:00
const currentListings = component.state.listings;
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
});
}
2017-03-31 14:31:56 +02:00
const handlers = {
'SET_MIN_PRICE': setMinPrice,
'LISTINGS_LOADED': listingsLoaded,
'EXPAND_DESCRIPTION': expandDescription,
2017-04-02 03:31:39 +02:00
'PREV_IMAGE': prevImage,
'NEXT_IMAGE': nextImage,
'VIEW_IMAGE': viewImage,
2017-03-31 14:31:56 +02:00
'VIEW_LISTING_DETAILS': viewListingDetails
}
export const handleMessage = ({type, action}, component) => {
if (!handlers[type]) {
throw new `Unhandled message: ${type}`;
}
return handlers[type]({type, action}, component);
}