import React, { ReactElement, useState } from "react"; import { AnyGeocoderQuery } from "./geocoders/types"; import getGeocoder from "./index"; const GeocoderTester = ({ at = { lat: 0, lng: 0 }, endpoint, geocodeEarthApiKey, hereApiKey, onResults }: { at?: { lat: number; lng: number }; endpoint: string; geocodeEarthApiKey: string; hereApiKey: string; onResults: (any) => void; }): ReactElement => { const [searchTerm, setSearchTerm] = useState(""); const [focusPoint, setFocusPoint] = useState({ lat: at.lat, lng: at.lng }); const [boundary, setBoundary] = useState({ maxLat: 48.687912, minLat: 46.981852, maxLon: -121.467028, minLon: -123.148723 }); const [enableBoundary, setEnableBoundary] = useState(false); const [enableFocusPoint, setEnableFocusPoint] = useState(true); const [enableGeocodeEarth, setEnableGeocodeEarth] = useState(true); const [enableHere, setEnableHere] = useState(true); const [enablePhoton, setEnablePhoton] = useState(true); const [enableOtp, setEnableOtp] = useState(false); const [otpHost, setOtpHost] = useState(""); const [ reverseUseFeatureCollection, setReverseUseFeatureCollection ] = useState(false); const hereGeocoder = getGeocoder({ apiKey: hereApiKey, focusPoint, reverseUseFeatureCollection, type: "HERE" }); const peliasGeocoder = getGeocoder({ apiKey: geocodeEarthApiKey, baseUrl: "https://api.geocode.earth/v1", focusPoint, reverseUseFeatureCollection, size: 1, type: "PELIAS" }); const photonGeocoder = getGeocoder({ baseUrl: "https://photon.komoot.io", focusPoint, reverseUseFeatureCollection, size: 1, type: "PHOTON" }); const otpGeocoder = getGeocoder({ baseUrl: otpHost, size: 1, type: "OTP" }); const searchObj: AnyGeocoderQuery = { text: searchTerm }; if (enableBoundary) { searchObj.boundary = { rect: boundary }; } if (enableFocusPoint) { searchObj.point = focusPoint; searchObj.focusPoint = focusPoint; } const search = async () => { const hereRes = enableHere ? await hereGeocoder[endpoint](searchObj) : null; const peliasRes = enableGeocodeEarth ? await peliasGeocoder[endpoint](searchObj) : null; const photonRes = enablePhoton ? await photonGeocoder[endpoint](searchObj) : null; const otpRes = enableOtp ? await otpGeocoder[endpoint](searchObj) : null; onResults({ hereRes, peliasRes, photonRes, otpRes }); }; return (
{endpoint === "autocomplete" && (
setOtpHost(e.target.value)} >
)} {/* Boundary Input */} {endpoint !== "reverse" && (
)} {/* Focus Point Input */}
{endpoint !== "reverse" && (
)}
{endpoint !== "reverse" && ( <> )}
{endpoint === "reverse" && ( )}
); }; export default { argTypes: { onResults: { action: "result" } }, component: GeocoderTester, title: "Geocoder" }; export const TestSearch = { args: { at: { lat: 47.67552, lng: -122.31831 }, endpoint: "search", geocodeEarthApiKey: "fake_api_key_geocodeearth", hereApiKey: "fake_api_key_here" } }; export const TestAutocomplete = { args: { at: { lat: 47.67552, lng: -122.31831 }, endpoint: "autocomplete", geocodeEarthApiKey: "fake_api_key_geocodeearth", hereApiKey: "fake_api_key_here" } }; export const TestReverse = { args: { at: { lat: 47.66427, lng: -122.31388 }, endpoint: "reverse", geocodeEarthApiKey: "fake_api_key_geocodeearth", hereApiKey: "fake_api_key_here" } };