From 55e9d812202aee0308875e6e34484bdd0b0005eb Mon Sep 17 00:00:00 2001 From: FleetAdmiralJakob Date: Wed, 3 Jan 2024 17:01:35 +0100 Subject: [PATCH] refactor: Improved the UX of the "choose my location" feature --- .env.example | 1 + public/locales/de/locationsettings.json | 2 +- public/locales/de/search.json | 2 +- public/locales/en/locationsettings.json | 2 +- public/locales/en/search.json | 2 +- src/env.mjs | 8 ++- src/pages/locationsettings/index.tsx | 38 +++++++----- src/pages/search/index.tsx | 42 +++++++------ src/server/api/root.ts | 2 + src/server/api/routers/reverseGeo.ts | 82 +++++++++++++++++++++++++ 10 files changed, 138 insertions(+), 43 deletions(-) create mode 100644 src/server/api/routers/reverseGeo.ts diff --git a/.env.example b/.env.example index 4bb44f90..e465a83d 100644 --- a/.env.example +++ b/.env.example @@ -16,3 +16,4 @@ UPSTASH_RATELIMITER_TOKENS_PER_TIME="..." UPSTASH_RATELIMITER_TIME_INTERVAL="..." RESEND_API_KEY="re_..." QWEATHER_API_KEY="..." +API_NINJA_API_KEY="..." diff --git a/public/locales/de/locationsettings.json b/public/locales/de/locationsettings.json index 61618032..2c659259 100644 --- a/public/locales/de/locationsettings.json +++ b/public/locales/de/locationsettings.json @@ -9,5 +9,5 @@ "city not found toast": "Stadt nicht gefunden!", "try again toast": "Versuche es erneut!", - "my city button": "Meine Stadt auswählen" + "my location button": "Meinen Standort auswählen" } diff --git a/public/locales/de/search.json b/public/locales/de/search.json index 079569c2..778b72ea 100644 --- a/public/locales/de/search.json +++ b/public/locales/de/search.json @@ -2,5 +2,5 @@ "search input placeholder": "Suche deinen Standort", "continue button": "Weiter -->", - "my city button": "Meine Stadt auswählen" + "my location button": "Meinen Standort auswählen" } diff --git a/public/locales/en/locationsettings.json b/public/locales/en/locationsettings.json index cc9a6f6b..b65f84b8 100644 --- a/public/locales/en/locationsettings.json +++ b/public/locales/en/locationsettings.json @@ -9,5 +9,5 @@ "city not found toast": "City not found!", "try again toast": "Try again!", - "my city button": "Choose my city" + "my location button": "Choose my location" } diff --git a/public/locales/en/search.json b/public/locales/en/search.json index 14013084..9ee408cc 100644 --- a/public/locales/en/search.json +++ b/public/locales/en/search.json @@ -2,5 +2,5 @@ "search input placeholder": "Search for your location", "continue button": "Continue -->", - "my city button": "Choose my city" + "my location button": "Choose my location" } diff --git a/src/env.mjs b/src/env.mjs index bd0ddb72..dab38058 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -1,4 +1,4 @@ -import { z } from 'zod' +import {z} from 'zod' /** * Specify your server-side environment variables schema here. This way you can ensure the app isn't @@ -12,7 +12,8 @@ const server = z.object({ UPSTASH_RATELIMITER_TOKENS_PER_TIME: z.string().min(1), UPSTASH_RATELIMITER_TIME_INTERVAL: z.string().min(1), RESEND_API_KEY: z.string().min(1), - QWEATHER_API_KEY: z.string().min(1) + QWEATHER_API_KEY: z.string().min(1), + API_NINJA_API_KEY: z.string().min(1), }) /** @@ -39,7 +40,8 @@ const processEnv = { UPSTASH_RATELIMITER_TIME_INTERVAL: process.env.UPSTASH_RATELIMITER_TIME_INTERVAL, RESEND_API_KEY: process.env.RESEND_API_KEY, - QWEATHER_API_KEY: process.env.QWEATHER_API_KEY + QWEATHER_API_KEY: process.env.QWEATHER_API_KEY, + API_NINJA_API_KEY: process.env.API_NINJA_API_KEY, // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR, } diff --git a/src/pages/locationsettings/index.tsx b/src/pages/locationsettings/index.tsx index 3352cb23..ce87ba83 100644 --- a/src/pages/locationsettings/index.tsx +++ b/src/pages/locationsettings/index.tsx @@ -46,6 +46,17 @@ const LocationSettings = observer(() => { name: searchValue.name, }); + const findCityByCoordinatesMutation = + api.reverseGeoRouter.getCity.useMutation({ + onSuccess: (data) => { + if (data) { + setSearchValue(data); + } else { + toast.error(translationLocationSettings("city not found toast")); + } + }, + }); + useEffect(() => { if (searchValue.name === "") { setResults([]); @@ -77,12 +88,15 @@ const LocationSettings = observer(() => { lat: 0, }, }; - if (searchValue.id === -1) { + if ( + searchValue.id.toString().length === 15 || + searchValue.id.toString().length === 14 + ) { city = { - id: -1, + id: searchValue.id, name: searchValue.name, - country: "", - region: "", + country: searchValue.country, + region: searchValue.region, coord: { lon: searchValue.coord.lon, lat: searchValue.coord.lat, @@ -309,23 +323,15 @@ const LocationSettings = observer(() => { navigator.geolocation.getCurrentPosition((position) => { const latitude = position.coords.latitude; const longitude = position.coords.longitude; - const city: ICity = { - id: -1, - name: translationCommon("my city"), - country: "", - region: "", - coord: { - lon: longitude, - lat: latitude, - }, - }; - setSearchValue(city); + findCityByCoordinatesMutation.mutate({ + coordinates: { lat: latitude, lng: longitude }, + }); }); } }} > - {translationLocationSettings("my city button")} + {translationLocationSettings("my location button")} {searchValue.name.length > 0 ? (