Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@tanstack/react-query": "^5.102.8",
"country-codes-list": "^3.2.0",
"react": "^19.2.8",
"react-dom": "^19.2.8"
},
Expand Down
5 changes: 3 additions & 2 deletions src/components/ViewCity.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import useGetWeather from "../hooks/fetchWeather/useGetWeather"
import useGetCityCoordinates from "../hooks/useGetCityCoordinates"
import useGetCityCoordinates from "../hooks/fetchCities/useGetCityCoordinates"

interface ViewCityProp {
city: string
}

export default function ViewCity(arg: ViewCityProp) {
const data = useGetCityCoordinates(arg.city)
const cityFilter = { cityName: arg.city }
const data = useGetCityCoordinates(cityFilter)

const { isLoading, error, result } = useGetWeather(
data.result[0]?.latitude,
Expand Down
47 changes: 47 additions & 0 deletions src/hooks/fetchCities/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export type citySearchResult = {
results: {
name: string
latitude: number
longitude: number
elevation: number
timezone: string
}[]
}

export const countryCodes = [
"AU",
"AT",
"AZ",
"BE",
"BR",
"CA",
"CN",
"DK",
"FI",
"FR",
"DE",
"GR",
"HU",
"IE",
"IT",
"JP",
"MX",
"NL",
"NO",
"PL",
"PT",
"RU",
"ES",
"SE",
"CH",
"TR",
"UK",
"US",
] as const

export type CountryCode = (typeof countryCodes)[number]

export type citySearchFilter = {
cityName: string
countryCode?: CountryCode
}
20 changes: 20 additions & 0 deletions src/hooks/fetchCities/useGetCityCoordinates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useQuery } from "@tanstack/react-query"
import type { citySearchResult, citySearchFilter } from "./types"

const useGetCityCoordinates = (filter: citySearchFilter) => {
const queryString = `?name=${filter.cityName}${filter.countryCode ? `&countryCode=${filter.countryCode}` : ""}`

const { isLoading, error, data } = useQuery({
queryKey: ["citySearch", filter.cityName],
queryFn: () =>
fetch(
`https://geocoding-api.open-meteo.com/v1/search${queryString}`
).then((res) => res.json() as Promise<citySearchResult>),
})

const result = data?.results ?? []

return { isLoading, error, result }
}

export default useGetCityCoordinates
27 changes: 0 additions & 27 deletions src/hooks/useGetCityCoordinates.ts

This file was deleted.

Loading