fix: favorites change event for sidebar refresh

This commit is contained in:
Julien Froidefond
2025-02-13 22:43:34 +01:00
parent f7892f5b3f
commit e3ad1690c5
2 changed files with 23 additions and 5 deletions

View File

@@ -70,16 +70,25 @@ export function Sidebar({ isOpen }: SidebarProps) {
fetchFavorites(); fetchFavorites();
}, []); // Suppression de la dépendance pathname }, []); // Suppression de la dépendance pathname
// Mettre à jour les favoris quand ils changent dans le localStorage // Mettre à jour les favoris quand ils changent
useEffect(() => { useEffect(() => {
const handleStorageChange = (e: StorageEvent) => { const handleFavoritesChange = () => {
fetchFavorites();
};
// Écouter les changements de favoris dans la même fenêtre
window.addEventListener("favoritesChanged", handleFavoritesChange);
// Écouter les changements de favoris dans d'autres fenêtres
window.addEventListener("storage", (e) => {
if (e.key === "stripstream_favorites") { if (e.key === "stripstream_favorites") {
fetchFavorites(); fetchFavorites();
} }
}; });
window.addEventListener("storage", handleStorageChange); return () => {
return () => window.removeEventListener("storage", handleStorageChange); window.removeEventListener("favoritesChanged", handleFavoritesChange);
window.removeEventListener("storage", handleFavoritesChange);
};
}, [fetchFavorites]); }, [fetchFavorites]);
const handleRefresh = async () => { const handleRefresh = async () => {

View File

@@ -1,6 +1,13 @@
import { storageService } from "./storage.service"; import { storageService } from "./storage.service";
export class FavoriteService { export class FavoriteService {
private static readonly FAVORITES_CHANGE_EVENT = "favoritesChanged";
private static dispatchFavoritesChanged() {
// Dispatch l'événement pour notifier les changements
window.dispatchEvent(new Event(FavoriteService.FAVORITES_CHANGE_EVENT));
}
/** /**
* Vérifie si une série est dans les favoris * Vérifie si une série est dans les favoris
*/ */
@@ -13,6 +20,7 @@ export class FavoriteService {
*/ */
static addToFavorites(seriesId: string): void { static addToFavorites(seriesId: string): void {
storageService.addFavorite(seriesId); storageService.addFavorite(seriesId);
this.dispatchFavoritesChanged();
} }
/** /**
@@ -20,6 +28,7 @@ export class FavoriteService {
*/ */
static removeFromFavorites(seriesId: string): void { static removeFromFavorites(seriesId: string): void {
storageService.removeFavorite(seriesId); storageService.removeFavorite(seriesId);
this.dispatchFavoritesChanged();
} }
/** /**