Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 5x 8x 13x 5x 26x 26x 8x 4x 4x 5x 10x 1x 26x 9x 9x | import type { ChartDataPoint, AverageScore } from '../utils/chart-data';
import { apiClient } from '@/lib/http-client';
import { STATISTICS_API, AVERAGE_SCORE } from '@/constants/statistics';
// Statistics API response type from Strapi
interface StatisticApiResponse {
id: number;
documentId: string;
company: string;
value: number;
averageScore: AverageScore;
category: string;
date: string;
createdAt: string;
updatedAt: string;
publishedAt: string;
locale: string;
}
// Transform API response to ChartDataPoint
const transformApiResponse = (
apiData: StatisticApiResponse[]
): ChartDataPoint[] => {
console.log('Transforming Statistics API response:', apiData);
return apiData.map(item => ({
company: item.company,
value: item.value,
averageScore: item.averageScore,
}));
};
// Fetch statistics from backend API (public access)
export const fetchStatistics = async (): Promise<ChartDataPoint[]> => {
try {
const data: StatisticApiResponse[] = await apiClient.get(
STATISTICS_API.ENDPOINT
);
return transformApiResponse(data);
} catch (error) {
console.error('Failed to fetch statistics:', error);
throw new Error(
error instanceof Error ? error.message : STATISTICS_API.ERROR_MESSAGE
);
}
};
// Calculate average score from fetched data
export const calculateAverageScoreFromApi = (
data: ChartDataPoint[]
): AverageScore => {
if (data.length === 0) {
return { current: 0, total: 0 };
}
const total = data.reduce((acc, item) => acc + item.averageScore.current, 0);
const maxPossible = data.length * AVERAGE_SCORE.MAX_SCORE_PER_ITEM; // Assuming max score is 100 per item
return {
current: Math.round(total),
total: maxPossible,
};
};
|