-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFeaturedProducts.tsx
108 lines (101 loc) · 2.88 KB
/
FeaturedProducts.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Grid, Typography } from "@mui/material";
import { useEffect, useState } from "react";
import ProductCardSkelton from "../../cards/ProductCardSkeleton";
import ProductCard from "../../cards/ProductCard";
import { useFetchProducts } from "../../../libs/queries";
import { IProduct } from "../../../types";
import api from "../../../redux/api/api";
const FeaturedProducts = () => {
const [products, setProducts] = useState<IProduct[] | []>([]);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState("");
useEffect(() => {
const fetch = async () => {
try {
const res = await api.get("/products");
setProducts(res.data.products);
setIsLoading(false);
} catch (error: any) {
setError(error.message);
setIsLoading(false);
} finally {
setIsLoading(false);
}
};
fetch();
}, []);
if (error) {
return (
<div className="my-5 px-4">
<Typography className="p-3 font-bold" variant="h5">
Featured Products
</Typography>
<p className=" flex justify-center items-center my-[50px]">{error}</p>
</div>
);
}
if (isLoading) {
return (
<div className="my-5">
<div className="flex items-center gap-2 p-3">
<p className="w-5 h-9 bg-[#DB4444] rounded-md" />
<p className="text-[#DB4444] font-medium">Our Products</p>
</div>
<Typography className="p-3 font-bold" variant="h5">
Featured Products
</Typography>
<Grid
container
spacing={{ xs: 1, sm: 1, md: 2 }}
alignItems="center"
data-testid="product-card-skeleton"
className="mb-4"
>
{Array.from({ length: 8 }).map((_, index) => (
<Grid
key={`skeleton-${index}`}
item
xs={12}
sm={4}
md={2.4}
className="soleil2"
>
<ProductCardSkelton value={2} />
</Grid>
))}
</Grid>
</div>
);
}
return (
<div className="my-5">
<div className="flex items-center gap-2 p-3">
<p className="w-5 h-9 bg-[#DB4444] rounded-md" />
<p className="text-[#DB4444] font-medium">Our Products</p>
</div>
<Typography className="p-3 font-bold" variant="h5">
Featured Products
</Typography>
<Grid
container
spacing={{ xs: 1, sm: 1, md: 2 }}
alignItems="center"
className="mb-4 px-4"
>
{products?.slice(0, 10).map((product: IProduct) => (
<Grid
key={product.id}
item
xs={12}
sm={4}
md={2.4}
className="soleil2"
>
<ProductCard product={product} />
</Grid>
))}
</Grid>
</div>
);
};
export default FeaturedProducts;