Skip to content

Commit

Permalink
Connect Backend APIs with eCommerce Website (Display all Products fro…
Browse files Browse the repository at this point in the history
…m the Database)
  • Loading branch information
elyse502 committed Nov 6, 2024
1 parent 1555bdd commit 54ccebe
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VITE_BACKEND_URL = http://localhost:4000
2 changes: 1 addition & 1 deletion frontend/src/components/BestSeller.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const BestSeller = () => {
useEffect(() => {
const bestProduct = products.filter((item) => item.bestseller);
setBestSeller(bestProduct.slice(0, 5));
}, []);
}, [products]);

return (
<div className="my-10">
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/LatestCollection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LatestCollection = () => {

useEffect(() => {
setLatestProducts(products.slice(0, 10));
}, []);
}, [products]);

return (
<div className="my-10">
Expand Down
25 changes: 24 additions & 1 deletion frontend/src/context/ShopContext.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { createContext, useEffect, useState } from "react";
import { products } from "../assets/assets";
import { toast } from "react-toastify";
import { useNavigate } from "react-router-dom";
import axios from "axios";

export const ShopContext = createContext();

const ShopContextProvider = (props) => {
const currency = "$";
const delivery_fee = 10;
const backendUrl = import.meta.env.VITE_BACKEND_URL;
const [search, setSearch] = useState("");
const [showSearch, setShowSearch] = useState(false);
const [cartItems, setCartItems] = useState({});
const [products, setProducts] = useState([]);
const navigate = useNavigate();

const addToCart = async (itemId, size) => {
Expand Down Expand Up @@ -74,6 +76,26 @@ const ShopContextProvider = (props) => {
return totalAmount;
};

const getProductsData = async () => {
try {
const response = await axios.get(backendUrl + "/api/product/list");
console.log(response.data);

if (response.data.success) {
setProducts(response.data.products);
} else {
toast.error(response.data.message);
}
} catch (error) {
console.error(error);
toast.error(error.message);
}
};

useEffect(() => {
getProductsData();
}, []);

const value = {
products,
currency,
Expand All @@ -88,6 +110,7 @@ const ShopContextProvider = (props) => {
updateQuantity,
getCartAmount,
navigate,
backendUrl,
};

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Collection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const Collection = () => {

useEffect(() => {
applyFilter();
}, [category, subCategory, search, showSearch]);
}, [category, subCategory, search, showSearch, products]);

useEffect(() => {
sortProduct();
Expand Down

0 comments on commit 54ccebe

Please sign in to comment.