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

235 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-04-06 02:05:06 +02:00
import { markSeen } from './api';
2017-04-05 00:14:30 +02:00
const setMaxPrice = ({ type, action }, component) => {
const maxPrice = parseFloat(action.maxPrice);
2017-04-04 04:36:52 +02:00
component.setState({
filters: {
...component.state.filters,
2017-04-05 00:14:30 +02:00
maxPrice: isNaN(maxPrice) ? undefined : maxPrice,
priceDirty: true
2017-04-04 04:36:52 +02:00
}
2017-04-05 00:14:30 +02:00
});
};
const setMinPrice = ({ type, action }, component) => {
const minPrice = parseFloat(action.minPrice);
component.setState({
filters: {
...component.state.filters,
minPrice: isNaN(minPrice) ? undefined : minPrice,
priceDirty: true
}
});
};
2017-04-04 04:36:52 +02:00
2017-04-05 00:14:30 +02:00
const setMinSize = ({ type, action }, component) => {
const minSize = parseFloat(action.minSize);
2017-03-31 14:31:56 +02:00
component.setState({
filters: {
2017-04-04 03:00:04 +02:00
...component.state.filters,
2017-04-05 00:14:30 +02:00
minSize: isNaN(minSize) ? undefined : minSize,
sizeDirty: true
2017-03-31 14:31:56 +02:00
}
2017-04-05 00:14:30 +02:00
});
};
const setMaxSize = ({ type, action }, component) => {
const maxSize = parseFloat(action.maxSize);
component.setState({
filters: {
...component.state.filters,
maxSize: isNaN(maxSize) ? undefined : maxSize,
sizeDirty: true
}
});
};
2017-03-31 14:31:56 +02:00
2017-04-05 00:14:30 +02:00
const viewListingDetails = ({ type, action }, component) => {
2017-04-06 03:20:13 +02:00
const scrollElem = document.querySelector('.right-content');
component.savedScrollTop = scrollElem.scrollTop;
2017-03-31 14:31:56 +02:00
component.setState({
listingDetails: true,
listingId: action.id,
2017-04-02 03:31:39 +02:00
descriptionExpanded: false,
imageIndex: 0
2017-04-06 02:05:06 +02:00
}, () => {
markSeen(action.id);
const m = component.findMarker(action.id);
if (m) {
m.marker.setIcon(component.selectedMarkerIcon());
}
2017-04-06 03:20:13 +02:00
scrollElem.scrollTop = 0
2017-04-05 00:14:30 +02:00
});
};
2017-03-31 14:31:56 +02:00
2017-04-05 00:14:30 +02:00
const listingsLoaded = ({ type, action }, component) => {
const currentListings = new Map();
2017-04-02 03:31:39 +02:00
2017-04-05 00:14:30 +02:00
for (const listing of action.listings) {
2017-04-06 01:11:51 +02:00
currentListings.set(listing._id, listing);
2017-04-02 03:31:39 +02:00
}
2017-03-31 14:31:56 +02:00
component.setState({
2017-04-02 03:31:39 +02:00
listings: currentListings
}, () => {
component.markers = action.newMarkers;
2017-03-31 14:31:56 +02:00
});
2017-04-05 00:14:30 +02:00
};
2017-03-31 14:31:56 +02:00
2017-04-05 00:14:30 +02:00
const expandDescription = ({ type, action }, component) => {
2017-03-31 14:31:56 +02:00
component.setState({
descriptionExpanded: true
});
2017-04-05 00:14:30 +02:00
};
2017-03-31 14:31:56 +02:00
2017-04-05 00:14:30 +02:00
const prevImage = ({ type, action }, component) => {
2017-04-02 03:31:39 +02:00
const index = component.state.imageIndex;
if (index > 0) {
component.setState({
imageIndex: index - 1
});
}
2017-04-05 00:14:30 +02:00
};
2017-04-02 03:31:39 +02:00
2017-04-05 00:14:30 +02:00
const nextImage = ({ type, action }, component) => {
2017-04-02 03:31:39 +02:00
const index = component.state.imageIndex;
component.setState({
imageIndex: index + 1
});
2017-04-05 00:14:30 +02:00
};
2017-04-02 03:31:39 +02:00
2017-04-05 00:14:30 +02:00
const viewImage = ({ type, action }, component) => {
2017-04-02 03:31:39 +02:00
component.setState({
imageIndex: action.index
});
2017-04-05 00:14:30 +02:00
};
2017-04-02 03:31:39 +02:00
2017-04-05 00:14:30 +02:00
const searchPlaceChanged = ({ type, action }, component) => {
component.setState({
listingDetails: false
});
2017-04-05 00:14:30 +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 03:00:04 +02:00
}
2017-04-05 00:14:30 +02:00
},
() => {
component.refreshListings();
2017-04-04 03:00:04 +02:00
}
2017-04-05 00:14:30 +02:00
);
};
const updateSearch = ({ type, action }, component) => {
component.setState(
{
filters: {
...component.state.filters,
sizeDirty: false,
priceDirty: false
}
},
() => {
component.refreshListings();
2017-04-04 04:36:52 +02:00
}
2017-04-05 00:14:30 +02:00
);
};
2017-04-04 03:00:04 +02:00
2017-04-05 02:02:43 +02:00
const setCategory = ({type, action}, component) => {
const prevCategory = component.state.filters.category || {};
component.setState(
{
filters: {
...component.state.filters,
category: {
...prevCategory,
[action.category]: !prevCategory[action.category]
}
}
},
() => {
component.refreshListings();
}
);
2017-04-06 01:11:51 +02:00
};
const onListingMouseOver = ({type, action}, component) => {
const marker = component.findMarker(action.id);
if (marker) {
2017-04-06 02:05:06 +02:00
const seen = component.isSeen(action.id);
if (seen) {
marker.marker.setIcon(component.visitedHoveredMarkerIcon());
} else {
marker.marker.setIcon(component.hoveredMarkerIcon());
}
2017-04-06 01:11:51 +02:00
marker.marker.setAnimation(google.maps.Animation.BOUNCE);
setTimeout(() => {
marker.marker.setAnimation(null);
2017-04-06 02:05:06 +02:00
if (seen) {
marker.marker.setIcon(component.visitedMarkerIcon());
} else {
marker.marker.setIcon(component.defaultMarkerIcon());
}
2017-04-06 01:11:51 +02:00
} , 710);
}
};
2017-04-05 02:02:43 +02:00
2017-04-06 02:37:18 +02:00
const backToResults = ({type, action}, component) => {
const prevSelected = component.findMarker(component.state.listingId);
component.setState({
listingId: undefined,
listingDetails: false
}, () => {
if (prevSelected) {
prevSelected.marker.setIcon(component.visitedMarkerIcon());
}
2017-04-06 03:20:13 +02:00
const scrollElem = document.querySelector('.right-content');
scrollElem.scrollTop = component.savedScrollTop;
2017-04-06 02:37:18 +02:00
});
}
2017-03-31 14:31:56 +02:00
const handlers = {
2017-04-05 00:14:30 +02:00
SET_MIN_PRICE: setMinPrice,
SET_MAX_PRICE: setMaxPrice,
SET_MIN_SIZE: setMinSize,
SET_MAX_SIZE: setMaxSize,
LISTINGS_LOADED: listingsLoaded,
EXPAND_DESCRIPTION: expandDescription,
PREV_IMAGE: prevImage,
NEXT_IMAGE: nextImage,
VIEW_IMAGE: viewImage,
SEARCH_PLACE_CHANGED: searchPlaceChanged,
SET_ROOMS: setRooms,
VIEW_LISTING_DETAILS: viewListingDetails,
2017-04-05 02:02:43 +02:00
UPDATE_SEARCH: updateSearch,
2017-04-06 01:11:51 +02:00
SET_CATEGORY: setCategory,
2017-04-06 02:37:18 +02:00
ON_LISTING_MOUSE_OVER: onListingMouseOver,
BACK_TO_RESULTS: backToResults
2017-04-05 00:14:30 +02:00
};
export const handleMessage = ({ type, action }, component) => {
2017-03-31 14:31:56 +02:00
if (!handlers[type]) {
2017-04-05 00:14:30 +02:00
throw new `Unhandled message: ${type}`();
2017-03-31 14:31:56 +02:00
}
2017-04-05 00:14:30 +02:00
return handlers[type]({ type, action }, component);
};