From 86c0665aed5079e3ca8c4181bbcea4563999452f Mon Sep 17 00:00:00 2001 From: rasmusq Date: Thu, 27 Nov 2025 21:12:16 +0100 Subject: [PATCH] refactor: abstract dashboard lists into components --- .../dashboard/WishlistSection.svelte | 157 +++++++ src/routes/dashboard/+page.svelte | 441 ++++++------------ 2 files changed, 287 insertions(+), 311 deletions(-) create mode 100644 src/lib/components/dashboard/WishlistSection.svelte diff --git a/src/lib/components/dashboard/WishlistSection.svelte b/src/lib/components/dashboard/WishlistSection.svelte new file mode 100644 index 0000000..396d3d4 --- /dev/null +++ b/src/lib/components/dashboard/WishlistSection.svelte @@ -0,0 +1,157 @@ + + +{#if shouldShow()} + + {#snippet headerAction()} +
+ {#if showCreateButton} + + {/if} + +
+ {/snippet} + + {#snippet searchBar()} + {#if items.length > 0} + + {/if} + {/snippet} + + {#snippet children(item)} + {@const wishlist = item.wishlist || item} + + {@render actions(item, unlocked)} + + {/snippet} +
+{/if} diff --git a/src/routes/dashboard/+page.svelte b/src/routes/dashboard/+page.svelte index a570b69..3764612 100644 --- a/src/routes/dashboard/+page.svelte +++ b/src/routes/dashboard/+page.svelte @@ -3,371 +3,190 @@ import type { PageData } from './$types'; import PageContainer from '$lib/components/layout/PageContainer.svelte'; import DashboardHeader from '$lib/components/layout/DashboardHeader.svelte'; - import WishlistGrid from '$lib/components/dashboard/WishlistGrid.svelte'; - import WishlistCard from '$lib/components/dashboard/WishlistCard.svelte'; + import WishlistSection from '$lib/components/dashboard/WishlistSection.svelte'; import { enhance } from '$app/forms'; import { Star } from 'lucide-svelte'; import { languageStore } from '$lib/stores/language.svelte'; - import SearchBar from '$lib/components/ui/SearchBar.svelte'; - import UnlockButton from '$lib/components/ui/UnlockButton.svelte'; let { data }: { data: PageData } = $props(); const t = $derived(languageStore.t); - let myWishlistsUnlocked = $state(false); - let claimedWishlistsUnlocked = $state(false); - let savedWishlistsUnlocked = $state(false); - let myWishlistsSearch = $state(''); - let claimedWishlistsSearch = $state(''); - let savedWishlistsSearch = $state(''); - - // Only owned wishlists for "My Wishlists" (exclude claimed) - const allMyWishlists = $derived(() => { - const owned = data.wishlists || []; - return owned; - }); + // Only owned wishlists for "My Wishlists" + const myWishlists = $derived(() => data.wishlists || []); // Claimed wishlists (those with ownerToken, meaning they were claimed via edit link) - const allClaimedWishlists = $derived(() => { - const claimed = (data.savedWishlists || []) - .filter(saved => saved.wishlist?.ownerToken) // Has edit access + const claimedWishlists = $derived(() => { + return (data.savedWishlists || []) + .filter(saved => saved.wishlist?.ownerToken) .map(saved => ({ ...saved.wishlist, isFavorite: saved.isFavorite, isClaimed: true, savedId: saved.id })); - return claimed; - }); - - const sortedWishlists = $derived(() => { - const filtered = myWishlistsSearch.trim() - ? allMyWishlists().filter(w => - w.title.toLowerCase().includes(myWishlistsSearch.toLowerCase()) || - w.description?.toLowerCase().includes(myWishlistsSearch.toLowerCase()) - ) - : allMyWishlists(); - - return [...filtered].sort((a, b) => { - if (a.isFavorite && !b.isFavorite) return -1; - if (!a.isFavorite && b.isFavorite) return 1; - - const aHasEndDate = !!a.endDate; - const bHasEndDate = !!b.endDate; - - if (aHasEndDate && !bHasEndDate) return -1; - if (!aHasEndDate && bHasEndDate) return 1; - - if (aHasEndDate && bHasEndDate) { - return new Date(a.endDate!).getTime() - new Date(b.endDate!).getTime(); - } - - return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); - }); - }); - - const sortedClaimedWishlists = $derived(() => { - const filtered = claimedWishlistsSearch.trim() - ? allClaimedWishlists().filter(w => - w.title.toLowerCase().includes(claimedWishlistsSearch.toLowerCase()) || - w.description?.toLowerCase().includes(claimedWishlistsSearch.toLowerCase()) - ) - : allClaimedWishlists(); - - return [...filtered].sort((a, b) => { - if (a.isFavorite && !b.isFavorite) return -1; - if (!a.isFavorite && b.isFavorite) return 1; - - const aHasEndDate = !!a.endDate; - const bHasEndDate = !!b.endDate; - - if (aHasEndDate && !bHasEndDate) return -1; - if (!aHasEndDate && bHasEndDate) return 1; - - if (aHasEndDate && bHasEndDate) { - return new Date(a.endDate!).getTime() - new Date(b.endDate!).getTime(); - } - - return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); - }); }); // Saved wishlists are those WITHOUT ownerToken (saved from public view only) - const sortedSavedWishlists = $derived(() => { - const filtered = savedWishlistsSearch.trim() - ? (data.savedWishlists || []) - .filter(saved => !saved.wishlist?.ownerToken) // No edit access - .filter(saved => - saved.wishlist?.title.toLowerCase().includes(savedWishlistsSearch.toLowerCase()) || - saved.wishlist?.description?.toLowerCase().includes(savedWishlistsSearch.toLowerCase()) - ) - : (data.savedWishlists || []).filter(saved => !saved.wishlist?.ownerToken); - - return [...filtered].sort((a, b) => { - if (a.isFavorite && !b.isFavorite) return -1; - if (!a.isFavorite && b.isFavorite) return 1; - - const aHasEndDate = !!a.wishlist?.endDate; - const bHasEndDate = !!b.wishlist?.endDate; - - if (aHasEndDate && !bHasEndDate) return -1; - if (!aHasEndDate && bHasEndDate) return 1; - - if (aHasEndDate && bHasEndDate) { - return new Date(a.wishlist.endDate!).getTime() - new Date(b.wishlist.endDate!).getTime(); - } - - return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); - }); + const savedWishlists = $derived(() => { + return (data.savedWishlists || []).filter(saved => !saved.wishlist?.ownerToken); }); - - function formatEndDate(date: Date | string | null): string | null { - if (!date) return null; - const d = new Date(date); - return d.toLocaleDateString(languageStore.t.date.format.short, { year: 'numeric', month: 'short', day: 'numeric' }); - } - - function getWishlistDescription(wishlist: any): string | null { - if (!wishlist) return null; - - const lines: string[] = []; - - const topItems = wishlist.items?.slice(0, 3).map((item: any) => item.title) || []; - if (topItems.length > 0) { - lines.push(topItems.join(', ')); - } - - if (wishlist.user?.name || wishlist.user?.username) { - const ownerName = wishlist.user.name || wishlist.user.username; - lines.push(`${t.dashboard.by} ${ownerName}`); - } - - if (wishlist.endDate) { - lines.push(`${t.dashboard.ends}: ${formatEndDate(wishlist.endDate)}`); - } - - return lines.length > 0 ? lines.join('\n') : null; - } - - function getSavedWishlistDescription(saved: any): string | null { - return getWishlistDescription(saved.wishlist); - } - + - {#snippet headerAction()} -
- - -
- {/snippet} - - {#snippet searchBar()} - {#if allMyWishlists().length > 0} - - {/if} - {/snippet} - - {#snippet children(wishlist)} - -
- -
{ + {#snippet actions(wishlist, unlocked)} +
+ { + return async ({ update }) => { + await update({ reset: false }); + }; + }}> + + + + + + + {#if unlocked} +
{ return async ({ update }) => { await update({ reset: false }); }; }}> - -
- - - {#if myWishlistsUnlocked} - -
{ - return async ({ update }) => { - await update({ reset: false }); - }; - }}> - - -
- {/if} -
- - {/snippet} - - - {#if allClaimedWishlists().length > 0} - - {#snippet headerAction()} -
- -
- {/snippet} - - {#snippet searchBar()} - {#if allClaimedWishlists().length > 0} - {/if} - {/snippet} +
+ {/snippet} +
- {#snippet children(wishlist)} - + + {#snippet actions(wishlist, unlocked)} +
+
{ + return async ({ update }) => { + await update({ reset: false }); + }; + }}> + + + +
+ - - + + {#if unlocked} +
{ + return async ({ update }) => { + await update({ reset: false }); + }; + }}> + + - - {#if claimedWishlistsUnlocked} - - { - return async ({ update }) => { - await update({ reset: false }); - }; - }}> - - -
- {/if} -
-
- {/snippet} -
- {/if} + + {/if} + + {/snippet} + - + - {#snippet headerAction()} -
- -
- {/snippet} - - {#snippet searchBar()} - {#if (data.savedWishlists || []).filter(saved => !saved.wishlist?.ownerToken).length > 0} - - {/if} - {/snippet} - - {#snippet children(saved)} - -
-
{ + {#snippet actions(saved, unlocked)} +
+ { + return async ({ update }) => { + await update({ reset: false }); + }; + }}> + + + + + + {#if unlocked} +
{ return async ({ update }) => { await update({ reset: false }); }; }}> - -
- - {#if savedWishlistsUnlocked} -
{ - return async ({ update }) => { - await update({ reset: false }); - }; - }}> - - -
- {/if} -
- + {/if} +
{/snippet} -
+