"use client"; import Image from "next/image"; import React, { useEffect, useState } from "react"; import "./blog.css"; import axios from "axios"; import { backendUrl } from "@/utils/axios"; import DOMPurify from 'dompurify'; import { useParams } from "next/navigation"; const SingleBlog = () => { const [blog, setBlog] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); // Debug log to check if slug is received const params = useParams(); console.log("BlogPage params:", params); console.log("SingleBlog received slug:", params?.["blog-id"]); const slug = params?.["blog-id"] useEffect(() => { const fetchBlogData = async () => { if (!slug) { console.error("No slug provided"); setError("Blog not found. Missing identifier."); setLoading(false); return; } try { console.log(`Fetching blog with slug: ${slug}`); setLoading(true); // Log the full URL being called for debugging const url = `${backendUrl}/blogs/${slug}/`; console.log(`Making API call to: ${url}`); const response = await axios.get(url); console.log("API response:", response.data); if (response.data) { setBlog(response.data); } else { setError("No blog data received from server"); } setLoading(false); } catch (err) { console.error("Error fetching blog data:", err); setError(`Failed to load blog content: ${err.message || "Unknown error"}`); setLoading(false); } }; fetchBlogData(); }, [slug]); if (loading) { return (

Loading blog content for slug: {slug}...

); } if (error) { return (

Error: {error}

); } if (!blog) { return (

Blog not found for slug: {slug}

); } // Format the image URL const imageUrl = blog.imageUrl !== "NA" ? (blog.imageUrl.startsWith('http') ? blog.imageUrl : `${backendUrl}${blog.imageUrl}`) : "/api/placeholder/800/500"; // Sanitize the blog description const sanitizedDescription = DOMPurify.sanitize(blog.description); return (
{/* Blog Title */}

{blog.title}

{/* Blog Meta Information */}

{blog.author} | {blog.date}

{/* Blog Image */}
{blog.title
{/* Blog Content */}
); }; export default SingleBlog;