|  | 
|  | 1 | +import React, { createContext, useContext, useReducer } from "react"; | 
|  | 2 | + | 
|  | 3 | +const CartContext = createContext({}); | 
|  | 4 | + | 
|  | 5 | +export const useCartStore = () => { | 
|  | 6 | +  const context = useContext(CartContext); | 
|  | 7 | + | 
|  | 8 | +  if (!context) { | 
|  | 9 | +    throw new Error("Consumer should be wrapped inside the Provider"); | 
|  | 10 | +  } | 
|  | 11 | + | 
|  | 12 | +  return context; | 
|  | 13 | +}; | 
|  | 14 | + | 
|  | 15 | +const cartReducer = (state, action) => { | 
|  | 16 | +  switch (action.type) { | 
|  | 17 | +    case "ADD": { | 
|  | 18 | +      const { id } = action.payload; | 
|  | 19 | +      const newState = [...state]; | 
|  | 20 | +      const index = state.findIndex(cartItem => { | 
|  | 21 | +        return cartItem.id === id; | 
|  | 22 | +      }); | 
|  | 23 | +      if (index > -1) { | 
|  | 24 | +        let quantity = newState[index].quantity + 1; | 
|  | 25 | +        if (quantity > 4) quantity = 4; | 
|  | 26 | +        newState[index] = { | 
|  | 27 | +          ...newState[index], | 
|  | 28 | +          quantity, | 
|  | 29 | +        }; | 
|  | 30 | +      } else { | 
|  | 31 | +        newState.push({ | 
|  | 32 | +          ...action.payload, | 
|  | 33 | +        }); | 
|  | 34 | +      } | 
|  | 35 | +      return [...newState]; | 
|  | 36 | +    } | 
|  | 37 | +    case "UPDATE": { | 
|  | 38 | +      const { id } = action.payload; | 
|  | 39 | +      const updatedItems = state.map(cartItem => { | 
|  | 40 | +        if (cartItem.id === id) { | 
|  | 41 | +          return { | 
|  | 42 | +            ...action.payload, | 
|  | 43 | +          }; | 
|  | 44 | +        } | 
|  | 45 | +        return cartItem; | 
|  | 46 | +      }); | 
|  | 47 | +      return [...updatedItems]; | 
|  | 48 | +    } | 
|  | 49 | +    case "REMOVE": { | 
|  | 50 | +      const { id } = action.payload; | 
|  | 51 | +      const remainingItems = state.filter(cartItem => cartItem.id !== id); | 
|  | 52 | +      return [...remainingItems]; | 
|  | 53 | +    } | 
|  | 54 | +    default: | 
|  | 55 | +      return state; | 
|  | 56 | +  } | 
|  | 57 | +}; | 
|  | 58 | + | 
|  | 59 | +const CartProvider = ({ children }) => { | 
|  | 60 | +  const [cart, dispatch] = useReducer(cartReducer, []); | 
|  | 61 | + | 
|  | 62 | +  const addToCart = payload => { | 
|  | 63 | +    dispatch({ type: "ADD", payload }); | 
|  | 64 | +  }; | 
|  | 65 | + | 
|  | 66 | +  const removeFromCart = payload => { | 
|  | 67 | +    dispatch({ type: "REMOVE", payload }); | 
|  | 68 | +  }; | 
|  | 69 | + | 
|  | 70 | +  const updateCart = payload => { | 
|  | 71 | +    dispatch({ type: "UPDATE", payload }); | 
|  | 72 | +  }; | 
|  | 73 | + | 
|  | 74 | +  const context = { | 
|  | 75 | +    cart, | 
|  | 76 | +    addToCart, | 
|  | 77 | +    updateCart, | 
|  | 78 | +    removeFromCart, | 
|  | 79 | +  }; | 
|  | 80 | + | 
|  | 81 | +  return ( | 
|  | 82 | +    <CartContext.Provider value={context}>{children}</CartContext.Provider> | 
|  | 83 | +  ); | 
|  | 84 | +}; | 
|  | 85 | + | 
|  | 86 | +export default CartProvider; | 
0 commit comments