-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Improved the UX of the "choose my location" feature
- Loading branch information
1 parent
e838253
commit 55e9d81
Showing
10 changed files
with
138 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { z } from "zod"; | ||
import { createTRPCRouter, rateLimitedProcedure } from "~/server/api/trpc"; | ||
import { env } from "~/env.mjs"; | ||
import axios from "axios"; | ||
import { log } from "next-axiom"; | ||
import { type ICity } from "~/types"; | ||
import crypto from "crypto"; | ||
|
||
const reverseGeoSchema = z.array( | ||
z.object({ | ||
name: z.string(), | ||
country: z.string(), | ||
state: z.string().optional(), | ||
}), | ||
); | ||
|
||
type ReverseGeo = z.infer<typeof reverseGeoSchema> | undefined; | ||
|
||
function generateId(object: ReverseGeo): number { | ||
const str = JSON.stringify(object); | ||
const hash = crypto.createHash("md5").update(str).digest("hex"); | ||
return parseInt(hash.slice(0, 12), 16); | ||
} | ||
|
||
export const reverseGeoRouter = createTRPCRouter({ | ||
getCity: rateLimitedProcedure | ||
.input( | ||
z.object({ | ||
coordinates: z.object({ | ||
lat: z.number(), | ||
lng: z.number(), | ||
}), | ||
}), | ||
) | ||
.mutation(async ({ input }): Promise<ICity | undefined> => { | ||
log.info("User requested location data for coordinates", { | ||
coordinates: input.coordinates, | ||
}); | ||
|
||
const urlReverseGeo = `https://api.api-ninjas.com/v1/reversegeocoding?lat=${input.coordinates.lat.toString()}&lon=${input.coordinates.lng.toString()}`; | ||
|
||
let reverseGeoData: ReverseGeo = undefined; | ||
|
||
try { | ||
const reverseGeoResult = await axios.get<ReverseGeo>(urlReverseGeo, { | ||
headers: { | ||
"X-Api-Key": `${env.API_NINJA_API_KEY}`, | ||
}, | ||
}); | ||
/* log.debug("Reverse geocoding data", { | ||
data: (reverseGeoResult).data, | ||
}); */ | ||
reverseGeoData = reverseGeoSchema.parse(reverseGeoResult.data); | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
log.error("Zod Errors", error.issues); | ||
} else { | ||
log.error("Error while fetching reverse geocoding data", { | ||
error: error, | ||
}); | ||
} | ||
} | ||
|
||
// Return if no data | ||
if (!reverseGeoData?.[0]) { | ||
return undefined; | ||
} | ||
|
||
// console.log(generateId(reverseGeoData)); | ||
|
||
return { | ||
id: generateId(reverseGeoData), | ||
name: reverseGeoData[0].name, | ||
coord: { | ||
lat: input.coordinates.lat, | ||
lon: input.coordinates.lng, | ||
}, | ||
country: reverseGeoData[0].country, | ||
region: reverseGeoData[0].state ?? "", | ||
}; | ||
}), | ||
}); |
55e9d81
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
weather-app – ./
weather-app-swart-eight.vercel.app
weather-app-git-master-creative-programming-group.vercel.app
weatherio1.vercel.app
weather-app-creative-programming-group.vercel.app
weather.roessner.tech