diff --git a/frontend/contexts/DataContext.tsx b/frontend/contexts/DataContext.tsx index f8b800d..63784c7 100644 --- a/frontend/contexts/DataContext.tsx +++ b/frontend/contexts/DataContext.tsx @@ -1,6 +1,7 @@ import React, { createContext, useContext, useState, ReactNode, useEffect } from "react"; import { useAuth } from "./AuthContext"; import { getPendingUsers, approveUser as apiApproveUser, getProfessionals, assignProfessional as apiAssignProfessional, removeProfessional as apiRemoveProfessional, updateEventStatus as apiUpdateStatus, updateAssignmentStatus as apiUpdateAssignmentStatus, updateAgenda as apiUpdateAgenda } from "../services/apiService"; +import { useRegion } from "./RegionContext"; import { EventData, EventStatus, @@ -621,6 +622,7 @@ export const DataProvider: React.FC<{ children: ReactNode }> = ({ children, }) => { const { token, user } = useAuth(); // Consume Auth Context + const { currentRegion, isRegionReady } = useRegion(); const [events, setEvents] = useState([]); const [institutions, setInstitutions] = useState(INITIAL_INSTITUTIONS); @@ -639,7 +641,7 @@ export const DataProvider: React.FC<{ children: ReactNode }> = ({ // Use token from context or fallback to localStorage if context not ready (though context is preferred sources of truth) const visibleToken = token || localStorage.getItem("token"); - if (visibleToken) { + if (visibleToken && isRegionReady) { setIsLoading(true); try { // Import dynamic to avoid circular dependency if any, or just use imported service @@ -747,7 +749,7 @@ export const DataProvider: React.FC<{ children: ReactNode }> = ({ } }; fetchEvents(); - }, [token, refreshTrigger]); // React to token change and manual refresh + }, [token, refreshTrigger, currentRegion, isRegionReady]); // React to context changes const refreshEvents = async () => { setRefreshTrigger(prev => prev + 1); @@ -795,7 +797,7 @@ export const DataProvider: React.FC<{ children: ReactNode }> = ({ useEffect(() => { const fetchProfs = async () => { const token = localStorage.getItem('token'); - if (token) { + if (token && isRegionReady) { try { const result = await getProfessionals(token); if (result.data) { @@ -850,7 +852,7 @@ export const DataProvider: React.FC<{ children: ReactNode }> = ({ } }; fetchProfs(); - }, [token]); + }, [token, currentRegion, isRegionReady]); const addEvent = async (event: any) => { const token = localStorage.getItem("token"); diff --git a/frontend/contexts/RegionContext.tsx b/frontend/contexts/RegionContext.tsx index b7273f8..d615ac2 100644 --- a/frontend/contexts/RegionContext.tsx +++ b/frontend/contexts/RegionContext.tsx @@ -7,12 +7,14 @@ interface RegionContextType { currentRegion: string; setRegion: (region: string) => void; availableRegions: string[]; + isRegionReady: boolean; } const RegionContext = createContext({ currentRegion: "SP", setRegion: () => {}, availableRegions: [], + isRegionReady: false, }); export const RegionProvider: React.FC<{ children: React.ReactNode }> = ({ @@ -32,9 +34,18 @@ export const RegionProvider: React.FC<{ children: React.ReactNode }> = ({ // Let's assume public = SP only or no switcher. // BUT: If user is logged out, they shouldn't see switcher anyway. const [availableRegions, setAvailableRegions] = useState(["SP"]); + const [isRegionReady, setIsRegionReady] = useState(false); useEffect(() => { console.log("RegionContext Debug:", { user, allowedRegions: user?.allowedRegions }); + // If not logged in or user still fetching, wait (but if public page, we could mark ready. For now, mark ready if no token or after user loads) + const token = localStorage.getItem("token"); + if (token && !user) { + // Wait for user to load to evaluate regions + setIsRegionReady(false); + return; + } + if (user && user.allowedRegions && user.allowedRegions.length > 0) { setAvailableRegions(user.allowedRegions); @@ -49,7 +60,9 @@ export const RegionProvider: React.FC<{ children: React.ReactNode }> = ({ // Fallback or Public setAvailableRegions(["SP"]); } - }, [user, user?.allowedRegions, currentRegion]); + + setIsRegionReady(true); + }, [user, currentRegion]); useEffect(() => { localStorage.setItem(REGION_KEY, currentRegion); @@ -67,7 +80,7 @@ export const RegionProvider: React.FC<{ children: React.ReactNode }> = ({ return ( {children} diff --git a/frontend/pages/ProfessionalStatement.tsx b/frontend/pages/ProfessionalStatement.tsx index 0879e64..d7bfe26 100644 --- a/frontend/pages/ProfessionalStatement.tsx +++ b/frontend/pages/ProfessionalStatement.tsx @@ -31,6 +31,21 @@ const ProfessionalStatement: React.FC = () => { const [selectedTransaction, setSelectedTransaction] = useState(null); const [isModalOpen, setIsModalOpen] = useState(false); + // Filter States + const [filters, setFilters] = useState({ + data: "", + nome: "", + tipo: "", + empresa: "", + status: "" + }); + + const [dateFilters, setDateFilters] = useState({ + startDate: "", + endDate: "" + }); + const [showDateFilters, setShowDateFilters] = useState(false); + useEffect(() => { if (token) { fetchStatement(); @@ -82,6 +97,43 @@ const ProfessionalStatement: React.FC = () => { ); }; + // derived filtered state + const transactions = data?.transactions || []; + const filteredTransactions = transactions.filter(t => { + // String Column Filters + if (filters.data && !t.data_evento.toLowerCase().includes(filters.data.toLowerCase())) return false; + if (filters.nome && !t.nome_evento.toLowerCase().includes(filters.nome.toLowerCase())) return false; + if (filters.tipo && !t.tipo_evento.toLowerCase().includes(filters.tipo.toLowerCase())) return false; + if (filters.empresa && !t.empresa.toLowerCase().includes(filters.empresa.toLowerCase())) return false; + if (filters.status && !t.status.toLowerCase().includes(filters.status.toLowerCase())) return false; + + // Date Range Filter logic + if (dateFilters.startDate || dateFilters.endDate) { + // Parse DD/MM/YYYY into JS Date if possible + const [d, m, y] = t.data_evento.split('/'); + if (d && m && y) { + const eventDateObj = new Date(parseInt(y), parseInt(m) - 1, parseInt(d)); + + if (dateFilters.startDate) { + const [sy, sm, sd] = dateFilters.startDate.split('-'); + const startObj = new Date(parseInt(sy), parseInt(sm) - 1, parseInt(sd)); + if (eventDateObj < startObj) return false; + } + if (dateFilters.endDate) { + const [ey, em, ed] = dateFilters.endDate.split('-'); + const endObj = new Date(parseInt(ey), parseInt(em) - 1, parseInt(ed)); + // Set end of day for precise comparison + endObj.setHours(23, 59, 59, 999); + if (eventDateObj > endObj) return false; + } + } + } + + return true; + }); + + const filteredTotalSum = filteredTransactions.reduce((acc, curr) => acc + curr.valor_recebido, 0); + if (loading) { return
Carregando extrato...
; } @@ -126,36 +178,103 @@ const ProfessionalStatement: React.FC = () => {

Histórico de Pagamentos

- +
+ +
+ + {/* Advanced Date Filters */} + {showDateFilters && ( +
+
+ + setDateFilters({...dateFilters, startDate: e.target.value})} + /> +
+ +
+ + setDateFilters({...dateFilters, endDate: e.target.value})} + /> +
+ + {(dateFilters.startDate || dateFilters.endDate) && ( +
+ +
+ )} +
+ )}
- - - - - - - + + + + + + + - {(data.transactions || []).length === 0 ? ( + {filteredTransactions.length === 0 ? ( ) : ( - (data.transactions || []).map((t) => ( + filteredTransactions.map((t) => ( {
Data EventoNome EventoTipo EventoEmpresaValor RecebidoData PagamentoStatus +
+ Data Evento + setFilters({...filters, data: e.target.value})} /> +
+
+
+ Nome Evento + setFilters({...filters, nome: e.target.value})} /> +
+
+
+ Tipo Evento + setFilters({...filters, tipo: e.target.value})} /> +
+
+
+ Empresa + setFilters({...filters, empresa: e.target.value})} /> +
+
+
Valor Recebido
+
+
Data Pagamento
+
+
+ Status + setFilters({...filters, status: e.target.value})} /> +
+
- Nenhum pagamento registrado. + Nenhum pagamento encontrado para os filtros.
- Total de pagamentos: {(data.transactions || []).length} +
+ Total filtrado: {filteredTransactions.length} + Soma Agrupada: {formatCurrency(filteredTotalSum)} +