Skip to content

Commit

Permalink
Merge pull request #251 from vansh-sardana/fixingcartprice
Browse files Browse the repository at this point in the history
Fixed the cart price issue in the navbar
  • Loading branch information
pooranjoyb authored Jun 18, 2024
2 parents e78dca3 + a830bb7 commit e2e8806
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { logout } from "../utils/features/Auth/authSlice";
import Glassnav from "./Floating_Nav";
import Button from "./Button";

interface Product {
name: string;
image: string;
price: number;
ratings: number;
quantity: number;
}


function Screensize() {
const [windowSize, setWindowSize] = useState({ width: window.innerWidth });

Expand All @@ -34,6 +43,7 @@ function Navbar() {
const userName = useSelector((state: RootState) => state.auth.user?.username);
const dispatch = useDispatch();
const [itemsInCart, setItemsInCart] = useState(0);
const [total, setTotal]= useState(0);
const [showMenu, setShowMenu] = useState(false);

useEffect(() => {
Expand All @@ -54,6 +64,15 @@ function Navbar() {
// Assuming products is an array within each row in the Cart table
const totalItems = data.reduce((acc, item) => acc + item.products.length, 0);
setItemsInCart(totalItems);
const subtotalAmount = data.reduce((acc, item) => {
return acc + item.products.reduce((itemAcc: number, product: Product) => {
const price = product.price;
const quantity = product.quantity;
console.log(price,quantity)
return itemAcc + (price * quantity);
}, 0);
}, 0);
setTotal(subtotalAmount);
}
};

Expand Down Expand Up @@ -141,7 +160,7 @@ function Navbar() {
>
<div className="card-body">
<span className="font-bold text-lg text-mynavy">{itemsInCart} Items</span>
<span className="text-mynavy">Subtotal: $$999</span>
<span className="text-mynavy">Subtotal: ${total}</span>
<Link to="/home/shop/cart" className="card-actions w-full">
<Button text="View cart" color="myyellow" hover="mygreen" />
</Link>
Expand Down

0 comments on commit e2e8806

Please sign in to comment.