fix: add null checks and fallback values for API response data
This commit is contained in:
parent
a340ab6abc
commit
5adb5ddead
|
|
@ -36,8 +36,8 @@ export default function Home() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchData() {
|
async function fetchData() {
|
||||||
const response = await ApiService.getMigrants(1, 10)
|
const response = await ApiService.getMigrants(1, 10)
|
||||||
setMigrants(response.data)
|
setMigrants(response.data || [])
|
||||||
setTotal(response.total)
|
setTotal(response.total || 0)
|
||||||
}
|
}
|
||||||
fetchData()
|
fetchData()
|
||||||
}, [])
|
}, [])
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ export const useMigrants = () => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await ApiService.getMigrants(1, 1000);
|
const response = await ApiService.getMigrants(1, 1000);
|
||||||
const people: Person[] = response.data;
|
const people: Person[] = response.data ?? [];
|
||||||
|
|
||||||
const migrationCounts = getMigrationCounts(people);
|
const migrationCounts = getMigrationCounts(people);
|
||||||
const residenceCounts = getResidenceCounts(people);
|
const residenceCounts = getResidenceCounts(people);
|
||||||
|
|
|
||||||
|
|
@ -76,9 +76,9 @@ export const useMigrants = (perPage: number = 10): UseMigrantsReturn => {
|
||||||
console.log("Sending filters:", activeFilters);
|
console.log("Sending filters:", activeFilters);
|
||||||
|
|
||||||
const res = await ApiService.getMigrants(currentPage, perPage, activeFilters);
|
const res = await ApiService.getMigrants(currentPage, perPage, activeFilters);
|
||||||
setMigrants(res.data);
|
setMigrants(res.data || []);
|
||||||
setTotalPages(res.last_page);
|
setTotalPages(res.last_page || 1);
|
||||||
setCurrentPage(res.current_page);
|
setCurrentPage(res.current_page || 1);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to fetch migrants", err);
|
console.error("Failed to fetch migrants", err);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
||||||
|
|
@ -34,19 +34,25 @@ export function useMigrantsSearch(perPage = 10) {
|
||||||
const filters = getSearchParamsObject(searchParams);
|
const filters = getSearchParamsObject(searchParams);
|
||||||
const response = await apiService.getMigrants(page, perPage, filters);
|
const response = await apiService.getMigrants(page, perPage, filters);
|
||||||
|
|
||||||
const fetchedMigrants = response.data;
|
const fetchedMigrants = response.data || [];
|
||||||
setMigrants(fetchedMigrants);
|
setMigrants(fetchedMigrants);
|
||||||
setPagination({
|
setPagination({
|
||||||
currentPage: response.current_page,
|
currentPage: response.current_page ?? 1,
|
||||||
totalPages: response.last_page,
|
totalPages: response.last_page ?? 1,
|
||||||
totalItems: response.total,
|
totalItems: response.total ?? 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch photos for each migrant
|
// Fetch photos for each migrant
|
||||||
const photosMap: Record<number, Photo[]> = {};
|
const photosMap: Record<number, Photo[]> = {};
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
fetchedMigrants.map(async (migrant: Person) => {
|
(fetchedMigrants || []).map(async (migrant: Person) => {
|
||||||
try {
|
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);
|
const photos = await apiService.getPhotos(migrant.person_id);
|
||||||
migrant.photos = photos; // Store all photos
|
migrant.photos = photos; // Store all photos
|
||||||
migrant.profilePhoto = photos.find((photo) => photo.is_profile_photo) || null;
|
migrant.profilePhoto = photos.find((photo) => photo.is_profile_photo) || null;
|
||||||
|
|
@ -58,6 +64,7 @@ export function useMigrantsSearch(perPage = 10) {
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
setPhotosById(photosMap);
|
setPhotosById(photosMap);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue