-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement network calls to the backend (#9)
* feat: server-side requests * feat: add client side hooks * docs: readme update
- Loading branch information
Showing
21 changed files
with
231 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import MapLoader from '@/components/Map/MapLoader'; | ||
import container from '@/container'; | ||
import { GlobalDataRepository } from '@/domain/repositories/GlobalDataRepository'; | ||
|
||
export default function Home() { | ||
return <MapLoader />; | ||
export default async function Home() { | ||
const globalRepo = container.resolve<GlobalDataRepository>('GlobalDataRepository'); | ||
const countryMapDataPromise = globalRepo.getMapDataForCountries(); | ||
const disputedAreasPromise = globalRepo.getDisputedAreas(); | ||
const [countryMapData, disputedAreas] = await Promise.all([countryMapDataPromise, disputedAreasPromise]); | ||
return <MapLoader countries={countryMapData} disputedAreas={disputedAreas} />; | ||
} |
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,21 @@ | ||
import { useState } from 'react'; | ||
import { Polygon } from 'react-leaflet'; | ||
|
||
import { CountryMapData } from '@/domain/entities/country/CountryMapData'; | ||
|
||
import { CountryPopup } from './CountryPopup'; | ||
|
||
export function CountryPolygon({ country }: { country: CountryMapData }) { | ||
const [active, setActive] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Polygon | ||
pathOptions={{ color: 'purple' }} | ||
positions={country.geometry.coordinates} | ||
eventHandlers={{ click: () => setActive(true) }} | ||
/> | ||
{active && <CountryPopup country={country} onClose={() => setActive(false)} />} | ||
</> | ||
); | ||
} |
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,20 @@ | ||
import { Popup } from 'react-leaflet'; | ||
|
||
import { CountryMapData } from '@/domain/entities/country/CountryMapData'; | ||
import { useCountryDataQuery } from '@/domain/hooks/countryHooks'; | ||
|
||
/** | ||
* This is just an example of how the sidebars could fetch the data when a country is clicked | ||
*/ | ||
export function CountryPopup({ country, onClose }: { country: CountryMapData; onClose: () => void }) { | ||
const { data, isPending } = useCountryDataQuery(country.properties.adm0_id); | ||
|
||
return ( | ||
<Popup | ||
position={{ lat: country.properties.centroid.latitude, lng: country.properties.centroid.longitude }} | ||
eventHandlers={{ remove: onClose }} | ||
> | ||
FCS: {isPending || !data ? 'Loading...' : data.fcs} | ||
</Popup> | ||
); | ||
} |
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,15 @@ | ||
import { QueryClient } from '@tanstack/react-query'; | ||
|
||
/** | ||
* Only refetches data if it's more than 1 hour old | ||
*/ | ||
export const cachedQueryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
refetchOnWindowFocus: false, | ||
gcTime: 1000 * 60 * 60, | ||
}, | ||
}, | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import { LatLngExpression } from 'leaflet'; | ||
|
||
export interface Geometry { | ||
type: string; | ||
coordinates: number[][][]; // Maybe a common type is not best idea here, the coordinate arrays seem to have different dephts. | ||
coordinates: LatLngExpression[][][]; // Maybe a common type is not best idea here, the coordinate arrays seem to have different dephts. | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface ResponseWrapper<T> { | ||
statusCode: string; | ||
body: T[]; | ||
body: T; | ||
} |
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,28 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { cachedQueryClient } from '@/config/queryClient'; | ||
import container from '@/container'; | ||
|
||
import { Conflict } from '../entities/alerts/Conflict'; | ||
import { Hazard } from '../entities/alerts/Hazard'; | ||
import { AlertRepository } from '../repositories/AlertRepository'; | ||
|
||
const alertsRepo = container.resolve<AlertRepository>('AlertRepository'); | ||
|
||
export const useConflictQuery = () => | ||
useQuery<Conflict[]>( | ||
{ | ||
queryKey: ['fetchConflicts'], | ||
queryFn: alertsRepo.getConflictData, | ||
}, | ||
cachedQueryClient | ||
); | ||
|
||
export const useHazardQuery = () => | ||
useQuery<Hazard[]>( | ||
{ | ||
queryKey: ['fetchHazards'], | ||
queryFn: alertsRepo.getHazardData, | ||
}, | ||
cachedQueryClient | ||
); |
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,48 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { cachedQueryClient } from '@/config/queryClient'; | ||
import container from '@/container'; | ||
|
||
import { AdditionalCountryData } from '../entities/country/AdditionalCountryData'; | ||
import { CountryData } from '../entities/country/CountryData'; | ||
import { CountryIso3Data } from '../entities/country/CountryIso3Data'; | ||
import { RegionIpc } from '../entities/region/RegionIpc'; | ||
import CountryRepository from '../repositories/CountryRepository'; | ||
|
||
const countryRepo = container.resolve<CountryRepository>('CountryRepository'); | ||
|
||
export const useCountryDataQuery = (countryId: number) => | ||
useQuery<CountryData, Error>( | ||
{ | ||
queryKey: ['fetchCountryData', countryId], | ||
queryFn: async () => countryRepo.getCountryData(countryId), | ||
}, | ||
cachedQueryClient | ||
); | ||
|
||
export const useRegionDataQuery = (countryId: number) => | ||
useQuery<AdditionalCountryData, Error>( | ||
{ | ||
queryKey: ['fetchRegionData', countryId], | ||
queryFn: async () => countryRepo.getRegionData(countryId), | ||
}, | ||
cachedQueryClient | ||
); | ||
|
||
export const useCountryIso3DataQuery = (countryCode: string) => | ||
useQuery<CountryIso3Data, Error>( | ||
{ | ||
queryKey: ['fetchCountryIso3Data', countryCode], | ||
queryFn: async () => countryRepo.getCountryIso3Data(countryCode), | ||
}, | ||
cachedQueryClient | ||
); | ||
|
||
export const useRegionIpcDataQuery = (countryId: number) => | ||
useQuery<RegionIpc, Error>( | ||
{ | ||
queryKey: ['fetchRegionIpcData', countryId], | ||
queryFn: async () => countryRepo.getRegionIpcData(countryId), | ||
}, | ||
cachedQueryClient | ||
); |
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,18 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { cachedQueryClient } from '@/config/queryClient'; | ||
import container from '@/container'; | ||
|
||
import { CountryIpcData } from '../entities/country/CountryIpcData'; | ||
import { GlobalDataRepository } from '../repositories/GlobalDataRepository'; | ||
|
||
const globalRepo = container.resolve<GlobalDataRepository>('GlobalDataRepository'); | ||
|
||
export const useIpcQuery = () => | ||
useQuery<CountryIpcData[]>( | ||
{ | ||
queryKey: ['fetchIpcData'], | ||
queryFn: globalRepo.getIpcData, | ||
}, | ||
cachedQueryClient | ||
); |
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,7 @@ | ||
import { CountryMapDataWrapper } from '../entities/country/CountryMapData'; | ||
import { DisputedAreas } from '../entities/DisputedAreas'; | ||
|
||
export interface MapProps { | ||
countries: CountryMapDataWrapper; | ||
disputedAreas: DisputedAreas; | ||
} |
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
Oops, something went wrong.