From 5adb5ddead0ec5d40b0b099988825364f9bd5c50 Mon Sep 17 00:00:00 2001 From: mark Date: Wed, 28 May 2025 08:02:22 +0800 Subject: [PATCH] fix: add null checks and fallback values for API response data --- src/components/home/HeroSection.tsx | 4 ++-- src/hooks/useGraph.ts | 2 +- src/hooks/useMigrants.ts | 6 +++--- src/hooks/useMigrantsSearch.ts | 19 +++++++++++++------ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/components/home/HeroSection.tsx b/src/components/home/HeroSection.tsx index d1b2b43..5f04ad7 100644 --- a/src/components/home/HeroSection.tsx +++ b/src/components/home/HeroSection.tsx @@ -36,8 +36,8 @@ export default function Home() { useEffect(() => { async function fetchData() { const response = await ApiService.getMigrants(1, 10) - setMigrants(response.data) - setTotal(response.total) + setMigrants(response.data || []) + setTotal(response.total || 0) } fetchData() }, []) diff --git a/src/hooks/useGraph.ts b/src/hooks/useGraph.ts index d58d926..7c52619 100644 --- a/src/hooks/useGraph.ts +++ b/src/hooks/useGraph.ts @@ -14,7 +14,7 @@ export const useMigrants = () => { const fetchData = async () => { try { const response = await ApiService.getMigrants(1, 1000); - const people: Person[] = response.data; + const people: Person[] = response.data ?? []; const migrationCounts = getMigrationCounts(people); const residenceCounts = getResidenceCounts(people); diff --git a/src/hooks/useMigrants.ts b/src/hooks/useMigrants.ts index 2f39392..7dd195a 100644 --- a/src/hooks/useMigrants.ts +++ b/src/hooks/useMigrants.ts @@ -76,9 +76,9 @@ export const useMigrants = (perPage: number = 10): UseMigrantsReturn => { console.log("Sending filters:", activeFilters); const res = await ApiService.getMigrants(currentPage, perPage, activeFilters); - setMigrants(res.data); - setTotalPages(res.last_page); - setCurrentPage(res.current_page); + setMigrants(res.data || []); + setTotalPages(res.last_page || 1); + setCurrentPage(res.current_page || 1); } catch (err) { console.error("Failed to fetch migrants", err); } finally { diff --git a/src/hooks/useMigrantsSearch.ts b/src/hooks/useMigrantsSearch.ts index eafad92..961c517 100644 --- a/src/hooks/useMigrantsSearch.ts +++ b/src/hooks/useMigrantsSearch.ts @@ -34,19 +34,25 @@ export function useMigrantsSearch(perPage = 10) { const filters = getSearchParamsObject(searchParams); const response = await apiService.getMigrants(page, perPage, filters); - const fetchedMigrants = response.data; + const fetchedMigrants = response.data || []; setMigrants(fetchedMigrants); setPagination({ - currentPage: response.current_page, - totalPages: response.last_page, - totalItems: response.total, + currentPage: response.current_page ?? 1, + totalPages: response.last_page ?? 1, + totalItems: response.total ?? 0, }); - // Fetch photos for each migrant const photosMap: Record = {}; await Promise.all( - fetchedMigrants.map(async (migrant: Person) => { + (fetchedMigrants || []).map(async (migrant: Person) => { try { + if (migrant.person_id === undefined) { + console.warn('Missing person_id for migrant', migrant); + migrant.photos = []; + migrant.profilePhoto = null; + return; + } + const photos = await apiService.getPhotos(migrant.person_id); migrant.photos = photos; // Store all photos migrant.profilePhoto = photos.find((photo) => photo.is_profile_photo) || null; @@ -58,6 +64,7 @@ export function useMigrantsSearch(perPage = 10) { }) ); + setPhotosById(photosMap);