Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetiraj3697 committed Nov 13, 2022
1 parent e4690ed commit d711fd0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
34 changes: 34 additions & 0 deletions src/Reducer/cartReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,40 @@ const cartReducer = (state, action) => {
cart:[],
}
}
if(action.type === "SET_DECREMENT"){
let updatedProduct = state.cart.map((curElem) => {
if(curElem.id === action.payload){
let decAmount = curElem.amount - 1;
if(decAmount <= 1){
decAmount = 1;
}
return {
...curElem,
amount:decAmount,
}
}else{
return curElem;
}
})
return {...state,cart:updatedProduct}
}
if(action.type === "SET_INCREMENT"){
let updatedProduct = state.cart.map((curElem) => {
if(curElem.id === action.payload){
let inAmount = curElem.amount + 1;
if(inAmount >= curElem.max){
inAmount = curElem.max;
}
return {
...curElem,
amount:inAmount,
}
}else{
return curElem;
}
})
return {...state,cart:updatedProduct}
}
return state;
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/CartAmountToggle.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import { FaMinus, FaPlus } from 'react-icons/fa';

const CartAmountToggle = ({amount,setDecrese, setIncrease}) => {
const CartAmountToggle = ({amount,setDecrease, setIncrease}) => {
return (
<div className='cart-button'>
<div className='amount-toggle'>
<button onClick={()=>setDecrese()}>
<button onClick={()=>setDecrease()}>
<FaMinus />
</button>
<div className='amount-style'>{amount}</div>
Expand Down
18 changes: 9 additions & 9 deletions src/components/CartItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { FaTrash } from "react-icons/fa";
import { useCartContext } from "../context/cart_context";

const CartItem = ({ id, name, image, color, price, amount }) => {
const { removeItem } = useCartContext();
const setDecrease = () => {
// amount > 1 ? setAmount(amount - 1) : setAmount(1);
};
const { removeItem,setDecrease,setIncrease } = useCartContext();
// const setDecrease = () => {
// amount > 1 ? setAmount(amount - 1) : setAmount(1);
// };

const setIncrease = () => {
// amount < stock ? setAmount(amount + 1) : setAmount(stock);
};
// const setIncrease = () => {
// amount < stock ? setAmount(amount + 1) : setAmount(stock);
// };

return (
<div className="cart_heading grid grid-five-column">
Expand Down Expand Up @@ -42,8 +42,8 @@ const CartItem = ({ id, name, image, color, price, amount }) => {
{/* Quantity */}
<CartAmountToggle
amount={amount}
setDecrease={setDecrease}
setIncrease={setIncrease}
setDecrease={() =>setDecrease(id)}
setIncrease={() =>setIncrease(id)}
/>

{/* //Subtotal */}
Expand Down
46 changes: 31 additions & 15 deletions src/context/cart_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { createContext, useContext, useReducer } from "react";
import reducer from "../Reducer/cartReducer";

const CartContext = createContext();
const getLocalCartData = () =>{
const getLocalCartData = () => {
let newCartData = localStorage.getItem("SheelaCart");
if(newCartData === []){
if (newCartData === []) {
return [];
}else{
} else {
return JSON.parse(newCartData);
}
}
};
const initialState = {
// cart: [],
cart:getLocalCartData(),
cart: getLocalCartData(),
total_item: "",
total_amount: "",
shipping_fee: 50000,
Expand All @@ -29,16 +29,32 @@ const CartProvider = ({ children }) => {
const removeItem = (id) => {
dispatch({ type: "REMOVE_ITEM", payload: id });
};
const clearCart = () =>{
dispatch({type:"CLEAR_CART"})
}
// to add the data in localStorage
//get vs set
useEffect(()=>{
localStorage.setItem("SheelaCart",JSON.stringify(state.cart))
},[state.cart])
//increment and decrement items
const setDecrease = (id) => {
dispatch({ type: "SET_DECREMENT", payload: id });
};
const setIncrease = (id) => {
dispatch({ type: "SET_INCREMENT", payload: id });
};
const clearCart = () => {
dispatch({ type: "CLEAR_CART" });
};
// to add the data in localStorage
//get vs set
useEffect(() => {
localStorage.setItem("SheelaCart", JSON.stringify(state.cart));
}, [state.cart]);
return (
<CartContext.Provider value={{ ...state, addToCart, removeItem,clearCart }}>
<CartContext.Provider
value={{
...state,
addToCart,
removeItem,
clearCart,
setDecrease,
setIncrease,
}}
>
{children}
</CartContext.Provider>
);
Expand All @@ -48,4 +64,4 @@ const useCartContext = () => {
return useContext(CartContext);
};

export { CartProvider, useCartContext };
export { CartProvider, useCartContext };

0 comments on commit d711fd0

Please sign in to comment.