add: local wishlists stored in local storage for anonymous users
This commit is contained in:
+16
-4
@@ -10,6 +10,7 @@
|
||||
import ColorPicker from '$lib/components/ui/ColorPicker.svelte';
|
||||
import type { PageData } from './$types';
|
||||
import { languageStore } from '$lib/stores/language.svelte';
|
||||
import { addLocalWishlist } from '$lib/utils/localWishlists';
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
|
||||
@@ -32,7 +33,19 @@
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const { ownerToken } = await response.json();
|
||||
const result = await response.json();
|
||||
const { ownerToken, publicToken, title: wishlistTitle, createdAt } = result;
|
||||
|
||||
// If user is not authenticated, save to localStorage
|
||||
if (!data.session?.user) {
|
||||
addLocalWishlist({
|
||||
ownerToken,
|
||||
publicToken,
|
||||
title: wishlistTitle,
|
||||
createdAt
|
||||
});
|
||||
}
|
||||
|
||||
goto(`/wishlist/${ownerToken}/edit`);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -56,9 +69,8 @@
|
||||
<div class="flex items-center gap-1 sm:gap-2 flex-shrink-0">
|
||||
<LanguageToggle />
|
||||
<ThemeToggle />
|
||||
{#if data.session?.user}
|
||||
<Button variant="outline" onclick={() => goto('/dashboard')}>{t.nav.dashboard}</Button>
|
||||
{:else}
|
||||
<Button variant="outline" onclick={() => goto('/dashboard')}>{t.nav.dashboard}</Button>
|
||||
{#if !data.session?.user}
|
||||
<Button variant="outline" onclick={() => goto('/signin')}>{t.auth.signIn}</Button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
@@ -29,5 +29,11 @@ export const POST: RequestHandler = async ({ request, locals }) => {
|
||||
})
|
||||
.returning();
|
||||
|
||||
return json({ ownerToken, publicToken, id: wishlist.id });
|
||||
return json({
|
||||
ownerToken,
|
||||
publicToken,
|
||||
id: wishlist.id,
|
||||
title: wishlist.title,
|
||||
createdAt: wishlist.createdAt
|
||||
});
|
||||
};
|
||||
|
||||
@@ -7,8 +7,14 @@ import { eq, and } from 'drizzle-orm';
|
||||
export const load: PageServerLoad = async (event) => {
|
||||
const session = await event.locals.auth();
|
||||
|
||||
// Allow anonymous users to access dashboard for local wishlists
|
||||
if (!session?.user?.id) {
|
||||
throw redirect(303, '/signin');
|
||||
return {
|
||||
user: null,
|
||||
wishlists: [],
|
||||
savedWishlists: [],
|
||||
isAuthenticated: false
|
||||
};
|
||||
}
|
||||
|
||||
const userWishlists = await db.query.wishlists.findMany({
|
||||
@@ -53,7 +59,8 @@ export const load: PageServerLoad = async (event) => {
|
||||
return {
|
||||
user: session.user,
|
||||
wishlists: userWishlists,
|
||||
savedWishlists: savedWithAccess
|
||||
savedWishlists: savedWithAccess,
|
||||
isAuthenticated: true
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
import PageContainer from '$lib/components/layout/PageContainer.svelte';
|
||||
import DashboardHeader from '$lib/components/layout/DashboardHeader.svelte';
|
||||
import WishlistSection from '$lib/components/dashboard/WishlistSection.svelte';
|
||||
import LocalWishlistsSection from '$lib/components/dashboard/LocalWishlistsSection.svelte';
|
||||
import { enhance } from '$app/forms';
|
||||
import { Star } from 'lucide-svelte';
|
||||
import { languageStore } from '$lib/stores/language.svelte';
|
||||
@@ -36,8 +37,12 @@
|
||||
<PageContainer>
|
||||
<DashboardHeader userName={data.user?.name} userEmail={data.user?.email} />
|
||||
|
||||
<!-- My Wishlists Section -->
|
||||
<WishlistSection
|
||||
<!-- Local Wishlists Section (for anonymous and authenticated users) -->
|
||||
<LocalWishlistsSection isAuthenticated={data.isAuthenticated} />
|
||||
|
||||
{#if data.isAuthenticated}
|
||||
<!-- My Wishlists Section -->
|
||||
<WishlistSection
|
||||
title={t.dashboard.myWishlists}
|
||||
description={t.dashboard.myWishlistsDescription}
|
||||
items={myWishlists()}
|
||||
@@ -189,4 +194,5 @@
|
||||
</div>
|
||||
{/snippet}
|
||||
</WishlistSection>
|
||||
{/if}
|
||||
</PageContainer>
|
||||
|
||||
@@ -130,6 +130,7 @@
|
||||
isAuthenticated={data.isAuthenticated}
|
||||
isOwner={data.isOwner}
|
||||
hasClaimed={data.hasClaimed}
|
||||
ownerToken={data.wishlist.ownerToken}
|
||||
/>
|
||||
|
||||
<WishlistActionButtons
|
||||
|
||||
Reference in New Issue
Block a user