Skip to content

Commit 29a427a

Browse files
authored
Merge pull request #43 from BuildFire/fix/intro-search
fix(intro search): handle user position in the intro search
2 parents f5b32cf + 18196ce commit 29a427a

7 files changed

Lines changed: 132 additions & 106 deletions

File tree

src/control/content/js/listView/index.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import PinnedLocationsList from "./pinnedLocationsList";
88
import Location from "../../../../entities/Location";
99
import loadAreaRadiusMap from "./introMap";
1010
import state from "../../state";
11-
12-
const SearchLocationsModes = {
13-
All: "All",
14-
UserPosition: "UserPosition",
15-
AreaRadius: "AreaRadius",
16-
};
11+
import constants from "../../../../widget/js/constants";
1712

1813
const listViewSection = document.querySelector("#main");
1914

@@ -55,7 +50,7 @@ window.onSortLocationsChanged = (sorting) => {
5550

5651
window.onShowLocationsModeChanged = (showMode) => {
5752
const areaRadiusOptionsContainer = document.querySelector("#areaRadiusOptionsContainer");
58-
if (showMode === SearchLocationsModes.AreaRadius) {
53+
if (showMode === constants.SearchLocationsModes.AreaRadius) {
5954
areaRadiusOptionsContainer?.classList?.remove('hidden');
6055
} else {
6156
areaRadiusOptionsContainer?.classList?.add('hidden');

src/widget/css/main.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ section#categories.overlay {
4646
border: 0;
4747
overflow: visible;
4848
background-color: transparent;
49+
height: 2.5rem;
4950
}
5051
.search-field.mdc-text-field .mdc-text-field__input {
5152
font-size: 13px;
@@ -100,6 +101,8 @@ section#categories.overlay {
100101
margin-left: -15px;
101102
margin-right: -15px;
102103
margin-top: 18px;
104+
height: 32px;
105+
line-height: 32px;
103106
}
104107
.header-qf .mdc-chip {
105108
border-radius: 8px;

src/widget/js/constants/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ const getMapStyle = (name) => {
1616

1717
const getDefaultLocation = () => ({ lat: 32.7182625, lng: -117.1601157 });
1818

19-
export default { getMapStyle, getDefaultLocation };
19+
const SearchLocationsModes = {
20+
All: "All",
21+
UserPosition: "UserPosition",
22+
AreaRadius: "AreaRadius",
23+
};
24+
25+
export default { getMapStyle, getDefaultLocation, SearchLocationsModes };

src/widget/js/util/ui.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,16 @@ export const adjustMapHeight = () => {
3939
const {
4040
design,
4141
filter,
42-
sorting,
43-
bookmarks
4442
} = state.settings;
4543
const mainMapContainer = document.querySelector('#mainMapContainer');
46-
const mapCenterBtn = document.querySelector('#mapCenterBtn');
4744

48-
let baseMapHeight = 164;
49-
let baseCenterBtnBottom = 120;
45+
let baseMapHeight = 210;
5046

51-
if (!design.hideQuickFilter || filter.allowFilterByArea) {
52-
baseMapHeight += 46;
53-
baseCenterBtnBottom -= 10;
54-
}
55-
56-
if (sorting.hideSorting && filter.hidePriceFilter && filter.hideOpeningHoursFilter && (!bookmarks.enabled || !bookmarks.allowForFilters)) {
57-
baseMapHeight -= 36;
58-
baseCenterBtnBottom -= 30;
47+
if (design.hideQuickFilter && !filter.allowFilterByArea) {
48+
baseMapHeight = 160;
5949
}
6050

6151
mainMapContainer.style.height = `calc(100vh - ${baseMapHeight}px)`;
62-
mapCenterBtn.style.bottom = `${baseCenterBtnBottom}px`;
6352
};
6453

6554
export const navigateTo = (template) => {

src/widget/services/search/introSearchService.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,53 @@
22
import { buildOpenNowCriteria, buildSearchCriteria } from "./shared";
33
import state from "../../js/state";
44
import WidgetController from "../../widget.controller";
5-
6-
const SearchLocationsModes = {
7-
All: "All",
8-
UserPosition: "UserPosition",
9-
AreaRadius: "AreaRadius",
10-
};
5+
import constants from "../../js/constants";
116

127
const IntroSearchService = {
13-
_setupNearPipelines(query) {
14-
const pipelines = [];
15-
16-
const $geoNear = {
17-
near: { type: "Point", coordinates: [state.currentLocation.lng, state.currentLocation.lat] },
18-
key: "_buildfire.geo",
19-
maxDistance: 100000,
20-
distanceField: "distance",
21-
query: { ...query }
22-
};
23-
pipelines.push({ $geoNear });
24-
25-
if (state.searchCriteria.sort) {
26-
const $sort = {};
27-
$sort[state.introSort.sortBy] = state.introSort.order;
28-
pipelines.push({ $sort });
8+
_getUserCoordinates() {
9+
const coordinates = [];
10+
if (state.userPosition && state.userPosition.latitude && state.userPosition.longitude) {
11+
coordinates.push(state.userPosition.longitude);
12+
coordinates.push(state.userPosition.latitude);
13+
} else {
14+
const defaultPosition = constants.getDefaultLocation();
15+
coordinates.push(defaultPosition.lng);
16+
coordinates.push(defaultPosition.lat);
2917
}
30-
31-
return pipelines;
18+
19+
return coordinates;
3220
},
3321

34-
_setupAreaRadiusPipelines(query) {
22+
_setUpIntroGeoQuery(query) {
3523
const pipelines = [];
24+
let centerSphere, radius;
25+
26+
if (state.settings.introductoryListView.searchOptions?.mode === constants.SearchLocationsModes.AreaRadius) {
27+
centerSphere = [
28+
state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.lng || 1,
29+
state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.lat || 1,
30+
];
31+
const _radiusMiles = state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.radius || 1;
32+
radius = _radiusMiles / 3963.2; // convert miles to radians
33+
} else {
34+
centerSphere = IntroSearchService._getUserCoordinates();
35+
radius = 100 / 6378.1; // 100 km in radians
36+
}
3637

37-
const lng = state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.lng || 1;
38-
const lat = state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.lat || 1;
39-
const radius = state.settings.introductoryListView.searchOptions?.areaRadiusOptions?.radius || 1;
38+
if (state.currentLocation) { // this is for user search by location name
39+
centerSphere = [state.currentLocation.lng, state.currentLocation.lat];
40+
}
4041

41-
const $geoNear = {
42-
near: { type: "Point", coordinates: [state.currentLocation.lng, state.currentLocation.lat] },
42+
const $geoNear = { // near user position to calculate distance for each location
43+
near: { type: "Point", coordinates: IntroSearchService._getUserCoordinates() },
4344
key: "_buildfire.geo",
4445
distanceField: "distance",
4546
query: { ...query }
4647
};
4748
const $match = {
4849
"_buildfire.geo": {
49-
$geoWithin: {
50-
$centerSphere: [[lng, lat], radius / 3963.2] // convert miles to radians
50+
$geoWithin: { // find locations within a specific area
51+
$centerSphere: [centerSphere, radius]
5152
}
5253
}
5354
};
@@ -91,19 +92,16 @@ const IntroSearchService = {
9192
const query = buildSearchCriteria();
9293
let pipelines;
9394

94-
if (state.settings.introductoryListView.searchOptions?.mode === SearchLocationsModes.All) {
95+
if (state.settings.introductoryListView.searchOptions?.mode === constants.SearchLocationsModes.All && !state.currentLocation) {
9596
if (!state.fetchingAllNearReached) {
96-
pipelines = this._setupNearPipelines(query);
97+
pipelines = this._setUpIntroGeoQuery(query);
9798
} else {
9899
pipelines = this._setupOtherLocationsPipelines(query);
99100
}
100-
} else if (state.settings.introductoryListView.searchOptions?.mode === SearchLocationsModes.AreaRadius) {
101-
state.fetchingAllNearReached = true;
102-
pipelines = this._setupAreaRadiusPipelines(query);
103101
} else {
104102
// the default search mode is UserPosition
105103
state.fetchingAllNearReached = true;
106-
pipelines = this._setupNearPipelines(query);
104+
pipelines = this._setUpIntroGeoQuery(query);
107105
}
108106

109107
return pipelines;
@@ -125,7 +123,7 @@ const IntroSearchService = {
125123
state.fetchingNextPage = false;
126124
state.fetchingEndReached = aggregateLocations.length < state.searchCriteria.pageSize && state.fetchingAllNearReached;
127125

128-
if (aggregateLocations.length < state.searchCriteria.pageSize && !state.fetchingAllNearReached && state.settings.introductoryListView.searchOptions?.mode === SearchLocationsModes.All) {
126+
if (aggregateLocations.length < state.searchCriteria.pageSize && !state.fetchingAllNearReached && state.settings.introductoryListView.searchOptions?.mode === constants.SearchLocationsModes.All) {
129127
state.fetchingAllNearReached = true;
130128
state.searchCriteria.page = 0;
131129
state.printOtherLocationMessage = true;

src/widget/services/search/mapSearchService.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,32 @@ import WidgetController from '../../widget.controller';
33
import mapSearchControl from '../../js/map/search-control';
44
import state from "../../js/state";
55
import { buildOpenNowCriteria, buildSearchCriteria } from './shared';
6+
import constants from '../../js/constants';
67

78
const MapSearchService = {
9+
getMapCenterPoint() {
10+
let centerPoint = null;
11+
if (state.settings.map.initialArea && state.settings.map.initialAreaCoordinates.lat && state.settings.map.initialAreaCoordinates.lng) {
12+
centerPoint = { ...state.settings.map.initialAreaCoordinates };
13+
} else if (state.userPosition) {
14+
centerPoint = {
15+
lat: state.userPosition.latitude,
16+
lng: state.userPosition.longitude
17+
};
18+
} else {
19+
centerPoint = constants.getDefaultLocation();
20+
}
21+
22+
return centerPoint;
23+
},
824

925
_getNearestLocation() {
1026
const pipelines = [];
1127
const query = buildSearchCriteria();
28+
const centerPoint = this.getMapCenterPoint();
1229

1330
const $geoNear = {
14-
near: { type: "Point", coordinates: [state.currentLocation.lng, state.currentLocation.lat] },
31+
near: { type: "Point", coordinates: [centerPoint.lng, centerPoint.lat] },
1532
key: "_buildfire.geo",
1633
distanceField: "distance",
1734
query: { ...query },
@@ -35,6 +52,7 @@ const MapSearchService = {
3552
const pipelines = [];
3653
const query = buildSearchCriteria();
3754
const { mapBounds } = state.maps.map;
55+
const centerPoint = this.getMapCenterPoint();
3856

3957
if (!mapBounds || !Array.isArray(mapBounds)) {
4058
return resolve({});
@@ -60,8 +78,8 @@ const MapSearchService = {
6078
if (state.searchCriteria.sort) {
6179
if (state.searchCriteria.sort.sortBy === 'distance' && state.userPosition && state.userPosition.latitude && state.userPosition.longitude) {
6280
const centerMapPoint = state.maps.map.getCenter();
63-
const lat1 = Math.abs(state.currentLocation.lat);
64-
const lng1 = Math.abs(state.currentLocation.lng);
81+
const lat1 = Math.abs(centerPoint.lat);
82+
const lng1 = Math.abs(centerPoint.lng);
6583
const lat2 = Math.abs(centerMapPoint.lat());
6684
const lng2 = Math.abs(centerMapPoint.lng());
6785
if (Math.abs(lat1 - lat2) >= Math.abs(lng1 - lng2)) {

0 commit comments

Comments
 (0)