type Product = { id: number; title: string; price: number; description: string;…
- User
type Product = { id: number; title: string; price: number; description: string; category: string; image: string; rating: { rate: number; count: number; }; };
type CartProduct = Product & { quantity: number; };
type CartState = { cartItems: CartProduct[]; addItemToCart: (item: Product) => void; increaseQuantity: (productId: number) => void; decreaseQuantity: (productId: number) => void; };
const useCartStore = create((set, get) => ({ cartItems: [],
addItemToCart: (item) => { const itemExists = get().cartItems.find( (cartItem) => cartItem.id === item.id );
if (itemExists) { if (typeof itemExists.quantity === "number") { itemExists.quantity++; } set({ cartItems: [...get().cartItems] }); } else { set({ cartItems: [...get().cartItems, { ...item, quantity: 1 }] }); }},
increaseQuantity: (productId) => { const itemExists = get().cartItems.find( (cartItem) => cartItem.id === productId );
if (itemExists) { if (typeof itemExists.quantity === "number") { itemExists.quantity++; } set({ cartItems: [...get().cartItems] }); }}, decreaseQuantity: (productId) => { const itemExists = get().cartItems.find( (cartItem) => cartItem.id === productId );
if (itemExists) { if (typeof itemExists.quantity === "number") { if (itemExists.quantity === 1) { const updatedCartItems = get().cartItems.filter( (item) => item.id !== productId ); set({ cartItems: updatedCartItems }); } else { itemExists.quantity--; set({ cartItems: [...get().cartItems] }); } } }}, }));
export default useCartStore;
"add this totalItems = 0 This returns the totaly quantity of items in the cart as an integer."
- Assistant
- User
not working
- Assistant
- User
now add this cartTotal = 0 This returns the total value of all items in the cart.
- Assistant
- User
add this emptyCart() The emptyCart() method should be used to remove all cart items, and resetting cart totals to the default 0 values.
- Assistant
- User
removeItem(itemId) The removeItem method should be used to remove an item from the cart.
- Assistant
- User
totalUniqueItems = 0 This returns the total unique items in the cart as an integer.
isEmpty = false A quick and easy way to check if the cart is empty. Returned as a boolean.
updateItem(itemId, data) The updateItem method should be used to update items in the cart.
- Assistant
- User
use persist
import { persist, createJSONStorage } from "zustand/middleware";
- Assistant
- User
updateItemQuantity(itemId, quantity) The updateItemQuantity method should be used to update an items quantity value.
- Assistant
- User
inCart(itemId) Quickly check if an item is in the cart. Returned as a boolean. getItem(itemId) Get a specific cart item by id. Returns the item object.
- Assistant
- User
setItems(items) The setItems method should be used to set all items in the cart. This will overwrite any existing cart items. A quantity default of 1 will be set for an item implicitly if no quantity is specified.
- Assistant
- User
Argument of type '{ quantity: number; id?: number | undefined; title?: string | undefined; price?: number | undefined; description?: string | undefined; category?: string | undefined; image?: string | undefined; rating?: { ...; } | undefined; }[]' is not assignable to parameter of type 'CartProduct[]'. Type '{ quantity: number; id?: number | undefined; title?: string | undefined; price?: number | undefined; description?: string | undefined; category?: string | undefined; image?: string | undefined; rating?: { ...; } | undefined; }' is not assignable to type 'CartProduct'. Type '{ quantity: number; id?: number | undefined; title?: string | undefined; price?: number | undefined; description?: string | undefined; category?: string | undefined; image?: string | undefined; rating?: { ...; } | undefined; }' is not assignable to type 'Product'. Types of property 'id' are incompatible. Type 'number | undefined' is not assignable to type 'number'. Type 'undefined' is not assignable to type 'number'.ts(2345)
- Assistant
- User
thanks all work done
- Assistant
You're welcome! I'm glad to hear that everything is working well. If you have any more questions or need further assistance in the future, feel free to reach out. Happy coding! 🚀