From 1d8ee8b0ab4803f6a192bdf1fcba423cf19d42ec Mon Sep 17 00:00:00 2001 From: RKWojs Date: Fri, 14 Nov 2025 15:17:29 +0100 Subject: [PATCH] feat: add year selection filter to TeamLeadsDashboard and update API request accordingly --- src/app/api/dashboard/route.js | 52 +++++++++++++++++++++++++++------- src/app/dashboard/page.js | 47 ++++++++++++++++++++++++++---- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/app/api/dashboard/route.js b/src/app/api/dashboard/route.js index 34e3229..94e679b 100644 --- a/src/app/api/dashboard/route.js +++ b/src/app/api/dashboard/route.js @@ -18,6 +18,9 @@ export async function GET(request) { return NextResponse.json({ error: "Forbidden" }, { status: 403 }); } + const { searchParams } = new URL(request.url); + const selectedYear = searchParams.get('year') ? parseInt(searchParams.get('year')) : null; + // Get all projects const projects = getAllProjects(); @@ -65,25 +68,41 @@ export async function GET(request) { let chartData; let summary; if (completedProjects.length === 0) { - // Generate continuous sample data from Jan 2024 to current month + // Generate continuous sample data based on selected year or default range const currentDate = new Date(); - const startDate = new Date(2024, 0, 1); // Jan 2024 + let startDate, endDate; + + if (selectedYear) { + startDate = new Date(selectedYear, 0, 1); // Jan 1st of selected year + endDate = new Date(selectedYear, 11, 31); // Dec 31st of selected year + if (endDate > currentDate) endDate = currentDate; + } else { + startDate = new Date(2024, 0, 1); // Jan 2024 + endDate = currentDate; + } + chartData = []; let cumulative = 0; let tempDate = new Date(startDate); - while (tempDate <= currentDate) { + while (tempDate <= endDate) { const monthName = tempDate.toLocaleDateString('en-US', { year: 'numeric', month: 'short' }); let monthlyValue = 0; - // Add some sample values for certain months - if (tempDate.getMonth() === 0 && tempDate.getFullYear() === 2024) monthlyValue = 50000; // Jan 2024 - else if (tempDate.getMonth() === 1 && tempDate.getFullYear() === 2024) monthlyValue = 75000; // Feb 2024 - else if (tempDate.getMonth() === 2 && tempDate.getFullYear() === 2024) monthlyValue = 60000; // Mar 2024 - else if (tempDate.getMonth() === 7 && tempDate.getFullYear() === 2024) monthlyValue = 10841; // Aug 2024 (real data) - else if (tempDate.getMonth() === 8 && tempDate.getFullYear() === 2024) monthlyValue = 18942; // Sep 2024 - else if (tempDate.getMonth() === 9 && tempDate.getFullYear() === 2024) monthlyValue = 13945; // Oct 2024 - else if (tempDate.getMonth() === 10 && tempDate.getFullYear() === 2024) monthlyValue = 12542; // Nov 2024 + // Add some sample values for certain months (only if they match the selected year or no year selected) + const shouldAddData = !selectedYear || tempDate.getFullYear() === selectedYear; + + if (shouldAddData) { + if (tempDate.getMonth() === 0 && tempDate.getFullYear() === 2024) monthlyValue = 50000; // Jan 2024 + else if (tempDate.getMonth() === 1 && tempDate.getFullYear() === 2024) monthlyValue = 75000; // Feb 2024 + else if (tempDate.getMonth() === 2 && tempDate.getFullYear() === 2024) monthlyValue = 60000; // Mar 2024 + else if (tempDate.getMonth() === 7 && tempDate.getFullYear() === 2024) monthlyValue = 10841; // Aug 2024 (real data) + else if (tempDate.getMonth() === 8 && tempDate.getFullYear() === 2024) monthlyValue = 18942; // Sep 2024 + else if (tempDate.getMonth() === 9 && tempDate.getFullYear() === 2024) monthlyValue = 13945; // Oct 2024 + else if (tempDate.getMonth() === 10 && tempDate.getFullYear() === 2024) monthlyValue = 12542; // Nov 2024 + else if (tempDate.getMonth() === 0 && tempDate.getFullYear() === 2025) monthlyValue = 25000; // Jan 2025 + else if (tempDate.getMonth() === 1 && tempDate.getFullYear() === 2025) monthlyValue = 35000; // Feb 2025 + } cumulative += monthlyValue; chartData.push({ @@ -158,6 +177,17 @@ export async function GET(request) { startDate = new Date(startDate.getFullYear(), startDate.getMonth(), 1); } + // If a specific year is selected, adjust the date range + if (selectedYear) { + startDate = new Date(selectedYear, 0, 1); // January 1st of selected year + endDate = new Date(selectedYear, 11, 31); // December 31st of selected year + + // Don't go beyond current date + if (endDate > new Date()) { + endDate = new Date(); + } + } + // Generate all months from start to current const allMonths = {}; let currentDate = new Date(startDate); diff --git a/src/app/dashboard/page.js b/src/app/dashboard/page.js index f51d27c..f734357 100644 --- a/src/app/dashboard/page.js +++ b/src/app/dashboard/page.js @@ -8,14 +8,21 @@ export default function TeamLeadsDashboard() { const [summaryData, setSummaryData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); + const [selectedYear, setSelectedYear] = useState(null); useEffect(() => { fetchDashboardData(); - }, []); + }, [selectedYear]); const fetchDashboardData = async () => { try { - const response = await fetch('/api/dashboard'); + const params = new URLSearchParams(); + if (selectedYear) { + params.append('year', selectedYear.toString()); + } + + const url = `/api/dashboard${params.toString() ? '?' + params.toString() : ''}`; + const response = await fetch(url); if (!response.ok) { throw new Error('Failed to fetch dashboard data'); } @@ -29,6 +36,16 @@ export default function TeamLeadsDashboard() { } }; + const currentYear = new Date().getFullYear(); + const availableYears = []; + for (let year = currentYear; year >= 2023; year--) { + availableYears.push(year); + } + + const handleYearChange = (year) => { + setSelectedYear(year === 'all' ? null : parseInt(year)); + }; + const formatCurrency = (value) => { return new Intl.NumberFormat('pl-PL', { style: 'currency', @@ -61,9 +78,29 @@ export default function TeamLeadsDashboard() { return (
-

- Dashboard -

+
+

+ Dashboard +

+ + {/* Year Filter */} +
+ + +
+