Add custom date range support to profit analytics

Introduces a date range picker to the profit analytics dashboard, allowing users to select custom date ranges for profit calculations. Updates ProfitAnalyticsChart and profit-analytics-service to handle both period and date range queries, improving flexibility and user experience.
This commit is contained in:
g
2025-11-28 18:32:38 +00:00
parent 28e292abb3
commit b10b2f2701
5 changed files with 98 additions and 14 deletions

View File

@@ -15,15 +15,16 @@ import {
} from "lucide-react";
import { useToast } from "@/hooks/use-toast";
import { formatGBP } from "@/utils/format";
import { getProfitOverview, type ProfitOverview } from "@/lib/services/profit-analytics-service";
import { getProfitOverview, type ProfitOverview, type DateRange } from "@/lib/services/profit-analytics-service";
import { Skeleton } from "@/components/ui/skeleton";
interface ProfitAnalyticsChartProps {
timeRange: string;
timeRange?: string;
dateRange?: DateRange;
hideNumbers?: boolean;
}
export default function ProfitAnalyticsChart({ timeRange, hideNumbers = false }: ProfitAnalyticsChartProps) {
export default function ProfitAnalyticsChart({ timeRange, dateRange, hideNumbers = false }: ProfitAnalyticsChartProps) {
const [data, setData] = useState<ProfitOverview | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
@@ -44,7 +45,8 @@ export default function ProfitAnalyticsChart({ timeRange, hideNumbers = false }:
try {
setIsLoading(true);
setError(null);
const response = await getProfitOverview(timeRange);
// Use dateRange if provided, otherwise fall back to timeRange
const response = await getProfitOverview(dateRange || timeRange || '30');
setData(response);
} catch (error) {
console.error('Error fetching profit data:', error);
@@ -60,7 +62,7 @@ export default function ProfitAnalyticsChart({ timeRange, hideNumbers = false }:
};
fetchData();
}, [timeRange, toast]);
}, [timeRange, dateRange, toast]);
if (isLoading) {
return (
@@ -304,7 +306,10 @@ export default function ProfitAnalyticsChart({ timeRange, hideNumbers = false }:
Most Profitable Products
</CardTitle>
<CardDescription>
Products generating the highest total profit (last {timeRange} days)
{dateRange
? `Products generating the highest total profit (${new Date(dateRange.from).toLocaleDateString()} - ${new Date(dateRange.to).toLocaleDateString()})`
: `Products generating the highest total profit (last ${timeRange || '30'} days)`
}
</CardDescription>
</CardHeader>
<CardContent>