Update FamilyList.tsx
This commit is contained in:
@@ -111,16 +111,19 @@ export const FamilyList: React.FC = () => {
|
|||||||
|
|
||||||
// --- NOTICE LOGIC ---
|
// --- NOTICE LOGIC ---
|
||||||
if (currentUser && appSettings.features.notices) {
|
if (currentUser && appSettings.features.notices) {
|
||||||
// Filter: Must be same condo AND Active
|
// Filter Visibility: Admin sees all. User sees Public OR Targeted.
|
||||||
// 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 => {
|
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;
|
if (isPrivileged) return true;
|
||||||
|
|
||||||
// Check visibility for regular users
|
// Check visibility for regular users
|
||||||
const isPublic = !n.targetFamilyIds || n.targetFamilyIds.length === 0;
|
// Handle case where targetFamilyIds might be null/undefined from backend
|
||||||
const isTargeted = currentUser.familyId && n.targetFamilyIds?.includes(currentUser.familyId);
|
const targets = n.targetFamilyIds || [];
|
||||||
|
const isPublic = targets.length === 0;
|
||||||
|
const isTargeted = currentUser.familyId && targets.includes(currentUser.familyId);
|
||||||
|
|
||||||
return isPublic || isTargeted;
|
return isPublic || isTargeted;
|
||||||
});
|
});
|
||||||
@@ -130,14 +133,23 @@ export const FamilyList: React.FC = () => {
|
|||||||
setNotices(relevantNotices);
|
setNotices(relevantNotices);
|
||||||
|
|
||||||
// Check read status for current user
|
// Check read status for current user
|
||||||
const readStatuses = await Promise.all(relevantNotices.map(n => CondoService.getNoticeReadStatus(n.id)));
|
// We use getUnreadNoticesForUser to find what is NOT read, then derive read IDs.
|
||||||
const readIds: string[] = [];
|
// This avoids using the Admin-only 'getNoticeReadStatus' endpoint.
|
||||||
readStatuses.forEach((reads, idx) => {
|
try {
|
||||||
if (reads.find(r => r.userId === currentUser.id)) {
|
if (currentUser.id && condo.id) {
|
||||||
readIds.push(relevantNotices[idx].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);
|
||||||
}
|
}
|
||||||
});
|
} catch (e) {
|
||||||
setUserReadIds(readIds);
|
console.warn("Error fetching unread status", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user