feat: add year selection filter to TeamLeadsDashboard and update API request accordingly
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user