From 45de1a631b267638d8daa6593a35a40f750215cf Mon Sep 17 00:00:00 2001 From: pheralb Date: Wed, 24 Sep 2025 14:09:03 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F=20Refactor=20favorites=20?= =?UTF-8?q?store,=20improve=20add/remove=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/routes/favorites/+page.svelte | 18 ++++++------ src/stores/favorites.store.ts | 46 +++++++++++++++++-------------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/routes/favorites/+page.svelte b/src/routes/favorites/+page.svelte index cd5bf4c..94d3050 100644 --- a/src/routes/favorites/+page.svelte +++ b/src/routes/favorites/+page.svelte @@ -94,14 +94,16 @@ {/if}

Favorites

- - - {#if !searchTerm} - {favoritesCount} SVGs - {:else} -

- {filteredFavorites.length} - search results -

+ {#if favoritesCount > 0} + - + {#if !searchTerm} + {favoritesCount} SVGs + {:else} +

+ {filteredFavorites.length} + search results +

+ {/if} {/if} {#if favoritesCount > 0} diff --git a/src/stores/favorites.store.ts b/src/stores/favorites.store.ts index d5bdcba..55e0489 100644 --- a/src/stores/favorites.store.ts +++ b/src/stores/favorites.store.ts @@ -7,7 +7,6 @@ import { browser } from "$app/environment"; const localStorageKey = "svgl_favorites"; function createFavoritesStore() { - // Check if the favorites exist in the SVGs array: const validateFavorites = (favorites: iSVG[]): iSVG[] => { return favorites.filter((favorite) => { const existsInSvgs = svgs.some((svg) => { @@ -62,10 +61,10 @@ function createFavoritesStore() { // Add SVG to favorites: addToFavorites: (item: iSVG) => update((favorites) => { - const exists = favorites.some((fav) => - typeof item === "object" && item.id - ? fav.id === item.id - : fav === item, + const exists = favorites.some( + (fav) => + fav.title === item.title && + JSON.stringify(fav.route) === JSON.stringify(item.route), ); if (!exists) { const newFavorites = [...favorites, item]; @@ -78,10 +77,12 @@ function createFavoritesStore() { // Delete SVG from favorites: removeFromFavorites: (item: iSVG) => update((favorites) => { - const newFavorites = favorites.filter((fav) => - typeof item === "object" && item.id - ? fav.id !== item.id - : fav !== item, + const newFavorites = favorites.filter( + (fav) => + !( + fav.title === item.title && + JSON.stringify(fav.route) === JSON.stringify(item.route) + ), ); saveFavorites(newFavorites); return newFavorites; @@ -90,18 +91,20 @@ function createFavoritesStore() { // Toggle (add/remove) SVG from favorites: toggleFavorite: (item: iSVG) => update((favorites) => { - const exists = favorites.some((fav) => - typeof item === "object" && item.id - ? fav.id === item.id - : fav === item, + const exists = favorites.some( + (fav) => + fav.title === item.title && + JSON.stringify(fav.route) === JSON.stringify(item.route), ); let newFavorites; if (exists) { - newFavorites = favorites.filter((fav) => - typeof item === "object" && item.id - ? fav.id !== item.id - : fav !== item, + newFavorites = favorites.filter( + (fav) => + !( + fav.title === item.title && + JSON.stringify(fav.route) === JSON.stringify(item.route) + ), ); } else { newFavorites = [...favorites, item]; @@ -113,18 +116,19 @@ function createFavoritesStore() { // Check if SVG is in favorites: isFavorite: (item: iSVG, currentFavorites: iSVG[]) => { - return currentFavorites.some((fav) => - typeof item === "object" && item.id ? fav.id === item.id : fav === item, + return currentFavorites.some( + (fav) => + fav.title === item.title && + JSON.stringify(fav.route) === JSON.stringify(item.route), ); }, - // Delete all favorites: + // Clear favorites: clearFavorites: () => { set([]); saveFavorites([]); }, - // Get count of favorites: getCount: (currentFavorites: iSVG[]) => currentFavorites.length, validateAndCleanup: () => {