Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"cmdk": "^1.1.1",
"date-fns": "^3.6.0",
"embla-carousel-react": "^8.6.0",
"framer-motion": "^12.23.24",
"input-otp": "^1.4.2",
"lucide-react": "^0.462.0",
"next-themes": "^0.3.0",
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import Browse from "./pages/Browse";
import Sell from "./pages/Sell";
import ProductDetailsPage from "./pages/ProductDetail";
import Navbar from "./components/Navbar";
import { CartProvider } from "./components/CartContext.jsx";
import Home from "./pages/Home.jsx";
import Cart from "./pages/Cart.jsx";
import Payment from "./pages/Payment";
import Dashboard from "./pages/Dashboard";
const queryClient = new QueryClient();

Expand All @@ -20,16 +24,22 @@ const App = () => (
<BrowserRouter>
<Navbar />
<div className="pt-[50px]">

<CartProvider>

<Routes>
<Route path="/" element={<Index />} />

<Route path="/signup" element={<SignUp />} />
<Route path="/signin" element={<SignIn />} />
<Route path="/browse" element={<Browse />} />
<Route path="/product/:id" element={<ProductDetailsPage />} />
<Route path="/CartHome" element={<Home />} />
<Route path="/cart" element={<Cart />} />
<Route path="/payment" element={<Payment />} />
<Route path="/sell" element={<Sell />} />
<Route path="/dashboard" element={<Dashboard />} />
</Routes>
</CartProvider>
</div>
</BrowserRouter>
</TooltipProvider>
Expand Down
38 changes: 38 additions & 0 deletions frontend/src/components/CartContext.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { createContext, useReducer, useEffect, useState } from "react";

export const CartContext = createContext();

const initialState = {
cart: JSON.parse(localStorage.getItem("cart")) || [],
};

const reducer = (state, action) => {
switch (action.type) {
case "ADD_ITEM":
return { ...state, cart: [...state.cart, action.payload] };
case "REMOVE_ITEM":
return {
...state,
cart: state.cart.filter((_, i) => i !== action.payload),
};
case "CLEAR_CART":
return { ...state, cart: [] };
default:
return state;
}
};

export const CartProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const [notification, setNotification] = useState(null);

useEffect(() => {
localStorage.setItem("cart", JSON.stringify(state.cart));
}, [state.cart]);

return (
<CartContext.Provider value={{ state, dispatch, notification, setNotification }}>
{children}
</CartContext.Provider>
);
};
Loading
Loading