2022-07-11 14:42:10 -05:00
|
|
|
import { atom, map } from 'nanostores';
|
|
|
|
|
|
|
|
export const isCartOpen = atom(false);
|
|
|
|
|
|
|
|
export type CartItem = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
imageSrc: string;
|
|
|
|
quantity: number;
|
2022-07-11 14:44:42 -05:00
|
|
|
};
|
2022-07-11 14:42:10 -05:00
|
|
|
|
|
|
|
export type CartItemDisplayInfo = Pick<CartItem, 'id' | 'name' | 'imageSrc'>;
|
|
|
|
|
|
|
|
export const cartItems = map<Record<string, CartItem>>({});
|
|
|
|
|
|
|
|
export function addCartItem({ id, name, imageSrc }) {
|
|
|
|
const existingEntry = cartItems.get()[id];
|
|
|
|
if (existingEntry) {
|
|
|
|
cartItems.setKey(id, {
|
|
|
|
...existingEntry,
|
|
|
|
quantity: existingEntry.quantity + 1,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cartItems.setKey(id, {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
imageSrc,
|
|
|
|
quantity: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|