all products page
This commit is contained in:
@@ -1,24 +1,55 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { allProduct } from "@/lib/allProducts";
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import EmptyState from "./emptyData";
|
import EmptyState from "./emptyData";
|
||||||
|
import { GET } from "@/lib/allProducts";
|
||||||
|
import { ProductModal } from "../ProductModal";
|
||||||
|
import { motion } from "framer-motion";
|
||||||
|
import { ProductCard } from "../ProductCard";
|
||||||
|
|
||||||
|
const itemVariants = {
|
||||||
|
hidden: { opacity: 0, y: 20 },
|
||||||
|
visible: { opacity: 1, y: 0 },
|
||||||
|
};
|
||||||
|
|
||||||
export default function Products() {
|
export default function Products() {
|
||||||
const [allProducts, setAllProducts] = useState<any>(null);
|
const [allProducts, setAllProducts] = useState<any>(null);
|
||||||
|
const [selectedProduct, setSelectedProduct] = useState<any>(null);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const all = allProduct();
|
const all = GET();
|
||||||
all && Array.isArray(all) && all.length > 0
|
all && Array.isArray(all) && all.length > 0
|
||||||
? setAllProducts(all)
|
? setAllProducts(all)
|
||||||
: setAllProducts([]);
|
: setAllProducts([]);
|
||||||
setAllProducts;
|
setAllProducts;
|
||||||
}, []);
|
}, []);
|
||||||
|
const handleViewDetails = (slug: string) => {
|
||||||
|
const product = allProducts.find((p: any) => p.slug === slug);
|
||||||
|
if (product) {
|
||||||
|
setSelectedProduct(product);
|
||||||
|
}
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<div className="">
|
<div className="">
|
||||||
{allProducts && allProducts.length > 0 ? (
|
{allProducts && allProducts.length > 0 ? (
|
||||||
<div className="grid lg:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-4"></div>
|
<div className="grid lg:grid-cols-3 sm:grid-cols-2 grid-cols-1 gap-4">
|
||||||
|
{allProducts.map((product: any) => (
|
||||||
|
<motion.div key={product.id} variants={itemVariants}>
|
||||||
|
<ProductCard
|
||||||
|
product={product}
|
||||||
|
onViewDetails={handleViewDetails}
|
||||||
|
/>
|
||||||
|
</motion.div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<EmptyState />
|
<EmptyState />
|
||||||
)}
|
)}
|
||||||
|
{/* Product Modal */}
|
||||||
|
{selectedProduct && (
|
||||||
|
<ProductModal
|
||||||
|
product={selectedProduct}
|
||||||
|
onClose={() => setSelectedProduct(null)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
export const allProduct = async () => {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get('https://api.serenmebel.uz/api/products/');
|
const res = await axios.get("https://api.serenmebel.uz/api/products/");
|
||||||
console.log("all products res: ",res);
|
console.log("all products res: ", res?.data);
|
||||||
|
|
||||||
return res
|
return Response.json(res.data);
|
||||||
}catch(err){
|
} catch (error: any) {
|
||||||
console.log("all product error: ",err);
|
console.log("all products error: ", error);
|
||||||
return []
|
|
||||||
|
return Response.json({ error: error.message });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user