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:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user