Skip to content

Commit 571855e

Browse files
committed
image from assets by getImgUrl utility function
1 parent 69672da commit 571855e

File tree

9 files changed

+21
-2
lines changed

9 files changed

+21
-2
lines changed

public/group-books.png

-432 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/components/ProductCard.jsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getImgUrl } from "../utils";
12
import AddToCart from "./AddToCart";
23
import Ratings from "./Ratings";
34

@@ -17,7 +18,11 @@ function ProductCard({ product }) {
1718
className="grid cursor-pointer gap-2 pb-6 transition-all delay-100 ease-in hover:shadow-lg"
1819
>
1920
<div className="group relative">
20-
<img src={image} />
21+
<img
22+
src={getImgUrl(image)}
23+
alt={name}
24+
className="aspect-square bg-brand-gray object-cover"
25+
/>
2126
<div className="absolute bottom-0 left-0 right-0 hidden w-full items-center justify-center bg-brand-dark py-1.5 text-sm text-white group-hover:flex">
2227
Quick View
2328
</div>

src/components/cart/CartProduct.jsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useContext } from "react";
22
import { CartContext } from "../../context";
3+
import { getImgUrl } from "../../utils";
34

45
function CartProduct({ product }) {
56
const { cartItems, setCartItems } = useContext(CartContext);
@@ -42,7 +43,11 @@ function CartProduct({ product }) {
4243
<>
4344
<div className="grid border font-semibold sm:grid-cols-12 sm:gap-4 sm:p-4">
4445
<div className="flex items-center justify-center p-3 sm:col-span-2 sm:p-0">
45-
<img src={image} className="h-20" />
46+
<img
47+
src={getImgUrl(image)}
48+
alt={name}
49+
className="aspect-square h-20 bg-brand-gray object-cover text-xs"
50+
/>
4651
</div>
4752
<div className="flex items-center justify-center border-t p-3 text-center sm:col-span-3 sm:border-0 sm:p-0">
4853
{name}

src/utils/getImgUrl.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function getImgUrl(path) {
2+
console.log(path);
3+
console.log(path.replace(/^\//, ""));
4+
5+
return new URL(`../assets/${path.replace(/^\//, "")}`, import.meta.url).href;
6+
}

src/utils/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import getImgUrl from "./getImgUrl";
2+
3+
export { getImgUrl };

0 commit comments

Comments
 (0)