Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update MapContext and Map components for revised API response #76

Merged
merged 1 commit into from
Feb 11, 2025
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
8 changes: 4 additions & 4 deletions src/components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ export default function Map() {
acc: Record<string, { lat: number; lon: number; country: string; count: number }>,
node: LocationNode
) => {
const { city, lat, lon, country } = node._source
const { city, lat, lon, country, count } = node

if (city) {
if (!acc[city]) {
acc[city] = { lat, lon, country, count: 0 }
acc[city] = { lat, lon, country, count }
} else {
acc[city].count += count
}

acc[city].count += 1
}

return acc
Expand Down
25 changes: 12 additions & 13 deletions src/context/MapContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ import React, {
useMemo
} from 'react'
import axios from 'axios'
import { LocationNode } from '../shared/types/locationNodeType'
import { getApiRoute } from '@/config'

interface CountryNodesInfo {
country: string
nodeIds: string[]
totalNodes: number
}
import { LocationNode } from '../shared/types/locationNodeType'

interface MapContextType {
data: LocationNode[]
countryNodesInfo: CountryNodesInfo[]
loading: boolean
error: any
totalCountries: number
Expand All @@ -32,7 +25,6 @@ export const MapContext = createContext<MapContextType | undefined>(undefined)

export const MapProvider: React.FC<MapProviderProps> = ({ children }) => {
const [data, setData] = useState<LocationNode[]>([])
const [countryNodesInfo, setCountryNodesInfo] = useState<CountryNodesInfo[]>([])
const [loading, setLoading] = useState<boolean>(true)
const [error, setError] = useState<any>(null)
const [totalCountries, setTotalCountries] = useState<number>(0)
Expand All @@ -46,13 +38,21 @@ export const MapProvider: React.FC<MapProviderProps> = ({ children }) => {
setLoading(true)
try {
const response = await axios.get(fetchUrl)
const nodes = response.data.locations
const locationsFromAPI = response.data.locations
const totalCountries = response.data.totalCountries

setData(nodes)
const transformedNodes = locationsFromAPI.map((location: any) => ({
city: location.location,
lat: location.coordinates.lat,
lon: location.coordinates.lon,
country: location.country || location.location,
count: location.count
}))

setData(transformedNodes)
setTotalCountries(totalCountries)
} catch (err) {
console.log('error', err)
console.error('Error fetching locations:', err)
setError(err)
} finally {
setLoading(false)
Expand All @@ -66,7 +66,6 @@ export const MapProvider: React.FC<MapProviderProps> = ({ children }) => {
<MapContext.Provider
value={{
data,
countryNodesInfo,
loading,
error,
totalCountries
Expand Down
16 changes: 5 additions & 11 deletions src/shared/types/locationNodeType.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
export interface LocationNode {
_index: string
_id: string
_score: number
_source: {
id: string
ip: string
country: string
city: string
lat: number
lon: number
}
city: string
count: number
country: string
lat: number
lon: number
}
Loading