Skip to content

Commit

Permalink
Update Geolocation to remove all location fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nmanu1 committed Dec 5, 2022
1 parent ff5293e commit 893b7b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/hooks/useGeolocationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { executeSearch } from '../utils/search-operations';
import { getUserLocation } from '../utils/location-operations';
import { useCallback, useState } from 'react';

const LOCATION_FIELD_ID = 'builtin.location';
const GEOLOCATION_FIELD_ID = 'builtin.location';
const LOCATION_FIELD_IDS = [GEOLOCATION_FIELD_ID, 'builtin.region', 'address.countryCode'];
const METERS_PER_MILE = 1609.344;

/**
Expand Down Expand Up @@ -49,7 +50,7 @@ export function useGeolocationHandler({
selected: true,
filter: {
kind: 'fieldValue',
fieldId: LOCATION_FIELD_ID,
fieldId: GEOLOCATION_FIELD_ID,
matcher: Matcher.Near,
value: {
lat: latitude,
Expand All @@ -60,7 +61,7 @@ export function useGeolocationHandler({
};
const nonLocationFilters = staticFilters.filter(filter => {
return !(filter.filter.kind === 'fieldValue'
&& filter.filter.fieldId === LOCATION_FIELD_ID);
&& LOCATION_FIELD_IDS.includes(filter.filter.fieldId));
});
searchActions.setStaticFilters([...nonLocationFilters, locationFilter]);
executeSearch(searchActions);
Expand Down
28 changes: 24 additions & 4 deletions tests/components/Geolocation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ const mockedStateWithFilters: Partial<State> = {
},
}
},
{
displayName: 'Virginia, US',
selected: true,
filter: {
kind: 'fieldValue',
fieldId: 'builtin.region',
matcher: Matcher.Equals,
value: 'US-VA',
}
},
{
displayName: 'United States',
selected: true,
filter: {
kind: 'fieldValue',
fieldId: 'address.countryCode',
matcher: Matcher.Equals,
value: 'US',
}
},
{
displayName: 'My name',
selected: true,
Expand Down Expand Up @@ -141,7 +161,7 @@ describe('custom click handler', () => {
describe('default click handler', () => {
it('sets a location filter using provided radius', async () => {
const actions = spyOnActions();
render(<Geolocation radius={10}/>);
render(<Geolocation radius={10} />);
clickUpdateLocation();

const expectedLocationFilter: SelectableStaticFilter = createLocationFilter(10 * 1609.344);
Expand All @@ -162,7 +182,7 @@ describe('default click handler', () => {
});
});

it('replace existing location filters with a new location filter in static filters state', async () => {
it('replaces existing location filters with a new location filter in static filters state', async () => {
mockAnswersState(mockedStateWithFilters);
const actions = spyOnActions();
render(<Geolocation />);
Expand All @@ -189,7 +209,7 @@ describe('default click handler', () => {
it('sets a location filter using a larger radius than provided value due to low accuracy of user coordinate', async () => {
jest.spyOn(locationOperations, 'getUserLocation').mockResolvedValue(newGeoPositionWithLowAccuracy);
const actions = spyOnActions();
render(<Geolocation radius={10}/>);
render(<Geolocation radius={10} />);
clickUpdateLocation();

const accuracy = newGeoPositionWithLowAccuracy.coords.accuracy;
Expand Down Expand Up @@ -243,4 +263,4 @@ function createLocationFilter(radius: number = 50 * 1609.344): SelectableStaticF
},
}
};
}
}

0 comments on commit 893b7b1

Please sign in to comment.