Update FamilyList.tsx
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user