-
+
{product?.title}
-
-
{product?.description}
-
+
+
+ {product?.description}
+
+
${product?.price}
-
+
Category: {product?.category}
-
{product?.rating?.rate}⭐️
+
+
+ Ratings: {product?.rating?.rate}
+ {starIcon}
+ {" "}
+
diff --git a/src/components/api/api.jsx b/src/components/api/api.jsx
index e699196..393d8fe 100644
--- a/src/components/api/api.jsx
+++ b/src/components/api/api.jsx
@@ -1,7 +1,12 @@
export const fetchProducts = async () => {
- const response = await fetch("https://fakestoreapi.com/products");
- if (!response?.ok) {
- throw new Error(`${response?.status} error occurred`);
+ try {
+ const response = await fetch("https://fakestoreapi.com/products");
+ if (!response?.ok) {
+ throw new Error(`${response?.status} error occurred`);
+ }
+ return await response?.json();
+ } catch (error) {
+ console.log("An error occured:", error?.message || error);
+ throw error;
}
- return response?.json();
};
diff --git a/src/components/cartContext/index.jsx b/src/components/cartContext/index.jsx
index 1dc3724..80121dc 100644
--- a/src/components/cartContext/index.jsx
+++ b/src/components/cartContext/index.jsx
@@ -47,15 +47,20 @@ export const CartProvider = ({ children }) => {
}
};
+ const removeItem = (item) => {
+ setCartItems(cartItems?.filter((cartItem) => cartItem?.id !== item?.id));
+ };
+
const clearCart = () => {
setCartItems([]);
};
const getCartTotal = () => {
- return cartItems?.reduce(
- (total, item) => total + item?.price * item?.quantity,
+ const total = cartItems?.reduce(
+ (sum, item) => sum + item?.price * item?.quantity,
0
);
+ return total?.toFixed(2);
};
useEffect(() => {
@@ -75,6 +80,7 @@ export const CartProvider = ({ children }) => {
cartItems,
addToCart,
removeFromCart,
+ removeItem,
clearCart,
getCartTotal,
}}
diff --git a/src/components/cartProducts/index.jsx b/src/components/cartProducts/index.jsx
index be96073..ea3e8d8 100644
--- a/src/components/cartProducts/index.jsx
+++ b/src/components/cartProducts/index.jsx
@@ -1,77 +1,120 @@
-import PropTypes from "prop-types";
-import { useContext } from "react";
+/* eslint-disable react/prop-types */
+import { useContext, useState } from "react";
import { CartContext } from "../cartContext";
+import { crossIcon, crossSecond } from "../../assets/SvgIcons/svgIcons";
export default function Cart({ showModal, toggle }) {
- const { cartItems, addToCart, removeFromCart, clearCart, getCartTotal } =
- useContext(CartContext);
+ const {
+ cartItems,
+ addToCart,
+ removeFromCart,
+ removeItem,
+ clearCart,
+ getCartTotal,
+ } = useContext(CartContext);
+
+ const [checkout, setCheckout] = useState("");
+
+ const handleCheckOut = () => {
+ setCheckout("Your Order has been checked out");
+ setTimeout(() => {
+ setCheckout("");
+ }, 3000);
+ };
return (
showModal && (
-
-
Cart
-
-