mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
faf3f3a8d1
* feat: replace with-nanostores with new example * docs: update README with docs call-out * chore: small formatting inconsistencies * nit: standardize to spaces :'( * nit: standardize to tabs! * refactor: use html "hidden" property * nit: beta.66 for sanity
31 lines
619 B
TypeScript
31 lines
619 B
TypeScript
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 }) {
|
|
const existingEntry = cartItems.get()[id];
|
|
if (existingEntry) {
|
|
cartItems.setKey(id, {
|
|
...existingEntry,
|
|
quantity: existingEntry.quantity + 1,
|
|
});
|
|
} else {
|
|
cartItems.setKey(id, {
|
|
id,
|
|
name,
|
|
imageSrc,
|
|
quantity: 1,
|
|
});
|
|
}
|
|
}
|