diff --git a/frontend/src/pages/History/History.js b/frontend/src/pages/History/History.js index b971b9a..186cf15 100644 --- a/frontend/src/pages/History/History.js +++ b/frontend/src/pages/History/History.js @@ -195,7 +195,10 @@ const History = () => { const filteredForOptions = getFilteredApplicationsForOptions(); - const uniqueProperties = [...new Set(completedApplications.map(app => app.propertyName))].filter(Boolean); + const uniqueProperties = [...new Set([ + ...completedApplications.map(app => app.propertyName), + ...mowingLogs.map(log => log.property_name) + ])].filter(Boolean); const uniqueProducts = [...new Set( filteredForOptions.flatMap(app => app.productDetails ? app.productDetails.map(p => p.name) : [] @@ -274,7 +277,58 @@ const History = () => { const totalApplications = completedApplications.length; const totalAreaTreated = completedApplications.reduce((sum, app) => sum + (app.totalSectionArea || 0), 0); const totalDuration = applicationLogs.reduce((sum, log) => sum + (log.gpsTrack?.duration || 0), 0); - const totalMowingSessions = mowingLogs.length; + // Derive filtered + sorted mowing logs using shared filters (date/property) + const filteredMowingLogs = (mowingLogs || []).filter((log) => { + // Date filter (use session_date when available, fallback to created_at) + if (dateFilter !== 'all') { + const dStr = log.session_date || log.created_at; + const logDate = dStr ? new Date(dStr) : null; + if (!logDate) return false; + const now = new Date(); + switch (dateFilter) { + case 'today': + if (logDate.toDateString() !== now.toDateString()) return false; + break; + case 'week': { + const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); + if (logDate < weekAgo) return false; + break; + } + case 'month': { + const monthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); + if (logDate < monthAgo) return false; + break; + } + case 'custom': + if (dateRangeStart) { + const startDate = new Date(dateRangeStart); + startDate.setHours(0, 0, 0, 0); + if (logDate < startDate) return false; + } + if (dateRangeEnd) { + const endDate = new Date(dateRangeEnd); + endDate.setHours(23, 59, 59, 999); + if (logDate > endDate) return false; + } + break; + default: + break; + } + } + + // Property filter (property_name from API) + if (propertyFilter !== 'all' && (log.property_name || '') !== propertyFilter) return false; + + return true; + }); + + const sortedMowingLogs = [...filteredMowingLogs].sort((a, b) => { + const aDate = a.session_date || a.created_at || 0; + const bDate = b.session_date || b.created_at || 0; + return new Date(bDate) - new Date(aDate); + }); + + const totalMowingSessions = sortedMowingLogs.length; if (loading) { return ( @@ -686,7 +740,7 @@ const History = () => {
{totalMowingSessions} session{totalMowingSessions!==1?'s':''}