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

feat: add switching logic when map type get changed #127

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 26 additions & 4 deletions src/components/Map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function Map({ countries, disputedAreas, fcsData, alertData }: Ma
const [regionNutritionData, setRegionNutritionData] = useState<FeatureCollection | undefined>();
const [ipcRegionData, setIpcRegionData] = useState<FeatureCollection<Geometry, GeoJsonProperties> | undefined>();
const [selectedCountryName, setSelectedCountryName] = useState<string | undefined>(undefined);
const [data, setData] = useState<boolean>(true);
ArmanpreetGhotra marked this conversation as resolved.
Show resolved Hide resolved

const onZoomThresholdReached = () => {
setSelectedCountryId(null);
Expand Down Expand Up @@ -82,13 +83,34 @@ export default function Map({ countries, disputedAreas, fcsData, alertData }: Ma
setCountryData,
setCountryIso3Data,
setRegionNutritionData,
setIpcRegionData
setIpcRegionData,
setData
);
setSelectedCountryName(selectedCountryData.properties.adm0_name);
mapRef.current?.fitBounds(L.geoJSON(selectedCountryData as GeoJSON).getBounds(), { animate: true });
if (data) {
setSelectedCountryName(selectedCountryData.properties.adm0_name);
mapRef.current?.fitBounds(L.geoJSON(selectedCountryData as GeoJSON).getBounds(), { animate: true });
} else {
mapRef.current?.fitBounds(
[
[-90, -180],
[90, 180],
],
{ animate: true }
);
setSelectedCountryId(null);
ArmanpreetGhotra marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
mapRef.current?.fitBounds(
[
[-90, -180], // South-West corner
[90, 180], // North-East corner
],
{ animate: true }
);
setSelectedCountryId(null);
}
}
}, [selectedCountryId, selectedMapType]);
}, [selectedCountryId, selectedMapType, data]);

return (
<MapContainer
Expand Down
9 changes: 8 additions & 1 deletion src/infrastructure/repositories/CountryRepositoryImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ export default class CountryRepositoryImpl implements CountryRepository {
}

async getRegionIpcData(countryId: number): Promise<RegionIpc> {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/adm0/${countryId}/ipc.geojson`);
const url = `${process.env.NEXT_PUBLIC_API_URL}/adm0/${countryId}/ipc.geojson`;

const response = await fetch(url);

if (!response.ok) {
throw new Error(`Failed to fetch IPC data: HTTP ${response.status} - ${response.statusText}`);
}

return response.json();
}
}
40 changes: 31 additions & 9 deletions src/operations/map/MapOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export class MapOperations {
setCountryData: (countryData: CountryData | undefined) => void,
setCountryIso3Data: (iso3Data: CountryIso3Data | undefined) => void,
setRegionNutritionData: (regionNutritionData: FeatureCollection | undefined) => void,
setIpcRegionData: (ipcRegionData: FeatureCollection<Geometry, GeoJsonProperties> | undefined) => void
setIpcRegionData: (ipcRegionData: FeatureCollection<Geometry, GeoJsonProperties> | undefined) => void,
setData: (data: boolean) => void
) {
setCountryClickLoading(true);

Expand All @@ -29,17 +30,25 @@ export class MapOperations {
type: 'FeatureCollection',
features: newRegionData.features as Feature<Geometry, GeoJsonProperties>[],
});
const hasFcs = newRegionData.features.some((feature) => feature.properties?.fcs !== undefined);
setData(hasFcs);
}
}

if (selectedMapType === GlobalInsight.IPC) {
setIpcRegionData(undefined);
const newIpcRegionData = await countryRepository.getRegionIpcData(selectedCountryData.properties.adm0_id);
if (newIpcRegionData && newIpcRegionData.features) {
setIpcRegionData({
type: 'FeatureCollection',
features: newIpcRegionData?.features as Feature<Geometry, GeoJsonProperties>[],
});
try {
const newIpcRegionData = await countryRepository.getRegionIpcData(selectedCountryData.properties.adm0_id);

if (newIpcRegionData && newIpcRegionData.features) {
setIpcRegionData({
type: 'FeatureCollection',
features: newIpcRegionData?.features as Feature<Geometry, GeoJsonProperties>[],
});
const hasIpc = newIpcRegionData.features.some((feature) => feature.properties?.ipcPhase !== undefined);
setData(hasIpc);
}
} catch {
setData(false);
}
}

Expand All @@ -50,7 +59,13 @@ export class MapOperations {

if (selectedMapType === GlobalInsight.FOOD) {
const newCountryIso3Data = await countryRepository.getCountryIso3Data(selectedCountryData.properties.iso3);
setCountryIso3Data(newCountryIso3Data);

if (Array.isArray(newCountryIso3Data) && newCountryIso3Data[1] === 404) {
setData(false);
} else {
setCountryIso3Data(newCountryIso3Data);
setData(true);
}
}

if (selectedMapType === GlobalInsight.NUTRITION) {
Expand All @@ -62,6 +77,13 @@ export class MapOperations {
type: 'FeatureCollection',
features: newRegionNutritionData.features as Feature<Geometry, GeoJsonProperties>[],
});
const hasNutrition = newRegionNutritionData.features.some(
(feature) =>
feature.properties?.nutrition &&
typeof feature.properties.nutrition === 'object' &&
Object.keys(feature.properties.nutrition).length > 0
);
setData(hasNutrition);
}
}

Expand Down
Loading