Files

58 lines
1.0 KiB
JavaScript

import useSWR from 'swr';
import { getStats, getTrendStats, getCategoryStats } from '../lib/api';
export function useStats(month) {
const key = month ? ['stats', month] : null;
const { data, error, isLoading, mutate } = useSWR(
key,
() => getStats(month),
{
revalidateOnFocus: false,
}
);
return {
stats: data || { income: 0, expense: 0, balance: 0 },
isLoading,
error,
mutate,
};
}
export function useTrendStats(period = 'month', month) {
const key = ['trend', period, month];
const { data, error, isLoading } = useSWR(
key,
() => getTrendStats(period, month),
{
revalidateOnFocus: false,
}
);
return {
trend: data || [],
isLoading,
error,
};
}
export function useCategoryStats(month, type = 'all') {
const key = ['categoryStats', month, type];
const { data, error, isLoading } = useSWR(
key,
() => getCategoryStats(month, type),
{
revalidateOnFocus: false,
}
);
return {
categories: data || [],
isLoading,
error,
};
}