diff options
Diffstat (limited to 'src/cartStore.ts')
-rw-r--r-- | src/cartStore.ts | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/cartStore.ts b/src/cartStore.ts new file mode 100644 index 000000000..a57a6ce87 --- /dev/null +++ b/src/cartStore.ts @@ -0,0 +1,31 @@ +import { atom, map } from 'nanostores'; + +export const isCartOpen = atom(false); + +export type CartItem = { + id: string; + name: string; + imageSrc: string; + quantity: number; +}; + +export type CartItemDisplayInfo = Pick<CartItem, 'id' | 'name' | 'imageSrc'>; + +export const cartItems = map<Record<string, CartItem>>({}); + +export function addCartItem({ id, name, imageSrc }: CartItemDisplayInfo) { + const existingEntry = cartItems.get()[id]; + if (existingEntry) { + cartItems.setKey(id, { + ...existingEntry, + quantity: existingEntry.quantity + 1, + }); + } else { + cartItems.setKey(id, { + id, + name, + imageSrc, + quantity: 1, + }); + } +} |