24 lines
712 B
TypeScript
24 lines
712 B
TypeScript
import type { CartRepository } from '@/domain/cart/repository';
|
|
import type { CartItem } from '@/domain/cart/entities';
|
|
import { STORAGE_KEYS } from '@/domain/shared/constants';
|
|
|
|
export class LocalStorageCartRepository implements CartRepository {
|
|
load(): CartItem[] {
|
|
if (typeof window === 'undefined') return [];
|
|
|
|
const raw = localStorage.getItem(STORAGE_KEYS.cart);
|
|
if (!raw) return [];
|
|
|
|
try {
|
|
const parsed = JSON.parse(raw) as CartItem[];
|
|
return Array.isArray(parsed) ? parsed : [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
save(items: CartItem[]): void {
|
|
if (typeof window === 'undefined') return;
|
|
localStorage.setItem(STORAGE_KEYS.cart, JSON.stringify(items));
|
|
}
|
|
} |