Update FamilyList.tsx
This commit is contained in:
@@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { CondoService } from '../services/mockDb';
|
||||
import { Family, Condo, Notice, AppSettings, Ticket, TicketStatus } from '../types';
|
||||
import { Search, ChevronRight, UserCircle, Building, Bell, AlertTriangle, Hammer, Calendar, Info, Link as LinkIcon, Check, Wallet, Briefcase, MessageSquareWarning, ArrowRight, CheckCircle2, ChevronDown, ChevronUp, Eye } from 'lucide-react';
|
||||
import { Search, ChevronRight, UserCircle, Building, Bell, AlertTriangle, Hammer, Calendar, Info, Link as LinkIcon, Check, Wallet, Briefcase, MessageSquareWarning, ArrowRight, CheckCircle2, ChevronDown, ChevronUp, Eye, Inbox } from 'lucide-react';
|
||||
|
||||
export const FamilyList: React.FC = () => {
|
||||
const navigate = useNavigate();
|
||||
@@ -34,18 +34,30 @@ export const FamilyList: React.FC = () => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
CondoService.seedPayments();
|
||||
const [fams, condo, allNotices, appSettings] = await Promise.all([
|
||||
CondoService.getFamilies(),
|
||||
CondoService.getActiveCondo(),
|
||||
CondoService.getNotices(),
|
||||
CondoService.getSettings()
|
||||
|
||||
// 1. Fetch Global Settings & Active Condo FIRST
|
||||
const [appSettings, condo] = await Promise.all([
|
||||
CondoService.getSettings(),
|
||||
CondoService.getActiveCondo()
|
||||
]);
|
||||
|
||||
setSettings(appSettings);
|
||||
setActiveCondo(condo);
|
||||
|
||||
if (!condo) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Fetch specific data using condo.id (prevents race conditions)
|
||||
const [fams, allNotices] = await Promise.all([
|
||||
CondoService.getFamilies(condo.id),
|
||||
CondoService.getNotices(condo.id)
|
||||
]);
|
||||
setFamilies(fams);
|
||||
setActiveCondo(condo);
|
||||
setSettings(appSettings);
|
||||
|
||||
// --- USER SPECIFIC DASHBOARD DATA ---
|
||||
if (currentUser && !isPrivileged && currentUser.familyId && condo) {
|
||||
if (currentUser && !isPrivileged && currentUser.familyId) {
|
||||
// 1. Find My Family
|
||||
const me = fams.find(f => f.id === currentUser.familyId) || null;
|
||||
setMyFamily(me);
|
||||
@@ -98,7 +110,7 @@ export const FamilyList: React.FC = () => {
|
||||
}
|
||||
|
||||
// --- NOTICE LOGIC ---
|
||||
if (condo && currentUser && appSettings.features.notices) {
|
||||
if (currentUser && appSettings.features.notices) {
|
||||
// Filter: Must be same condo AND Active
|
||||
// Visibility: Admin sees all. User sees Public OR Targeted.
|
||||
const relevantNotices = allNotices.filter(n => {
|
||||
@@ -184,104 +196,99 @@ export const FamilyList: React.FC = () => {
|
||||
<div className="space-y-8 pb-12 animate-fade-in">
|
||||
|
||||
{/* 1. BACHECA CONDOMINIALE (Notices) */}
|
||||
{/* This section is rendered if notices feature is enabled and there are notices */}
|
||||
{settings?.features.notices && notices.length > 0 && (
|
||||
{/* Visualizzata SEMPRE se la feature è attiva, anche se vuota */}
|
||||
{settings?.features.notices && (
|
||||
<div className="space-y-4">
|
||||
<h3 className="font-bold text-slate-800 text-lg flex items-center gap-2">
|
||||
<Bell className="w-5 h-5 text-blue-600" /> Bacheca Condominiale
|
||||
</h3>
|
||||
<div className="grid grid-cols-1 gap-4">
|
||||
{notices.map(notice => {
|
||||
const isRead = userReadIds.includes(notice.id);
|
||||
const isExpanded = expandedNoticeId === notice.id;
|
||||
|
||||
// Style configuration
|
||||
// New notices get a distinct border and white background.
|
||||
// Read notices get a slate background and are slightly dimmed to indicate "archive" status.
|
||||
let containerClass = isRead
|
||||
? 'bg-slate-50 border-slate-200'
|
||||
: 'bg-white border-blue-200 shadow-sm ring-1 ring-blue-50';
|
||||
{notices.length === 0 ? (
|
||||
<div className="bg-white rounded-xl border border-slate-200 p-6 text-center shadow-sm">
|
||||
<Inbox className="w-10 h-10 text-slate-300 mx-auto mb-2" />
|
||||
<p className="text-slate-500 text-sm">Nessun avviso in bacheca.</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-4">
|
||||
{notices.map(notice => {
|
||||
const isRead = userReadIds.includes(notice.id);
|
||||
const isExpanded = expandedNoticeId === notice.id;
|
||||
|
||||
if (notice.type === 'warning' && !isRead) {
|
||||
containerClass = 'bg-amber-50/50 border-amber-300 shadow-sm ring-1 ring-amber-50';
|
||||
}
|
||||
let containerClass = isRead
|
||||
? 'bg-slate-50 border-slate-200'
|
||||
: 'bg-white border-blue-200 shadow-sm ring-1 ring-blue-50';
|
||||
|
||||
return (
|
||||
<div key={notice.id} className={`rounded-xl border p-4 transition-all duration-300 ${containerClass}`}>
|
||||
<div className="flex items-start gap-4">
|
||||
{/* Icon Column */}
|
||||
<div className={`p-2.5 rounded-full flex-shrink-0 ${isRead ? 'bg-slate-200 grayscale opacity-70' : 'bg-white shadow-sm'}`}>
|
||||
<NoticeIcon type={notice.type} />
|
||||
</div>
|
||||
if (notice.type === 'warning' && !isRead) {
|
||||
containerClass = 'bg-amber-50/50 border-amber-300 shadow-sm ring-1 ring-amber-50';
|
||||
}
|
||||
|
||||
{/* Content Column */}
|
||||
<div className="flex-1 min-w-0">
|
||||
{/* Header: Title + Badges */}
|
||||
<div className="flex items-center justify-between gap-2 mb-1.5">
|
||||
<div className="flex items-center gap-2 overflow-hidden">
|
||||
<h4 className={`font-bold text-base truncate ${isRead ? 'text-slate-500' : 'text-slate-900'}`}>{notice.title}</h4>
|
||||
{!isRead && (
|
||||
<span className="text-[10px] bg-blue-600 text-white px-2 py-0.5 rounded-full font-bold uppercase tracking-wide animate-pulse">
|
||||
Nuovo
|
||||
</span>
|
||||
)}
|
||||
{isRead && (
|
||||
<span className="text-[10px] bg-slate-200 text-slate-500 px-2 py-0.5 rounded-full font-bold uppercase tracking-wide flex items-center gap-1">
|
||||
<Check className="w-3 h-3"/> Letto
|
||||
</span>
|
||||
)}
|
||||
return (
|
||||
<div key={notice.id} className={`rounded-xl border p-4 transition-all duration-300 ${containerClass}`}>
|
||||
<div className="flex items-start gap-4">
|
||||
<div className={`p-2.5 rounded-full flex-shrink-0 ${isRead ? 'bg-slate-200 grayscale opacity-70' : 'bg-white shadow-sm'}`}>
|
||||
<NoticeIcon type={notice.type} />
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center justify-between gap-2 mb-1.5">
|
||||
<div className="flex items-center gap-2 overflow-hidden">
|
||||
<h4 className={`font-bold text-base truncate ${isRead ? 'text-slate-500' : 'text-slate-900'}`}>{notice.title}</h4>
|
||||
{!isRead && (
|
||||
<span className="text-[10px] bg-blue-600 text-white px-2 py-0.5 rounded-full font-bold uppercase tracking-wide animate-pulse">
|
||||
Nuovo
|
||||
</span>
|
||||
)}
|
||||
{isRead && (
|
||||
<span className="text-[10px] bg-slate-200 text-slate-500 px-2 py-0.5 rounded-full font-bold uppercase tracking-wide flex items-center gap-1">
|
||||
<Check className="w-3 h-3"/> Letto
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-slate-400 whitespace-nowrap">{new Date(notice.date).toLocaleDateString()}</span>
|
||||
</div>
|
||||
<span className="text-xs text-slate-400 whitespace-nowrap">{new Date(notice.date).toLocaleDateString()}</span>
|
||||
</div>
|
||||
|
||||
{/* Body Text */}
|
||||
<div className={`text-sm leading-relaxed whitespace-pre-wrap transition-all ${isRead ? 'text-slate-500' : 'text-slate-700'} ${isExpanded ? '' : 'line-clamp-2'}`}>
|
||||
{notice.content}
|
||||
</div>
|
||||
<div className={`text-sm leading-relaxed whitespace-pre-wrap transition-all ${isRead ? 'text-slate-500' : 'text-slate-700'} ${isExpanded ? '' : 'line-clamp-2'}`}>
|
||||
{notice.content}
|
||||
</div>
|
||||
|
||||
{/* Footer Actions */}
|
||||
<div className="flex items-center justify-between mt-3 pt-2 border-t border-slate-200/50">
|
||||
<div className="flex items-center gap-4">
|
||||
{/* Expand Button (only if long content) */}
|
||||
{(notice.content.length > 120 || notice.content.includes('\n')) && (
|
||||
<div className="flex items-center justify-between mt-3 pt-2 border-t border-slate-200/50">
|
||||
<div className="flex items-center gap-4">
|
||||
{(notice.content.length > 120 || notice.content.includes('\n')) && (
|
||||
<button
|
||||
onClick={() => toggleExpandNotice(notice.id)}
|
||||
className="text-xs font-medium text-slate-500 hover:text-blue-600 flex items-center gap-1"
|
||||
>
|
||||
{isExpanded ? <><ChevronUp className="w-3 h-3"/> Riduci</> : <><ChevronDown className="w-3 h-3"/> Leggi tutto</>}
|
||||
</button>
|
||||
)}
|
||||
{notice.link && (
|
||||
<a href={notice.link} target="_blank" rel="noopener noreferrer" className="text-xs font-medium text-blue-600 hover:text-blue-800 hover:underline flex items-center gap-1">
|
||||
<LinkIcon className="w-3 h-3"/> Apri Allegato
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!isRead && (
|
||||
<button
|
||||
onClick={() => toggleExpandNotice(notice.id)}
|
||||
className="text-xs font-medium text-slate-500 hover:text-blue-600 flex items-center gap-1"
|
||||
onClick={() => handleMarkAsRead(notice.id)}
|
||||
className="text-xs bg-white border border-slate-200 hover:bg-blue-50 hover:text-blue-700 hover:border-blue-200 text-slate-600 px-3 py-1.5 rounded-lg font-medium transition-all flex items-center gap-1 shadow-sm"
|
||||
>
|
||||
{isExpanded ? <><ChevronUp className="w-3 h-3"/> Riduci</> : <><ChevronDown className="w-3 h-3"/> Leggi tutto</>}
|
||||
<CheckCircle2 className="w-3 h-3"/> Segna come letto
|
||||
</button>
|
||||
)}
|
||||
|
||||
{/* Link Button */}
|
||||
{notice.link && (
|
||||
<a href={notice.link} target="_blank" rel="noopener noreferrer" className="text-xs font-medium text-blue-600 hover:text-blue-800 hover:underline flex items-center gap-1">
|
||||
<LinkIcon className="w-3 h-3"/> Apri Allegato
|
||||
</a>
|
||||
{isRead && (
|
||||
<span className="text-[10px] text-slate-400 flex items-center gap-1">
|
||||
<Eye className="w-3 h-3"/> Archiviato
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Mark as Read Button (Only if not read) */}
|
||||
{!isRead && (
|
||||
<button
|
||||
onClick={() => handleMarkAsRead(notice.id)}
|
||||
className="text-xs bg-white border border-slate-200 hover:bg-blue-50 hover:text-blue-700 hover:border-blue-200 text-slate-600 px-3 py-1.5 rounded-lg font-medium transition-all flex items-center gap-1 shadow-sm"
|
||||
>
|
||||
<CheckCircle2 className="w-3 h-3"/> Segna come letto
|
||||
</button>
|
||||
)}
|
||||
{/* Re-read indicator (optional visual cue) */}
|
||||
{isRead && (
|
||||
<span className="text-[10px] text-slate-400 flex items-center gap-1">
|
||||
<Eye className="w-3 h-3"/> Archiviato
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user