Skip to content
Snippets Groups Projects
Commit b20824e9 authored by Princelle Maxime's avatar Princelle Maxime :gay_pride_flag:
Browse files

:bug: fix CartActions

parent b6327a6a
Branches
1 merge request!6Finalisation du panier
import { useContext } from 'react';
import { Dispatch } from 'react';
import { Product, ActionType } from './CartTypes';
import { Product } from './CartTypes';
import { cartContext } from './CartStore';
const { cartState, dispatch } = useContext(cartContext);
export function addToCart(product: Product): Product[] {
let cart = cartState.cart;
export function addToCart(cart: Product[], dispatch: Dispatch<ActionType>, product: Product): Product[] {
let newCart: Product[] = cart;
// Check if product already exists in cart, then update quantity
const existingProduct = cart.find(p => p.name === product.name);
const existingProduct = newCart.find(p => p.name === product.name);
if (existingProduct) {
existingProduct.quantity = existingProduct.quantity ? existingProduct.quantity + 1 : 1;
return setCart(cart);
return setCart(newCart, dispatch);
}
// If not, add product to cart
return setCart([...cart, { ...product, quantity: 1 }]);
return setCart([...cart, { ...product, quantity: 1 }], dispatch);
}
export function removeFromCart(product: Product): Product[] {
let cart = cartState.cart;
export function removeFromCart(cart: Product[], dispatch: Dispatch<ActionType>, product: Product): Product[] {
let newCart: Product[] = cart;
// Check if product already exists in cart, then update quantity
const existingProduct = cart.find(p => p.name === product.name);
const existingProduct = newCart.find(p => p.name === product.name);
if (existingProduct) {
existingProduct.quantity = existingProduct.quantity ? existingProduct.quantity - 1 : 0;
// If quantity is 0, remove product from cart
if (existingProduct.quantity === 0) {
return setCart(cart.filter(p => p.name !== product.name));
return setCart(newCart.filter(p => p.name !== product.name), dispatch);
}
}
return setCart(cart);
return setCart(cart, dispatch);
}
export function updateProductQuantity(product: Product, quantity: number): Product[] {
let cart = cartState.cart;
export function updateProductQuantity(cart: Product[], dispatch: Dispatch<ActionType>, product: Product, quantity: number): Product[] {
let newCart: Product[] = cart;
// Check if product already exists in cart, then update quantity
const existingProduct = cart.find(p => p.name === product.name);
const existingProduct = newCart.find(p => p.name === product.name);
if (existingProduct) {
existingProduct.quantity = quantity;
}
return setCart(cart);
return setCart(newCart, dispatch);
}
function setCart(newCart: Product[]): Product[]{
function setCart(newCart: Product[], dispatch: Dispatch<ActionType>): Product[]{
dispatch({ type: 'SET_CART', payload: newCart });
return newCart;
}
\ No newline at end of file
......@@ -90,7 +90,7 @@ function Cart() {
<button
type="button"
className="ml-4 text-sm font-medium text-indigo-600 hover:text-indigo-500 sm:ml-0 sm:mt-3"
onClick={() => removeFromCart(cartItem)}
onClick={() => removeFromCart(cartState.cart, dispatch, cartItem)}
>
<span>Supprimer</span>
</button>
......
......@@ -34,7 +34,7 @@ const SellListItem = ({product}: {product: Product}) => {
<div className="-mt-px flex divide-x divide-gray-200">
<div className="-ml-px w-0 flex-1 flex">
<button
onClick={() => addToCart(product)}
onClick={() => addToCart(cartState.cart, dispatch, product)}
className="relative w-0 flex-1 inline-flex items-center justify-center py-4 text-sm text-gray-700 font-medium border border-transparent rounded-br-lg hover:text-gray-500"
>
<ShoppingCartIcon className="w-5 h-5 mr-2 text-gray-400" aria-hidden="true"/>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment