feat(blog): enhance blog data handling with normalization and improved image management

This commit is contained in:
hamed
2026-06-19 00:35:11 +03:30
parent 4dca516598
commit e9136ea42b
10 changed files with 77 additions and 71 deletions
+7 -35
View File
@@ -1,39 +1,13 @@
"use client";
import { useState, useEffect } from "react";
import CustomLoading from "@/app/component/loading/Custom";
import TextLoading from "@/app/component/loading/Text";
import Link from "next/link";
import { request } from "@/services/response";
// نمایش «بلاگ‌های برتر» فعلاً غیرفعال است (endpoint /api/v1/blogs/top در بک‌اند وجود ندارد).
// markup زیر برای فعال‌سازی بعدی نگه داشته شده؛ تا آن زمان کامپوننت چیزی رندر نمی‌کند.
function Head() {
const [topBlogs, setTopBlogs] = useState([]);
const [isLoading, setIsLoading] = useState(true);
return null;
}
useEffect(() => {
const fetchTopBlogs = async () => {
try {
setIsLoading(true);
const response = await request.getTopBlogs();
setTopBlogs(response || []);
} catch (error) {
console.error("Error fetching top blogs:", error);
setTopBlogs([]);
} finally {
setIsLoading(false);
}
};
fetchTopBlogs();
}, []);
if (isLoading || topBlogs.length === 0) {
return null;
}
const [firstBlog, secondBlog, thirdBlog, fourthBlog] = topBlogs;
return (
<>{/*
/* eslint-disable */
/*
<div className="flex flex-col lg:flex-row mt-[40px] items-center justify-center gap-[16px] sm:gap-[19px] md:gap-[22px] lg:gap-[24px]">
{firstBlog && (
<Link href={`/blog/${firstBlog.uuid}`} className="w-full lg:w-1/2 cover-head-blogs">
@@ -104,8 +78,6 @@ function Head() {
)}
</div>
</div>
*/}</>
);
}
*/
export default Head;
+3 -2
View File
@@ -5,6 +5,7 @@ import Head from "./Head";
import Title from "./title";
import LatestArticles from "./latestArticles";
import { request } from "@/services/response";
import { normalizeBlog } from "@/helper";
function BlogsPage() {
const [blogs, setBlogs] = useState([]);
@@ -25,8 +26,8 @@ function BlogsPage() {
params.tag = selectedTag;
}
const response = await request.getBlogs(params);
setBlogs(response.blogs || []);
setTotalPages(response.page?.totalPages || 1);
setBlogs((response?.data || []).map(normalizeBlog));
setTotalPages(response?.meta?.totalPages || 1);
} catch (error) {
console.error("Error fetching blogs:", error);
setBlogs([]);
+4 -2
View File
@@ -2,10 +2,12 @@ import CustomLoading from "@/app/component/loading/Custom";
import TextLoading from "@/app/component/loading/Text";
import Image from "next/image";
import Link from "next/link";
import { convertTimestampToJalali } from "@/helper";
import { convertTimestampToJalali, imageUrl } from "@/helper";
function Article({ data }) {
const imageSrc = data.images?.[0]?.url || "/assets/images/default-blog.jpg";
const imageSrc = data.images?.[0]?.url
? imageUrl(data.images[0].url, "/assets/images/cover-blog-1.png")
: "/assets/images/cover-blog-1.png";
const createdDate = data.created ? convertTimestampToJalali(data.created) : "";
return (
+1 -1
View File
@@ -15,7 +15,7 @@ function Title({ onTagChange }) {
limit: 100,
};
const response = await request.getBlogTags(params);
setTags(response.data || []);
setTags(response?.data?.data || []);
} catch (error) {
console.error("Error fetching tags:", error);
setTags([]);