Fix previous period data
This commit is contained in:
@@ -523,53 +523,24 @@ const SalesChart = ({
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
// Get previous period params
|
||||
const prevPeriodParams = calculatePreviousPeriodDates(
|
||||
params.timeRange,
|
||||
params.startDate,
|
||||
params.endDate
|
||||
);
|
||||
// Fetch data
|
||||
const response = await axios.get('/api/klaviyo/events/stats/details', {
|
||||
params: {
|
||||
...params,
|
||||
metric: 'revenue',
|
||||
daily: true
|
||||
}
|
||||
});
|
||||
|
||||
// Fetch both current and previous period data
|
||||
const [currentResponse, prevResponse] = await Promise.all([
|
||||
axios.get('/api/klaviyo/events/stats/details', {
|
||||
params: {
|
||||
...params,
|
||||
metric: 'revenue',
|
||||
daily: true
|
||||
}
|
||||
}),
|
||||
axios.get('/api/klaviyo/events/stats/details', {
|
||||
params: {
|
||||
metric: 'revenue',
|
||||
daily: true,
|
||||
...(prevPeriodParams || {})
|
||||
}
|
||||
})
|
||||
]);
|
||||
|
||||
if (!currentResponse.data) {
|
||||
if (!response.data) {
|
||||
throw new Error('Invalid response format');
|
||||
}
|
||||
|
||||
// Process the data
|
||||
const currentStats = Array.isArray(currentResponse.data) ? currentResponse.data : currentResponse.data.stats || [];
|
||||
const prevStats = Array.isArray(prevResponse.data) ? prevResponse.data : (prevResponse.data?.stats || []);
|
||||
const currentStats = Array.isArray(response.data) ? response.data : response.data.stats || [];
|
||||
|
||||
// Map previous period data to current period dates
|
||||
const processedStats = currentStats.map((day, index) => {
|
||||
// Find the corresponding previous period day
|
||||
const prevDay = prevStats[index] || {};
|
||||
|
||||
return {
|
||||
...day,
|
||||
prevRevenue: Number(prevDay.revenue || 0),
|
||||
prevOrders: Number(prevDay.orders || 0),
|
||||
prevAvgOrderValue: Number(prevDay.averageOrderValue || (prevDay.orders > 0 ? prevDay.revenue / prevDay.orders : 0))
|
||||
};
|
||||
});
|
||||
|
||||
const processedData = processData(processedStats);
|
||||
// Process the data directly without remapping
|
||||
const processedData = processData(currentStats);
|
||||
const stats = calculateSummaryStats(processedData);
|
||||
|
||||
setData(processedData);
|
||||
|
||||
Reference in New Issue
Block a user