diff --git a/frontend/src/pages/History/History.js b/frontend/src/pages/History/History.js index 7cc1d05..4b0691a 100644 --- a/frontend/src/pages/History/History.js +++ b/frontend/src/pages/History/History.js @@ -22,6 +22,10 @@ const History = () => { const [selectedPropertyDetails, setSelectedPropertyDetails] = useState(null); const [dateFilter, setDateFilter] = useState('all'); // all, today, week, month const [sortBy, setSortBy] = useState('date'); // date, area, duration + const [statusFilter, setStatusFilter] = useState('all'); // all, completed, archived + const [propertyFilter, setPropertyFilter] = useState('all'); + const [productFilter, setProductFilter] = useState('all'); + const [showFilters, setShowFilters] = useState(false); // Calculate coverage percentage based on GPS tracking and equipment specifications const calculateCoverage = (application, log) => { @@ -66,15 +70,21 @@ const History = () => { try { setLoading(true); - // Fetch completed applications (plans with completed status) - const plansResponse = await applicationsAPI.getPlans({ status: 'completed' }); - const completedPlans = plansResponse.data.data.plans || []; + // Fetch completed and archived applications + const [completedResponse, archivedResponse] = await Promise.all([ + applicationsAPI.getPlans({ status: 'completed' }), + applicationsAPI.getPlans({ status: 'archived' }) + ]); + + const completedPlans = completedResponse.data.data.plans || []; + const archivedPlans = archivedResponse.data.data.plans || []; + const allHistoryApplications = [...completedPlans, ...archivedPlans]; // Fetch application logs for additional details const logsResponse = await applicationsAPI.getLogs(); const logs = logsResponse.data.data.logs || []; - setCompletedApplications(completedPlans); + setCompletedApplications(allHistoryApplications); setApplicationLogs(logs); } catch (error) { @@ -95,25 +105,48 @@ const History = () => { } }; - // Filter applications based on date filter + // Get unique values for filter options + const uniqueProperties = [...new Set(completedApplications.map(app => app.propertyName))].filter(Boolean); + const uniqueProducts = [...new Set( + completedApplications.flatMap(app => + app.products ? app.products.map(p => p.productName) : [] + ) + )].filter(Boolean); + + // Filter applications based on all filters const filteredApplications = completedApplications.filter(app => { - if (dateFilter === 'all') return true; - - const appDate = new Date(app.plannedDate); - const now = new Date(); - - switch (dateFilter) { - case 'today': - return appDate.toDateString() === now.toDateString(); - case 'week': - const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); - return appDate >= weekAgo; - case 'month': - const monthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); - return appDate >= monthAgo; - default: - return true; + // Date filter + if (dateFilter !== 'all') { + const appDate = new Date(app.plannedDate); + const now = new Date(); + + switch (dateFilter) { + case 'today': + if (appDate.toDateString() !== now.toDateString()) return false; + break; + case 'week': + const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); + if (appDate < weekAgo) return false; + break; + case 'month': + const monthAgo = new Date(now.getTime() - 30 * 24 * 60 * 60 * 1000); + if (appDate < monthAgo) return false; + break; + } } + + // Status filter + if (statusFilter !== 'all' && app.status !== statusFilter) return false; + + // Property filter + if (propertyFilter !== 'all' && app.propertyName !== propertyFilter) return false; + + // Product filter + if (productFilter !== 'all') { + if (!app.products || !app.products.some(p => p.productName === productFilter)) return false; + } + + return true; }); // Sort applications @@ -195,47 +228,138 @@ const History = () => { {/* Filters and Sort */} -
Complete some applications to see them here.
++ {statusFilter === 'completed' ? 'Complete some applications to see them here.' : + statusFilter === 'archived' ? 'Archive some completed applications to see them here.' : + 'Complete and archive applications to see them here.'} +