add: local wishlists stored in local storage for anonymous users
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Utility functions for managing anonymous user's wishlists in localStorage
|
||||
*/
|
||||
|
||||
const LOCAL_WISHLISTS_KEY = 'local_wishlists';
|
||||
|
||||
export interface LocalWishlist {
|
||||
ownerToken: string;
|
||||
publicToken: string;
|
||||
title: string;
|
||||
createdAt: string;
|
||||
isFavorite?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all local wishlists from localStorage
|
||||
*/
|
||||
export function getLocalWishlists(): LocalWishlist[] {
|
||||
if (typeof window === 'undefined') return [];
|
||||
|
||||
try {
|
||||
const stored = localStorage.getItem(LOCAL_WISHLISTS_KEY);
|
||||
return stored ? JSON.parse(stored) : [];
|
||||
} catch (error) {
|
||||
console.error('Failed to parse local wishlists:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a wishlist to localStorage
|
||||
*/
|
||||
export function addLocalWishlist(wishlist: LocalWishlist): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const wishlists = getLocalWishlists();
|
||||
|
||||
// Check if already exists
|
||||
const exists = wishlists.some(w => w.ownerToken === wishlist.ownerToken);
|
||||
if (exists) return;
|
||||
|
||||
wishlists.push(wishlist);
|
||||
localStorage.setItem(LOCAL_WISHLISTS_KEY, JSON.stringify(wishlists));
|
||||
} catch (error) {
|
||||
console.error('Failed to add local wishlist:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a wishlist from localStorage (forget it)
|
||||
*/
|
||||
export function forgetLocalWishlist(ownerToken: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const wishlists = getLocalWishlists();
|
||||
const filtered = wishlists.filter(w => w.ownerToken !== ownerToken);
|
||||
localStorage.setItem(LOCAL_WISHLISTS_KEY, JSON.stringify(filtered));
|
||||
} catch (error) {
|
||||
console.error('Failed to forget local wishlist:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all local wishlists (e.g., when user claims all wishlists)
|
||||
*/
|
||||
export function clearLocalWishlists(): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
localStorage.removeItem(LOCAL_WISHLISTS_KEY);
|
||||
} catch (error) {
|
||||
console.error('Failed to clear local wishlists:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a wishlist is in local storage
|
||||
*/
|
||||
export function isLocalWishlist(ownerToken: string): boolean {
|
||||
const wishlists = getLocalWishlists();
|
||||
return wishlists.some(w => w.ownerToken === ownerToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle favorite status for a local wishlist
|
||||
*/
|
||||
export function toggleLocalFavorite(ownerToken: string): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
const wishlists = getLocalWishlists();
|
||||
const updated = wishlists.map(w =>
|
||||
w.ownerToken === ownerToken
|
||||
? { ...w, isFavorite: !w.isFavorite }
|
||||
: w
|
||||
);
|
||||
localStorage.setItem(LOCAL_WISHLISTS_KEY, JSON.stringify(updated));
|
||||
} catch (error) {
|
||||
console.error('Failed to toggle local wishlist favorite:', error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user