Skip to content

Commit 8e1aadc

Browse files
authored
fix: addresses several ui issues in shop (#253)
* fix: only display cartIcon in header at /shop * fix(style): add radius to shop items in single item view * fix: remove space character after item URL in shop carousel * fix: display item categories in their color codes in the cart drawer * feat: display cart items count besides the cart icon * refactor: remove redundant style declarations * feat: display and keep track of cart items count in the cart icon * feat: display cart count if there's at least one item * fix: linting issue * fix: slice product name in ProductCard if len exceeds 15
1 parent 9254211 commit 8e1aadc

File tree

6 files changed

+57
-12
lines changed

6 files changed

+57
-12
lines changed

src/components/Header.jsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ function Header() {
6868
{/* mobile menu */}
6969
<div className="flex gap-4 items-center">
7070
<div className="flex md:hidden">
71-
<button
72-
type="button"
73-
aria-label="open cart"
74-
onClick={() => setOpen(true)}
75-
>
76-
<CartIcon />
77-
</button>
71+
{pathname === "/shop" && (
72+
<button
73+
type="button"
74+
aria-label="open cart"
75+
onClick={() => setOpen(true)}
76+
>
77+
<CartIcon />
78+
</button>
79+
)}
7880
</div>
7981
{showNavlinks ? (
8082
<button

src/components/shop/CartDrawer.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { LazyLoadImage } from "react-lazy-load-image-component";
88
import { Link, useNavigate } from "react-router-dom";
99
import { useDeleteSwag } from "../../hooks/Mutations/shop/useCartSwagg";
1010
import formatPrice from "../../utilities/formatPrice";
11+
import { categoryColors } from "../../utilities/utils";
1112

1213
function CartDrawer({ open, setOpen }) {
1314
const navigate = useNavigate();
@@ -64,6 +65,8 @@ function CartDrawer({ open, setOpen }) {
6465
const handleDeleteSwag = (cartItemId) => {
6566
removeSwagFromCart(cartItemId);
6667
deleteFromLocalStorage(cartItemId);
68+
// dispatch custom event to notify cart change
69+
window.dispatchEvent(new Event("swagListUpdated"));
6770
};
6871

6972
const handleCheckout = () => {
@@ -148,7 +151,12 @@ function CartDrawer({ open, setOpen }) {
148151
<div className="flex flex-row justify-between items-center mb-4">
149152
<div className="flex flex-col space-y-2 justify-start">
150153
<div className="flex justify-between">
151-
<p className="flex justify-between items-center gap-1 font-medium bg-[#FEF3F2] text-[#B42318] text-sm rounded-full px-2 py-1">
154+
<p
155+
style={categoryColors(
156+
cartProduct.category
157+
)}
158+
className="flex justify-between items-center gap-1 font-medium bg-[#FEF3F2] text-[#B42318] text-sm rounded-full px-2 py-1"
159+
>
152160
<CiShoppingTag />
153161
{cartProduct.category}
154162
</p>

src/components/shop/CartIcon.jsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,45 @@
1+
import { useEffect, useState } from "react";
12
import { MdAddShoppingCart } from "react-icons/md";
23
import SectionWrapper from "./SectionWrapper";
34

45
function CartIcon() {
6+
const [cartProducts, setCartProducts] = useState(() => {
7+
// Initialize state with the value from localStorage if it exists
8+
const storedProducts = localStorage.getItem("swagList");
9+
return storedProducts ? JSON.parse(storedProducts) : [];
10+
});
11+
12+
useEffect(() => {
13+
// Event listener for storage changes in other tabs/windows
14+
const handleStorageChange = () => {
15+
const storedProducts = localStorage.getItem("swagList");
16+
setCartProducts(storedProducts ? JSON.parse(storedProducts) : []);
17+
};
18+
19+
// Listen for "storage" events, whenever swagList is updated from another tab
20+
window.addEventListener("storage", handleStorageChange);
21+
// Listen for custom swagListUpdated events, fired whenever swagList is updated
22+
window.addEventListener("swagListUpdated", handleStorageChange);
23+
24+
// Cleanup event listeners on unmount
25+
return () => {
26+
window.removeEventListener("storage", handleStorageChange);
27+
window.removeEventListener("swagListUpdated", handleStorageChange);
28+
};
29+
}, []);
30+
531
return (
632
<SectionWrapper>
7-
<div className="flex justify-end ">
33+
<div className="flex justify-end relative">
834
<div className="w-12 md:w-16 h-12 md:h-16 rounded-full p-0.5 md:p-1 bg-white border shadow-lg cursor-pointer">
935
<div className="flex w-full h-full p-1.5 md:p-2 rounded-full justify-center items-center bg-green-dark">
1036
<MdAddShoppingCart color="white" className="h-full w-full" />
1137
</div>
38+
{cartProducts.length > 0 && (
39+
<div className="absolute bottom-0 right-0 bg-[#B3261E] text-white text-xs font-medium rounded-full w-5 h-5 flex items-center justify-center">
40+
<p>{cartProducts?.length}</p>
41+
</div>
42+
)}
1243
</div>
1344
</div>
1445
</SectionWrapper>

src/components/shop/ProductCard.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ function ProductCard({ product }) {
3535
className="flex justify-between pr-1"
3636
>
3737
<h3 className="text-md uppercase font-medium text-gray-600">
38-
{product.name}
38+
{product.name.length > 15
39+
? `${product.name.slice(0, 18)}...`
40+
: product.name}
3941
</h3>
4042
<div className="p-1 rounded-xl bg-green-dark/10">
4143
{totalStock > 0 ? (

src/pages/shop/SingleItemPage.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ export default function SingleItemPage() {
128128
});
129129
// Add to local storage
130130
addToLocalStorage();
131+
// dispatch custom event to notify cart change
132+
window.dispatchEvent(new Event("swagListUpdated"));
131133

132134
// open cart
133135
setOpen(true);
@@ -177,7 +179,7 @@ export default function SingleItemPage() {
177179
key={index}
178180
src={image}
179181
alt={singleSwag.name}
180-
className={`m-auto min-w-full ${selectedImage === index + 1 ? "block" : "hidden"}`}
182+
className={`m-auto rounded-lg min-w-full ${selectedImage === index + 1 ? "block" : "hidden"}`}
181183
/>
182184
))}
183185
</div>

src/pages/shop/sections/Banner/Carousal.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function Carousel() {
8585
From KES {formatPrice(swagList[selectedIndex]?.price)}
8686
</p>
8787
<Link
88-
to={`/shop/item/${isSuccess ? swagList[selectedIndex]?.slug : ""} `}
88+
to={`/shop/item/${isSuccess ? swagList[selectedIndex]?.slug : ""}`}
8989
className="text-white font-bold bg-gradient-to-b to-primary from-green-dark py-3 px-4 mb-2 rounded-md"
9090
>
9191
Shop Now

0 commit comments

Comments
 (0)