All files / src/features/statistics/hooks use-statistics.ts

93.75% Statements 15/16
83.33% Branches 5/6
100% Functions 7/7
100% Lines 15/15

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                4x   70x   1x 22x       4x 67x             4x 20x   20x     5x 5x             4x 11x 11x   11x                
import { useQuery } from '@tanstack/react-query';
import {
  fetchStatistics,
  calculateAverageScoreFromApi,
} from '../services/statistics.service';
import { QUERY_KEYS } from '@/constants/statistics';
 
// Query keys for React Query
export const statisticsKeys = {
  all: QUERY_KEYS.STATISTICS.ALL,
  lists: () => [...statisticsKeys.all, 'list'] as const,
  list: (filters: Record<string, any>) =>
    [...statisticsKeys.lists(), { filters }] as const,
  averageScore: () => [...statisticsKeys.all, 'averageScore'] as const,
};
 
// Hook to fetch statistics data
export const useStatistics = () => {
  return useQuery({
    queryKey: statisticsKeys.lists(),
    queryFn: fetchStatistics,
  });
};
 
// Hook to get calculated average score
export const useAverageScore = () => {
  const { data: statistics } = useStatistics();
 
  return useQuery({
    queryKey: statisticsKeys.averageScore(),
    queryFn: () => {
      Iif (!statistics) throw new Error('No statistics data available');
      return calculateAverageScoreFromApi(statistics);
    },
    enabled: !!statistics,
  });
};
 
// Combined hook for statistics operations (backward compatibility)
export const useStatisticsOperations = () => {
  const { data, isLoading, error, refetch } = useStatistics();
  const { data: averageScore } = useAverageScore();
 
  return {
    data: data || [],
    loading: isLoading,
    error: error?.message || null,
    refetch,
    averageScore,
  };
};