refacto: error on favorites
This commit is contained in:
@@ -7,6 +7,8 @@ import { Button } from "../ui/button";
|
||||
import { useToast } from "@/components/ui/use-toast";
|
||||
import { Cover } from "@/components/ui/cover";
|
||||
import { RefreshButton } from "@/components/library/RefreshButton";
|
||||
import { AppError } from "@/utils/errors";
|
||||
import { ERROR_CODES } from "@/constants/errorCodes";
|
||||
|
||||
interface SeriesHeaderProps {
|
||||
series: KomgaSeries;
|
||||
@@ -18,21 +20,26 @@ export const SeriesHeader = ({ series, refreshSeries }: SeriesHeaderProps) => {
|
||||
const [isFavorite, setIsFavorite] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Vérifier si la série est dans les favoris
|
||||
const checkFavorite = async () => {
|
||||
try {
|
||||
const response = await fetch("/api/komga/favorites");
|
||||
if (response.ok) {
|
||||
const favoriteIds = await response.json();
|
||||
setIsFavorite(favoriteIds.includes(series.id));
|
||||
if (!response.ok) {
|
||||
throw new AppError(ERROR_CODES.FAVORITE.STATUS_CHECK_ERROR);
|
||||
}
|
||||
const favoriteIds = await response.json();
|
||||
setIsFavorite(favoriteIds.includes(series.id));
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la vérification des favoris:", error);
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: error instanceof AppError ? error.message : ERROR_MESSAGES[ERROR_CODES.FAVORITE.NETWORK_ERROR],
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
checkFavorite();
|
||||
}, [series.id]);
|
||||
}, [series.id, toast]);
|
||||
|
||||
const handleToggleFavorite = async () => {
|
||||
try {
|
||||
@@ -46,20 +53,25 @@ export const SeriesHeader = ({ series, refreshSeries }: SeriesHeaderProps) => {
|
||||
|
||||
if (response.ok) {
|
||||
setIsFavorite(!isFavorite);
|
||||
// Déclencher l'événement pour mettre à jour la sidebar
|
||||
window.dispatchEvent(new Event("favoritesChanged"));
|
||||
toast({
|
||||
title: !isFavorite ? "Ajouté aux favoris" : "Retiré des favoris",
|
||||
description: series.metadata.title,
|
||||
});
|
||||
} else if (response.status === 500) {
|
||||
throw new AppError(ERROR_CODES.FAVORITE.SERVER_ERROR);
|
||||
} else if (response.status === 404) {
|
||||
throw new AppError(ERROR_CODES.FAVORITE.UPDATE_ERROR);
|
||||
} else {
|
||||
throw new Error("Erreur lors de la modification des favoris");
|
||||
throw new AppError(
|
||||
isFavorite ? ERROR_CODES.FAVORITE.DELETE_ERROR : ERROR_CODES.FAVORITE.ADD_ERROR
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Erreur lors de la modification des favoris:", error);
|
||||
toast({
|
||||
title: "Erreur",
|
||||
description: "Impossible de modifier les favoris",
|
||||
description: error instanceof AppError ? error.message : ERROR_MESSAGES[ERROR_CODES.FAVORITE.NETWORK_ERROR],
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -47,6 +47,9 @@ export const ERROR_CODES = {
|
||||
DELETE_ERROR: "FAVORITE_DELETE_ERROR",
|
||||
FETCH_ERROR: "FAVORITE_FETCH_ERROR",
|
||||
UPDATE_ERROR: "FAVORITE_UPDATE_ERROR",
|
||||
NETWORK_ERROR: "FAVORITE_NETWORK_ERROR",
|
||||
SERVER_ERROR: "FAVORITE_SERVER_ERROR",
|
||||
STATUS_CHECK_ERROR: "FAVORITE_STATUS_CHECK_ERROR",
|
||||
},
|
||||
PREFERENCES: {
|
||||
FETCH_ERROR: "PREFERENCES_FETCH_ERROR",
|
||||
@@ -82,6 +85,11 @@ export const ERROR_CODES = {
|
||||
SAVE_ERROR: "DEBUG_SAVE_ERROR",
|
||||
CLEAR_ERROR: "DEBUG_CLEAR_ERROR",
|
||||
},
|
||||
CLIENT: {
|
||||
FETCH_ERROR: "CLIENT_FETCH_ERROR",
|
||||
NETWORK_ERROR: "CLIENT_NETWORK_ERROR",
|
||||
REQUEST_FAILED: "CLIENT_REQUEST_FAILED",
|
||||
},
|
||||
} as const;
|
||||
|
||||
type Values<T> = T[keyof T];
|
||||
|
||||
@@ -44,10 +44,13 @@ export const ERROR_MESSAGES: Record<string, string> = {
|
||||
[ERROR_CODES.BOOK.DOWNLOAD_CANCELLED]: "❌ Téléchargement annulé",
|
||||
|
||||
// Favorite
|
||||
[ERROR_CODES.FAVORITE.ADD_ERROR]: "⭐️ Erreur lors de l'ajout aux favoris",
|
||||
[ERROR_CODES.FAVORITE.DELETE_ERROR]: "🗑️ Erreur lors de la suppression des favoris",
|
||||
[ERROR_CODES.FAVORITE.FETCH_ERROR]: "⭐️ Erreur lors de la récupération des favoris",
|
||||
[ERROR_CODES.FAVORITE.UPDATE_ERROR]: "📝 Erreur lors de la modification des favoris",
|
||||
[ERROR_CODES.FAVORITE.ADD_ERROR]: "⭐️ Impossible d'ajouter la série aux favoris",
|
||||
[ERROR_CODES.FAVORITE.DELETE_ERROR]: "🗑️ Impossible de retirer la série des favoris",
|
||||
[ERROR_CODES.FAVORITE.FETCH_ERROR]: "⭐️ Impossible de récupérer la liste des favoris",
|
||||
[ERROR_CODES.FAVORITE.UPDATE_ERROR]: "📝 Impossible de mettre à jour les favoris",
|
||||
[ERROR_CODES.FAVORITE.NETWORK_ERROR]: "📡 Erreur réseau lors de l'accès aux favoris",
|
||||
[ERROR_CODES.FAVORITE.SERVER_ERROR]: "🔧 Le serveur a rencontré une erreur lors du traitement des favoris",
|
||||
[ERROR_CODES.FAVORITE.STATUS_CHECK_ERROR]: "❓ Impossible de vérifier le statut des favoris",
|
||||
|
||||
// Preferences
|
||||
[ERROR_CODES.PREFERENCES.FETCH_ERROR]: "⚙️ Erreur lors de la récupération des préférences",
|
||||
@@ -85,4 +88,9 @@ export const ERROR_MESSAGES: Record<string, string> = {
|
||||
[ERROR_CODES.DEBUG.FETCH_ERROR]: "🔍 Erreur lors de la récupération des logs",
|
||||
[ERROR_CODES.DEBUG.SAVE_ERROR]: "💾 Erreur lors de l'enregistrement du log",
|
||||
[ERROR_CODES.DEBUG.CLEAR_ERROR]: "🧹 Erreur lors de la suppression des logs",
|
||||
|
||||
// Client
|
||||
[ERROR_CODES.CLIENT.FETCH_ERROR]: "🌐 Erreur lors de la requête",
|
||||
[ERROR_CODES.CLIENT.NETWORK_ERROR]: "📡 Erreur de connexion réseau",
|
||||
[ERROR_CODES.CLIENT.REQUEST_FAILED]: "❌ La requête a échoué",
|
||||
} as const;
|
||||
|
||||
Reference in New Issue
Block a user