fix statistics; i need to make unit tests

This commit is contained in:
Wavering Ana 2025-01-29 21:57:59 -05:00
parent 67e94b4483
commit c2d5f5a644
4 changed files with 47 additions and 41 deletions

View file

@ -63,15 +63,26 @@ export const deleteLink = async (id: number) => {
};
export const getLinkClickStats = async (id: number) => {
const response = await api.get<ClickStats[]>(`/links/${id}/clicks`);
return response.data;
try {
const response = await api.get<ClickStats[]>(`/links/${id}/clicks`);
return response.data;
} catch (error) {
console.error('Error fetching click stats:', error);
throw error;
}
};
export const getLinkSourceStats = async (id: number) => {
const response = await api.get<SourceStats[]>(`/links/${id}/sources`);
return response.data;
try {
const response = await api.get<SourceStats[]>(`/links/${id}/sources`);
return response.data;
} catch (error) {
console.error('Error fetching source stats:', error);
throw error;
}
};
export const checkFirstUser = async () => {
const response = await api.get<{ isFirstUser: boolean }>('/auth/check-first-user');
return response.data.isFirstUser;

View file

@ -9,6 +9,7 @@ import {
ResponsiveContainer,
} from "recharts";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { toast } from "@/hooks/use-toast"
import { useState, useEffect } from "react";
import { getLinkClickStats, getLinkSourceStats } from '../api/client';
@ -36,13 +37,18 @@ export function StatisticsModal({ isOpen, onClose, linkId }: StatisticsModalProp
]);
setClicksOverTime(clicksData);
setSourcesData(sourcesData);
} catch (error) {
} catch (error: any) {
console.error("Failed to fetch statistics:", error);
toast({
variant: "destructive",
title: "Error",
description: error.response?.data || "Failed to load statistics",
});
} finally {
setLoading(false);
}
};
fetchData();
}
}, [isOpen, linkId]);