Skip to content

Commit 62cf66a

Browse files
committed
feat: add react-smart-conditional dependency and implement theme button styles; enhance routing and product display functionality
1 parent e402a5a commit 62cf66a

File tree

10 files changed

+97
-21
lines changed

10 files changed

+97
-21
lines changed

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"react": "^19.0.0",
1616
"react-dom": "^19.0.0",
1717
"react-icons": "^5.5.0",
18-
"react-router-dom": "^7.4.1"
18+
"react-router-dom": "^7.4.1",
19+
"react-smart-conditional": "^1.0.4"
1920
},
2021
"devDependencies": {
2122
"@eslint/js": "^9.21.0",

src/App.css

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,35 @@ header {
662662
font-weight: 500;
663663
}
664664

665+
/* Theme Button Styles */
666+
.theme-btn {
667+
position: fixed;
668+
bottom: 2rem;
669+
right: 2rem;
670+
width: 3rem;
671+
height: 3rem;
672+
border-radius: 50%;
673+
background-color: #fff;
674+
border: 2px solid #e3e3e3;
675+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
676+
cursor: pointer;
677+
display: flex;
678+
align-items: center;
679+
justify-content: center;
680+
font-size: 1.2rem;
681+
z-index: 1000;
682+
transition: all 0.3s ease;
683+
}
684+
685+
.theme-btn:hover {
686+
transform: scale(1.1);
687+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
688+
}
689+
690+
.theme-btn span {
691+
line-height: 1;
692+
}
693+
665694
@media screen and (max-width: 550px) {
666695
.product-detail {
667696
display: flex;

src/App.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,22 @@ import { BrowserRouter, Route, Routes } from "react-router-dom";
66
import CheckOut from "./pages/CheckOut";
77
import NotFound from "./pages/NotFound";
88
import Product from "./pages/Product";
9+
import { useProducts } from "./hooks/useProducts";
910

1011
function App() {
12+
const { products, status } = useProducts();
1113
return (
1214
<>
1315
<BrowserRouter>
1416
<Routes>
15-
<Route path="/" element={<HomePage />} />
16-
<Route path="/product" element={<Product />} />
17+
<Route
18+
path="/"
19+
element={<HomePage products={products} status={status} />}
20+
/>
21+
<Route
22+
path="/product/:id"
23+
element={<Product products={products} />}
24+
/>
1725
<Route path="/login" element={<Login />} />
1826
<Route path="/checkout" element={<CheckOut />} />
1927
<Route path="*" element={<NotFound />} />

src/components/Body.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Carousel from "./Carousel";
22
import ProductDisplay from "./ProductDisplay";
33
import Loader from "./Loader";
44
import Error from "./Error";
5+
import Theme from "./Theme";
56

67
function Body({ products, status, onAdd }) {
78
return (
@@ -12,6 +13,7 @@ function Body({ products, status, onAdd }) {
1213
<ProductDisplay products={products} onAdd={onAdd} />
1314
)}
1415
{status === "error" && <Error />}
16+
<Theme />
1517
</div>
1618
);
1719
}

src/components/Product.jsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useRef, useState } from "react";
66
import { Link } from "react-router-dom";
77

88
function Product({ productObj, onAdd }) {
9-
const { image, name, priceCents } = productObj;
9+
const { image, name, priceCents, id } = productObj;
1010

1111
const [selectValue, setSelectValue] = useState(1);
1212
const [userRating, setUserRating] = useState(0);
@@ -81,9 +81,7 @@ function Product({ productObj, onAdd }) {
8181
</div>
8282
<div className="btn-container">
8383
<button className="preview-btn">
84-
<Link to="/product" state={{ product: productObj }}>
85-
preview product
86-
</Link>
84+
<Link to={`/product/${id}`}>product preview</Link>
8785
</button>
8886
<button className="buy-button" onClick={handleBuy}>
8987
Buy

src/components/Theme.jsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect } from "react";
2+
import { useState } from "react";
3+
import { Show } from "react-smart-conditional";
4+
5+
function Theme() {
6+
const [isDark, setIsDark] = useState(false);
7+
8+
useEffect(() => {
9+
document.documentElement.classList.toggle("Dark-mode");
10+
}, [isDark]);
11+
return (
12+
<button className="theme-btn" onClick={() => setIsDark((s) => !s)}>
13+
<Show>
14+
<Show.If condition={isDark} as={"span"}>
15+
☀️
16+
</Show.If>
17+
<Show.Else>🌙</Show.Else>
18+
</Show>
19+
</button>
20+
);
21+
}
22+
23+
export default Theme;

src/pages/HomePage.jsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { useState } from "react";
2-
import { useProducts } from "../hooks/useProducts";
2+
33

44
import Header from "../components/Header";
55
import Body from "../components/Body";
66

7-
function HomePage() {
8-
const { products, status } = useProducts();
7+
8+
function HomePage({products, status}) {
9+
910
const [shoppingCart, setShoppingCart] = useState([]);
1011
const [query, setQuery] = useState("");
1112

src/pages/Login.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useEffect, useRef, useState } from "react";
22
import Header from "../components/Header";
33
import { NavLink } from "react-router-dom";
4+
import Theme from "../components/Theme";
45

56
function Login() {
67
const [email, setEmail] = useState("");
@@ -58,6 +59,7 @@ function Login() {
5859
</button>
5960
</div>
6061
</div>
62+
<Theme />
6163
</div>
6264
);
6365
}

src/pages/Product.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Link, useLocation } from "react-router-dom";
1+
import { Navigate, useNavigate, useParams } from "react-router-dom";
22
import Header from "../components/Header";
3+
import Theme from "../components/Theme";
34

4-
function Product() {
5-
const location = useLocation();
6-
const {
7-
product: { image, name, priceCents },
8-
} = location.state;
5+
function Product({ products }) {
6+
const { id } = useParams();
7+
const navigate = useNavigate()
98

10-
console.log(image, name, priceCents);
9+
const matchProduct = products.find((product) => product.id === id);
10+
11+
// console.log(matchProduct);
12+
13+
const { image, name, priceCents } = matchProduct;
1114

1215
return (
1316
<>
@@ -28,12 +31,11 @@ function Product() {
2831
</span>
2932
</div>
3033
<div>
31-
<button>
32-
<Link to="/">Return To Products</Link>
33-
</button>
34+
<button onClick={() => navigate(-1)}>back to products</button>
3435
</div>
3536
</div>
3637
</div>
38+
<Theme />
3739
</>
3840
);
3941
}

0 commit comments

Comments
 (0)