From ae7002052f7f4185705afe5a887d492bd0ee0836 Mon Sep 17 00:00:00 2001 From: frakarr Date: Thu, 11 Dec 2025 23:11:53 +0100 Subject: [PATCH] Update FamilyList.tsx --- pages/FamilyList.tsx | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/pages/FamilyList.tsx b/pages/FamilyList.tsx index 7d22fbc..58d17a7 100644 --- a/pages/FamilyList.tsx +++ b/pages/FamilyList.tsx @@ -111,16 +111,19 @@ export const FamilyList: React.FC = () => { // --- NOTICE LOGIC --- if (currentUser && appSettings.features.notices) { - // Filter: Must be same condo AND Active - // Visibility: Admin sees all. User sees Public OR Targeted. + // Filter Visibility: Admin sees all. User sees Public OR Targeted. + // Note: We removed the condoId check here because the API returns snake_case (condo_id) + // but we fetch by ID anyway, so we trust the API context. const relevantNotices = allNotices.filter(n => { - if (n.condoId !== condo.id || !n.active) return false; + if (!n.active) return false; // Filter inactive notices if (isPrivileged) return true; // Check visibility for regular users - const isPublic = !n.targetFamilyIds || n.targetFamilyIds.length === 0; - const isTargeted = currentUser.familyId && n.targetFamilyIds?.includes(currentUser.familyId); + // Handle case where targetFamilyIds might be null/undefined from backend + const targets = n.targetFamilyIds || []; + const isPublic = targets.length === 0; + const isTargeted = currentUser.familyId && targets.includes(currentUser.familyId); return isPublic || isTargeted; }); @@ -130,14 +133,23 @@ export const FamilyList: React.FC = () => { setNotices(relevantNotices); // Check read status for current user - const readStatuses = await Promise.all(relevantNotices.map(n => CondoService.getNoticeReadStatus(n.id))); - const readIds: string[] = []; - readStatuses.forEach((reads, idx) => { - if (reads.find(r => r.userId === currentUser.id)) { - readIds.push(relevantNotices[idx].id); + // We use getUnreadNoticesForUser to find what is NOT read, then derive read IDs. + // This avoids using the Admin-only 'getNoticeReadStatus' endpoint. + try { + if (currentUser.id && condo.id) { + const unreadList = await CondoService.getUnreadNoticesForUser(currentUser.id, condo.id); + const unreadIds = new Set(unreadList.map(n => n.id)); + + // If it's in relevantNotices but NOT in unreadList, it's read. + const readIds = relevantNotices + .filter(n => !unreadIds.has(n.id)) + .map(n => n.id); + + setUserReadIds(readIds); } - }); - setUserReadIds(readIds); + } catch (e) { + console.warn("Error fetching unread status", e); + } } } catch (e) {