Skip to content

Commit fd3e7a6

Browse files
committed
Fix location filtering
1 parent 4cf97a6 commit fd3e7a6

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

backend/api/src/search-location.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { APIHandler } from './helpers/endpoint'
2+
import {geodbHost} from "common/constants";
23

34
export const searchLocation: APIHandler<'search-location'> = async (body) => {
45
const { term, limit } = body
56
const apiKey = process.env.GEODB_API_KEY
6-
console.log('GEODB_API_KEY', apiKey)
77

88
if (!apiKey) {
99
return { status: 'failure', data: 'Missing GEODB API key' }
1010
}
11-
const host = 'wft-geo-db.p.rapidapi.com'
12-
const baseUrl = `https://${host}/v1/geo`
11+
const baseUrl = `https://${geodbHost}/v1/geo`
1312
const url = `${baseUrl}/cities?namePrefix=${term}&limit=${
1413
limit ?? 10
1514
}&offset=0&sort=-population`
@@ -19,15 +18,15 @@ export const searchLocation: APIHandler<'search-location'> = async (body) => {
1918
method: 'GET',
2019
headers: {
2120
'X-RapidAPI-Key': apiKey,
22-
'X-RapidAPI-Host': host,
21+
'X-RapidAPI-Host': geodbHost,
2322
},
2423
})
2524
if (!res.ok) {
2625
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
2726
}
2827

2928
const data = await res.json()
30-
// console.log('GEO DB', data)
29+
3130
return { status: 'success', data: data }
3231
} catch (error: any) {
3332
console.log('failure', error)

backend/api/src/search-near-city.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { APIHandler } from './helpers/endpoint'
2+
import { geodbHost } from 'common/constants'
23

34
export const searchNearCity: APIHandler<'search-near-city'> = async (body) => {
45
const { cityId, radius } = body
@@ -11,27 +12,29 @@ const searchNearCityMain = async (cityId: string, radius: number) => {
1112
if (!apiKey) {
1213
return { status: 'failure', data: 'Missing GEODB API key' }
1314
}
14-
const host = 'wft-geo-db.p.rapidapi.com'
15-
const baseUrl = `https://${host}/v1/geo`
16-
const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=100`
15+
const baseUrl = `https://${geodbHost}/v1/geo`
16+
// Limit to 10 cities for now for free plan, was 100 before (may need to buy plan)
17+
const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10`
1718

1819
try {
1920
const res = await fetch(url, {
2021
method: 'GET',
2122
headers: {
2223
'X-RapidAPI-Key': apiKey,
23-
'X-RapidAPI-Host': host,
24+
'X-RapidAPI-Host': geodbHost,
2425
},
2526
})
2627

2728
if (!res.ok) {
28-
throw new Error(`HTTP error! Status: ${res.status}`)
29+
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
2930
}
3031

3132
const data = await res.json()
33+
console.log('searchNearCityMain', data)
3234

3335
return { status: 'success', data: data }
3436
} catch (error) {
37+
console.log('failure', error)
3538
return { status: 'failure', data: error }
3639
}
3740
}

common/src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
export const geodbHost = 'wft-geo-db.p.rapidapi.com'

web/hooks/use-nearby-locations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function useNearbyCities(
1919
cityId: referenceCityId,
2020
radius,
2121
}).then((result) => {
22+
console.log('search-near-city', result)
2223
if (thisSearchCount == searchCount.current) {
2324
if (result.status === 'failure') {
2425
setNearbyCities(null)

0 commit comments

Comments
 (0)