diff --git a/package-lock.json b/package-lock.json index d01ddfa..bbd664e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "react-ecommerce", "version": "0.0.1", "dependencies": { "@headlessui/react": "^1.6.0", diff --git a/src/Components/Cart.jsx b/src/Components/Cart.jsx index 917b7a1..3b29653 100644 --- a/src/Components/Cart.jsx +++ b/src/Components/Cart.jsx @@ -1,140 +1,179 @@ +import { useEffect } from "react"; import { Dialog, Transition } from "@headlessui/react"; -import { XIcon } from "@heroicons/react/outline"; +import { XIcon, ShoppingCartIcon } from "@heroicons/react/outline"; import React, { Fragment } from "react"; export default function Cart({ open, setOpen, cart, updateCart }) { - return ( - - { - setOpen; - }} - > -
- - - -
- -
-
-
-
- Shopping cart -
- -
-
+ const calculate_cart_total = () => { + let sum = 0 + cart.forEach((product) => { + // sum += product.price + sum = sum + (product.price * product.quantity) + }) + return sum + } -
-
-
    - {cart.map((product) => ( -
  • -
    - {product.imageAlt} -
    + useEffect(() => { + // localStorage can only store string values thats + // why we use JSON.stringify + localStorage.setItem(`cart-item`, JSON.stringify(cart)); +}, [cart]); -
    -
    -
    -

    {product.name}

    -

    ${product.price}

    -
    -
    -
    -

    Qty {product.quantity}

    -
    - -
    -
    -
    -
  • - ))} -
-
-
-
+
+ +
+
+
+
+ + Shopping Cart + +
+ +
+
+ {/* if no items then show Empty Cart icon */} + {cart.length === 0 && +
+ +

Your Cart is Empty.

+
+ } -
-
-

Subtotal

-

$262.00

-
-

Shipping and taxes calculated at checkout.

- -
-

- or{" "} - -

-
-
-
-
- -
-
-
-
- ); +
+
+ +
+
+ + +
+
+

Subtotal

+

${calculate_cart_total()}

+
+

+ Shipping and taxes calculated at checkout. +

+
+ + Checkout + +
+
+

+ or{" "} + +

+
+
+ + + + + + + + ); } diff --git a/src/Components/NavBar.jsx b/src/Components/NavBar.jsx index b1d0db9..9e9fff5 100644 --- a/src/Components/NavBar.jsx +++ b/src/Components/NavBar.jsx @@ -1,7 +1,9 @@ import { ShoppingBagIcon } from "@heroicons/react/outline"; import React from "react"; -export default function NavBar({ setOpen }) { +export default function NavBar({ cart, setOpen }) { + let cart_items = cart.length + return (
@@ -41,7 +43,7 @@ export default function NavBar({ setOpen }) { className="flex-shrink-0 h-6 w-6 text-gray-400 group-hover:text-gray-500" aria-hidden="true" /> - 0 + {cart_items} items in cart, view bag
diff --git a/src/Components/ProductFilters.jsx b/src/Components/ProductFilters.jsx index 0f3262a..09a8721 100644 --- a/src/Components/ProductFilters.jsx +++ b/src/Components/ProductFilters.jsx @@ -7,6 +7,10 @@ function classNames(...classes) { } export default function ProductFilters({ filterOptions, setFilterOptions, sortOptions, setSortOptions }) { + const sort_list = () => { + setSortOptions(sortOptions === 'arc' ? 'desc' : 'asc') + } + return ( (