mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-02-24 02:46:11 -05:00
Fix location filtering
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { APIHandler } from './helpers/endpoint'
|
||||
import {geodbHost} from "common/constants";
|
||||
|
||||
export const searchLocation: APIHandler<'search-location'> = async (body) => {
|
||||
const { term, limit } = body
|
||||
const apiKey = process.env.GEODB_API_KEY
|
||||
console.log('GEODB_API_KEY', apiKey)
|
||||
|
||||
if (!apiKey) {
|
||||
return { status: 'failure', data: 'Missing GEODB API key' }
|
||||
}
|
||||
const host = 'wft-geo-db.p.rapidapi.com'
|
||||
const baseUrl = `https://${host}/v1/geo`
|
||||
const baseUrl = `https://${geodbHost}/v1/geo`
|
||||
const url = `${baseUrl}/cities?namePrefix=${term}&limit=${
|
||||
limit ?? 10
|
||||
}&offset=0&sort=-population`
|
||||
@@ -19,7 +18,7 @@ export const searchLocation: APIHandler<'search-location'> = async (body) => {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-RapidAPI-Key': apiKey,
|
||||
'X-RapidAPI-Host': host,
|
||||
'X-RapidAPI-Host': geodbHost,
|
||||
},
|
||||
})
|
||||
if (!res.ok) {
|
||||
@@ -27,7 +26,7 @@ export const searchLocation: APIHandler<'search-location'> = async (body) => {
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
// console.log('GEO DB', data)
|
||||
|
||||
return { status: 'success', data: data }
|
||||
} catch (error: any) {
|
||||
console.log('failure', error)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { APIHandler } from './helpers/endpoint'
|
||||
import { geodbHost } from 'common/constants'
|
||||
|
||||
export const searchNearCity: APIHandler<'search-near-city'> = async (body) => {
|
||||
const { cityId, radius } = body
|
||||
@@ -11,27 +12,29 @@ const searchNearCityMain = async (cityId: string, radius: number) => {
|
||||
if (!apiKey) {
|
||||
return { status: 'failure', data: 'Missing GEODB API key' }
|
||||
}
|
||||
const host = 'wft-geo-db.p.rapidapi.com'
|
||||
const baseUrl = `https://${host}/v1/geo`
|
||||
const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=100`
|
||||
const baseUrl = `https://${geodbHost}/v1/geo`
|
||||
// Limit to 10 cities for now for free plan, was 100 before (may need to buy plan)
|
||||
const url = `${baseUrl}/cities/${cityId}/nearbyCities?radius=${radius}&offset=0&sort=-population&limit=10`
|
||||
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-RapidAPI-Key': apiKey,
|
||||
'X-RapidAPI-Host': host,
|
||||
'X-RapidAPI-Host': geodbHost,
|
||||
},
|
||||
})
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP error! Status: ${res.status}`)
|
||||
throw new Error(`HTTP error! Status: ${res.status} ${await res.text()}`)
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
console.log('searchNearCityMain', data)
|
||||
|
||||
return { status: 'success', data: data }
|
||||
} catch (error) {
|
||||
console.log('failure', error)
|
||||
return { status: 'failure', data: error }
|
||||
}
|
||||
}
|
||||
|
||||
2
common/src/constants.ts
Normal file
2
common/src/constants.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
|
||||
export const geodbHost = 'wft-geo-db.p.rapidapi.com'
|
||||
@@ -19,6 +19,7 @@ export function useNearbyCities(
|
||||
cityId: referenceCityId,
|
||||
radius,
|
||||
}).then((result) => {
|
||||
console.log('search-near-city', result)
|
||||
if (thisSearchCount == searchCount.current) {
|
||||
if (result.status === 'failure') {
|
||||
setNearbyCities(null)
|
||||
|
||||
Reference in New Issue
Block a user