|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | +import { getUsuarioLogado } from "../services/authService"; |
| 3 | +import { getNotificacoes, marcarLida, deletarNotif } from "../services/NotificacoesService"; |
| 4 | + |
| 5 | +const fmt = (v) => |
| 6 | + Number(v ?? 0).toLocaleString("pt-BR", { style: "currency", currency: "BRL" }); |
| 7 | + |
| 8 | +const fmtData = (d) => |
| 9 | + d ? new Date(d).toLocaleString("pt-BR", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "—"; |
| 10 | + |
| 11 | +const TIPO_CONFIG = { |
| 12 | + Cobrança: { icon: "💳", cor: "#7C3AED", bg: "#EDE9FE" }, |
| 13 | + Pagamento: { icon: "✅", cor: "#16A34A", bg: "#D1FAE5" }, |
| 14 | + Vencimento: { icon: "⚠️", cor: "#D97706", bg: "#FEF3C7" }, |
| 15 | + default: { icon: "🔔", cor: "#2563EB", bg: "#DBEAFE" }, |
| 16 | +}; |
| 17 | + |
| 18 | +export default function Notificacoes() { |
| 19 | + const cobrador = getUsuarioLogado()?.nome ?? ""; |
| 20 | + |
| 21 | + const [lista, setLista] = useState([]); |
| 22 | + const [loading, setLoading] = useState(true); |
| 23 | + const [filtro, setFiltro] = useState("todas"); |
| 24 | + |
| 25 | + async function carregar() { |
| 26 | + setLoading(true); |
| 27 | + try { |
| 28 | + const data = await getNotificacoes(cobrador); |
| 29 | + setLista(Array.isArray(data) ? data : []); |
| 30 | + } catch { |
| 31 | + setLista([]); |
| 32 | + } finally { |
| 33 | + setLoading(false); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + useEffect(() => { if (cobrador) carregar(); }, [cobrador]); |
| 38 | + |
| 39 | + async function handleLida(id) { |
| 40 | + try { |
| 41 | + await marcarLida(id); |
| 42 | + setLista((prev) => prev.map((n) => n.id === id ? { ...n, lida: true } : n)); |
| 43 | + } catch { /* silencioso */ } |
| 44 | + } |
| 45 | + |
| 46 | + async function handleDeletar(id) { |
| 47 | + if (!confirm("Excluir esta notificação?")) return; |
| 48 | + try { |
| 49 | + await deletarNotif(id); |
| 50 | + setLista((prev) => prev.filter((n) => n.id !== id)); |
| 51 | + } catch { /* silencioso */ } |
| 52 | + } |
| 53 | + |
| 54 | + async function marcarTodas() { |
| 55 | + const naoLidas = lista.filter((n) => !n.lida); |
| 56 | + await Promise.all(naoLidas.map((n) => marcarLida(n.id))); |
| 57 | + setLista((prev) => prev.map((n) => ({ ...n, lida: true }))); |
| 58 | + } |
| 59 | + |
| 60 | + const naoLidas = lista.filter((n) => !n.lida).length; |
| 61 | + |
| 62 | + const filtradas = lista.filter((n) => { |
| 63 | + if (filtro === "naoLidas") return !n.lida; |
| 64 | + if (filtro === "lidas") return n.lida; |
| 65 | + return true; |
| 66 | + }); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div style={s.page}> |
| 70 | + <div style={s.header}> |
| 71 | + <div> |
| 72 | + <h1 style={s.titulo}> |
| 73 | + Notificações |
| 74 | + {naoLidas > 0 && <span style={s.badge}>{naoLidas}</span>} |
| 75 | + </h1> |
| 76 | + <p style={s.sub}>{lista.length} notificação(ões) no total</p> |
| 77 | + </div> |
| 78 | + {naoLidas > 0 && ( |
| 79 | + <button style={s.btnMarcar} onClick={marcarTodas}> |
| 80 | + ✔ Marcar todas como lidas |
| 81 | + </button> |
| 82 | + )} |
| 83 | + </div> |
| 84 | + |
| 85 | + {/* Filtros */} |
| 86 | + <div style={s.filtros}> |
| 87 | + {[ |
| 88 | + { key: "todas", label: "Todas" }, |
| 89 | + { key: "naoLidas", label: `Não lidas${naoLidas > 0 ? ` (${naoLidas})` : ""}` }, |
| 90 | + { key: "lidas", label: "Lidas" }, |
| 91 | + ].map((f) => ( |
| 92 | + <button |
| 93 | + key={f.key} |
| 94 | + style={{ ...s.filtroBtn, ...(filtro === f.key ? s.filtroBtnAtivo : {}) }} |
| 95 | + onClick={() => setFiltro(f.key)} |
| 96 | + > |
| 97 | + {f.label} |
| 98 | + </button> |
| 99 | + ))} |
| 100 | + </div> |
| 101 | + |
| 102 | + {/* Lista */} |
| 103 | + {loading ? ( |
| 104 | + <div style={s.vazio}>Carregando notificações...</div> |
| 105 | + ) : filtradas.length === 0 ? ( |
| 106 | + <div style={s.vazioBox}> |
| 107 | + <span style={{ fontSize: "2.5rem" }}>🔔</span> |
| 108 | + <p style={s.vazioTexto}>Nenhuma notificação encontrada.</p> |
| 109 | + </div> |
| 110 | + ) : ( |
| 111 | + <div style={s.lista}> |
| 112 | + {filtradas.map((n) => { |
| 113 | + const cfg = TIPO_CONFIG[n.tipo] ?? TIPO_CONFIG.default; |
| 114 | + return ( |
| 115 | + <div key={n.id} style={{ ...s.card, opacity: n.lida ? 0.65 : 1, borderLeft: `4px solid ${cfg.cor}` }}> |
| 116 | + <div style={{ ...s.iconBox, background: cfg.bg, color: cfg.cor }}> |
| 117 | + {cfg.icon} |
| 118 | + </div> |
| 119 | + <div style={s.cardBody}> |
| 120 | + <div style={s.cardTop}> |
| 121 | + <span style={s.cardNome}>{n.clienteNome}</span> |
| 122 | + <span style={{ ...s.tipoBadge, background: cfg.bg, color: cfg.cor }}>{n.tipo}</span> |
| 123 | + </div> |
| 124 | + <p style={s.cardMsg}>{n.mensagem}</p> |
| 125 | + <div style={s.cardBottom}> |
| 126 | + <span style={s.cardData}>{fmtData(n.dataCriacao ?? n.data)}</span> |
| 127 | + {n.valor > 0 && <span style={s.cardValor}>{fmt(n.valor)}</span>} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + <div style={s.acoes}> |
| 131 | + {!n.lida && ( |
| 132 | + <button style={s.btnLer} onClick={() => handleLida(n.id)} title="Marcar como lida">✔</button> |
| 133 | + )} |
| 134 | + <button style={s.btnDel} onClick={() => handleDeletar(n.id)} title="Excluir">✕</button> |
| 135 | + </div> |
| 136 | + {!n.lida && <span style={s.ponto} />} |
| 137 | + </div> |
| 138 | + ); |
| 139 | + })} |
| 140 | + </div> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + ); |
| 144 | +} |
| 145 | + |
| 146 | +const s = { |
| 147 | + page: { padding: "2rem", background: "#F5F3FF", minHeight: "100vh" }, |
| 148 | + header: { display: "flex", justifyContent: "space-between", alignItems: "flex-start", marginBottom: "1.25rem" }, |
| 149 | + titulo: { margin: 0, fontSize: "1.8rem", fontWeight: 700, color: "#1F2937", display: "flex", alignItems: "center", gap: 10 }, |
| 150 | + badge: { background: "#7C3AED", color: "#fff", borderRadius: 20, padding: "2px 10px", fontSize: "0.85rem", fontWeight: 700 }, |
| 151 | + sub: { margin: "0.25rem 0 0", color: "#6B7280", fontSize: "0.9rem" }, |
| 152 | + btnMarcar: { background: "#7C3AED", color: "#fff", border: "none", borderRadius: 8, padding: "0.5rem 1.1rem", cursor: "pointer", fontWeight: 600, fontSize: "0.85rem" }, |
| 153 | + filtros: { display: "flex", gap: 8, marginBottom: "1.25rem", flexWrap: "wrap" }, |
| 154 | + filtroBtn: { background: "#fff", border: "1px solid #E5E7EB", borderRadius: 8, padding: "0.4rem 1rem", cursor: "pointer", fontSize: "0.85rem", color: "#6B7280" }, |
| 155 | + filtroBtnAtivo:{ background: "#7C3AED", color: "#fff", border: "1px solid #7C3AED" }, |
| 156 | + lista: { display: "flex", flexDirection: "column", gap: 10 }, |
| 157 | + card: { background: "#fff", borderRadius: 12, padding: "1rem 1.25rem", display: "flex", alignItems: "flex-start", gap: "1rem", boxShadow: "0 1px 4px rgba(0,0,0,0.06)", position: "relative" }, |
| 158 | + iconBox: { fontSize: "1.4rem", borderRadius: 10, width: 44, height: 44, display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }, |
| 159 | + cardBody: { flex: 1, minWidth: 0 }, |
| 160 | + cardTop: { display: "flex", alignItems: "center", gap: 8, marginBottom: 4 }, |
| 161 | + cardNome: { fontWeight: 700, fontSize: "0.95rem", color: "#1F2937" }, |
| 162 | + tipoBadge: { fontSize: "0.72rem", fontWeight: 600, borderRadius: 20, padding: "2px 8px" }, |
| 163 | + cardMsg: { margin: "0 0 6px", fontSize: "0.85rem", color: "#4B5563" }, |
| 164 | + cardBottom: { display: "flex", gap: 12, alignItems: "center" }, |
| 165 | + cardData: { fontSize: "0.75rem", color: "#9CA3AF" }, |
| 166 | + cardValor: { fontSize: "0.85rem", fontWeight: 700, color: "#7C3AED" }, |
| 167 | + acoes: { display: "flex", flexDirection: "column", gap: 6, flexShrink: 0 }, |
| 168 | + btnLer: { background: "#D1FAE5", color: "#16A34A", border: "none", borderRadius: 6, padding: "4px 8px", cursor: "pointer", fontSize: 13 }, |
| 169 | + btnDel: { background: "#FEE2E2", color: "#DC2626", border: "none", borderRadius: 6, padding: "4px 8px", cursor: "pointer", fontSize: 13 }, |
| 170 | + ponto: { position: "absolute", top: 12, right: 12, width: 8, height: 8, background: "#7C3AED", borderRadius: "50%" }, |
| 171 | + vazio: { textAlign: "center", color: "#9CA3AF", padding: "3rem 0" }, |
| 172 | + vazioBox: { display: "flex", flexDirection: "column", alignItems: "center", gap: 12, padding: "3rem 0" }, |
| 173 | + vazioTexto: { color: "#9CA3AF", fontSize: "0.95rem" }, |
| 174 | +}; |
0 commit comments