category , subcategory, product url changes
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
"use client";
|
||||
|
||||
import React, { useState, useEffect } from "react";
|
||||
import React, { useState, useEffect, useContext } from "react";
|
||||
import { backendUrl } from "@/utils/axios";
|
||||
import axios from "axios";
|
||||
import Image from "next/image";
|
||||
import { motion } from "framer-motion";
|
||||
import slugify from "slugify";
|
||||
import ProductContext from "@/app/contexts/productContext";
|
||||
|
||||
const cleanHTML = (html) => {
|
||||
if (!html) return "";
|
||||
@@ -12,48 +14,54 @@ const cleanHTML = (html) => {
|
||||
};
|
||||
|
||||
const SubcategoryHero = ({ params }) => {
|
||||
const [selectedCategory, setSubcategory] = useState(null);
|
||||
const { products, category } = useContext(ProductContext);
|
||||
console.log("SubcategoryHero mounted. Params:", params);
|
||||
const [selectedSubcategory, setSelectedSubcategory] = useState(null);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchSubcategory = async () => {
|
||||
try {
|
||||
const response = await axios.get(
|
||||
`${backendUrl}/admins/product/subcategory/${params.subCategoryId}`
|
||||
);
|
||||
console.log(response.data)
|
||||
const selectedSubcategory = response.data
|
||||
setSubcategory(selectedSubcategory);
|
||||
} catch (error) {
|
||||
console.error("Error fetching subcategory:", error);
|
||||
}
|
||||
};
|
||||
fetchSubcategory();
|
||||
}, [params.subCategoryId]);
|
||||
useEffect(() => {
|
||||
if (products?.length > 0) {
|
||||
const match = products
|
||||
.map((p) => p.product_subcategory)
|
||||
.find(
|
||||
(sub) =>
|
||||
slugify(sub?.subcategory_name || "", { lower: true }) ===
|
||||
params.subcategory
|
||||
);
|
||||
|
||||
// Fallback values
|
||||
console.log("Matched from products:", match);
|
||||
setSelectedSubcategory(match || null);
|
||||
}
|
||||
}, [products, params.subcategory]);
|
||||
|
||||
// Fallbacks
|
||||
const fallbackImage = "/placeholder-image.jpg";
|
||||
const fallbackDescription =
|
||||
"Explore our curated collection of spiritual items designed to enhance your journey of self-discovery and inner peace.";
|
||||
const fallbackCategoryName = "Spiritual Essentials";
|
||||
|
||||
return (
|
||||
<section className="relative min-h-[50vh] md:h-[80vh] py-8 md:py-0 flex items-center justify-center">
|
||||
<section className="relative min-h-[50vh] md:h-[80vh] py-8 md:py-0 flex mt-3 justify-center">
|
||||
<div className="container mx-auto px-4">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-8 items-center">
|
||||
{/* Hero Image */}
|
||||
<div className="relative h-[250px] md:h-[500px] overflow-hidden rounded-lg shadow-xl">
|
||||
<Image
|
||||
src={`${
|
||||
selectedCategory?.image_url
|
||||
? backendUrl + selectedCategory?.image_url
|
||||
: "/sidhi-mala/Artboard_1_bf5ccd46-7152-4355-82a8-9e9f27c1bfc2.jpg"
|
||||
}`}
|
||||
alt="Category Background"
|
||||
layout="fill"
|
||||
objectFit="cover"
|
||||
src={
|
||||
selectedSubcategory?.image
|
||||
? `${backendUrl}${selectedSubcategory.image}`
|
||||
: fallbackImage
|
||||
}
|
||||
alt={
|
||||
selectedSubcategory?.subcategory_name || "Category Background"
|
||||
}
|
||||
fill
|
||||
style={{ objectFit: "cover" }}
|
||||
quality={100}
|
||||
priority
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Hero Text */}
|
||||
<div className="text-start flex justify-start flex-col h-full">
|
||||
<motion.h1
|
||||
initial={{ opacity: 0, y: -20 }}
|
||||
@@ -61,7 +69,7 @@ const SubcategoryHero = ({ params }) => {
|
||||
transition={{ duration: 0.6 }}
|
||||
className="text-2xl md:text-3xl font-bold text-black mb-4"
|
||||
>
|
||||
{selectedCategory?.subcategory_name || fallbackCategoryName}
|
||||
{selectedSubcategory?.subcategory_name || fallbackCategoryName}
|
||||
</motion.h1>
|
||||
|
||||
<motion.div
|
||||
@@ -71,7 +79,7 @@ const SubcategoryHero = ({ params }) => {
|
||||
className="text-black text-sm md:text-base"
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: cleanHTML(
|
||||
selectedCategory?.description || fallbackDescription
|
||||
selectedSubcategory?.description || fallbackDescription
|
||||
),
|
||||
}}
|
||||
/>
|
||||
@@ -82,4 +90,4 @@ const SubcategoryHero = ({ params }) => {
|
||||
);
|
||||
};
|
||||
|
||||
export default SubcategoryHero;
|
||||
export default SubcategoryHero;
|
||||
Reference in New Issue
Block a user