fix: missing theme on local wishlists in dashboard page

This commit is contained in:
rasmusq
2025-11-28 13:03:13 +01:00
parent eb7ccdf7a2
commit d165e5992a
4 changed files with 115 additions and 19 deletions

View File

@@ -15,34 +15,87 @@
const t = $derived(languageStore.t); const t = $derived(languageStore.t);
let localWishlists = $state<LocalWishlist[]>([]); let localWishlists = $state<LocalWishlist[]>([]);
let enrichedWishlists = $state<any[]>([]);
// Load local wishlists on mount (client-side only) // Load local wishlists on mount and fetch their data from server
onMount(() => { onMount(async () => {
localWishlists = getLocalWishlists(); localWishlists = getLocalWishlists();
// Fetch full wishlist data for each local wishlist
const promises = localWishlists.map(async (local) => {
try {
const response = await fetch(`/api/wishlist/${local.ownerToken}`);
if (response.ok) {
const data = await response.json();
return {
...data,
isFavorite: local.isFavorite || false
};
}
} catch (error) {
console.error('Failed to fetch wishlist data:', error);
}
// Fallback to local data if fetch fails
return {
id: local.ownerToken,
title: local.title,
ownerToken: local.ownerToken,
publicToken: local.publicToken,
createdAt: local.createdAt,
isFavorite: local.isFavorite || false,
items: [],
theme: null,
color: null
};
}); });
function handleForget(ownerToken: string) { enrichedWishlists = await Promise.all(promises);
});
async function refreshEnrichedWishlists() {
const promises = localWishlists.map(async (local) => {
try {
const response = await fetch(`/api/wishlist/${local.ownerToken}`);
if (response.ok) {
const data = await response.json();
return {
...data,
isFavorite: local.isFavorite || false
};
}
} catch (error) {
console.error('Failed to fetch wishlist data:', error);
}
return {
id: local.ownerToken,
title: local.title,
ownerToken: local.ownerToken,
publicToken: local.publicToken,
createdAt: local.createdAt,
isFavorite: local.isFavorite || false,
items: [],
theme: null,
color: null
};
});
enrichedWishlists = await Promise.all(promises);
}
async function handleForget(ownerToken: string) {
forgetLocalWishlist(ownerToken); forgetLocalWishlist(ownerToken);
localWishlists = getLocalWishlists(); localWishlists = getLocalWishlists();
await refreshEnrichedWishlists();
} }
function handleToggleFavorite(ownerToken: string) { async function handleToggleFavorite(ownerToken: string) {
toggleLocalFavorite(ownerToken); toggleLocalFavorite(ownerToken);
localWishlists = getLocalWishlists(); localWishlists = getLocalWishlists();
await refreshEnrichedWishlists();
} }
// Transform LocalWishlist to match the format expected by WishlistSection // Use enriched wishlists which have full data including theme and color
const transformedWishlists = $derived(() => { const transformedWishlists = $derived(() => enrichedWishlists);
return localWishlists.map(w => ({
id: w.ownerToken,
title: w.title,
ownerToken: w.ownerToken,
publicToken: w.publicToken,
createdAt: w.createdAt,
isFavorite: w.isFavorite || false,
items: [] // We don't have item data in localStorage
}));
});
// Description depends on authentication status // Description depends on authentication status
const sectionDescription = $derived(() => { const sectionDescription = $derived(() => {

View File

@@ -44,8 +44,13 @@ export function getTheme(themeName?: string | null): Theme {
/** /**
* Get color from a CSS color string or class * Get color from a CSS color string or class
* For now, we'll use currentColor which inherits from the parent * Returns the provided color or a default primary color
*/ */
export function getThemeColor(color?: string | null): string { export function getThemeColor(color?: string | null): string {
return color || 'currentColor'; // Use the provided color, or default to a visible color
if (color && color !== 'null' && color !== '') {
return color;
}
// Default to a blue color (can't use CSS variables in SVG fill)
return '#3b82f6';
} }

View File

@@ -0,0 +1,38 @@
import { json } from '@sveltejs/kit';
import type { RequestHandler } from './$types';
import { db } from '$lib/server/db';
import { wishlists } from '$lib/server/schema';
import { eq, or } from 'drizzle-orm';
export const GET: RequestHandler = async ({ params }) => {
const { token } = params;
// Find wishlist by either ownerToken or publicToken
const wishlist = await db.query.wishlists.findFirst({
where: or(
eq(wishlists.ownerToken, token),
eq(wishlists.publicToken, token)
),
with: {
items: {
orderBy: (items, { asc }) => [asc(items.order)]
}
}
});
if (!wishlist) {
return json({ error: 'Wishlist not found' }, { status: 404 });
}
// Return only the necessary fields
return json({
id: wishlist.id,
title: wishlist.title,
ownerToken: wishlist.ownerToken,
publicToken: wishlist.publicToken,
createdAt: wishlist.createdAt,
theme: wishlist.theme,
color: wishlist.color,
items: wishlist.items || []
});
};

View File

@@ -58,7 +58,7 @@
}); });
</script> </script>
<PageContainer theme={currentTheme} themeColor="#3b82f6"> <PageContainer theme={currentTheme} themeColor={null}>
<DashboardHeader <DashboardHeader
userName={data.user?.name} userName={data.user?.name}
userEmail={data.user?.email} userEmail={data.user?.email}