feat(*): Add basket functionality

This commit is contained in:
Josh Creek
2024-05-05 12:32:37 +01:00
parent d7e24928fc
commit a46ed76fb9
5 changed files with 311 additions and 28 deletions
+41
View File
@@ -0,0 +1,41 @@
import { writable } from 'svelte/store';
const loadInitialState = () => {
// Check if localStorage is available (i.e., we're on the client side)
if (typeof localStorage !== 'undefined') {
const storedData = localStorage.getItem('basket');
return storedData ? JSON.parse(storedData) : getDefaultState();
} else {
return getDefaultState();
}
};
const getDefaultState = () => ({
items: [],
subtotal: 0
});
export const basket = writable(loadInitialState());
// Subscribe to changes in the store and update localStorage accordingly
if (typeof localStorage !== 'undefined') {
basket.subscribe((value) => {
localStorage.setItem('basket', JSON.stringify(value));
});
}
export type Basket = {
items: Item[];
subtotal: number;
};
export type Item = {
id: number;
categoryDescription: string;
name: string;
imgAlt: string;
imgSrc: string;
price: number;
priceId: string;
quantity: number;
};