refactor: enhance blog functionality with related blogs fetching, improved loading states, and tag filtering

This commit is contained in:
hamed
2025-11-19 11:32:09 +03:30
parent 235a64c378
commit e26d342c90
10 changed files with 251 additions and 130 deletions
+41 -9
View File
@@ -1,21 +1,53 @@
"use client";
import { useState } from "react";
import { useState, useEffect } from "react";
import ItemTitle from "./ItemTitle";
import { request } from "@/services/response";
const listTitle = ["سلامت روان", "پوست و مو", "کودک و نوجوان", "دارو", "تغذیه"];
function Title() {
function Title({ onTagChange }) {
const [activeBtn, setActiveBtn] = useState(0);
const [tags, setTags] = useState([]);
useEffect(() => {
const fetchTags = async () => {
try {
const params = {
page: 1,
limit: 100,
};
const response = await request.getBlogTags(params);
setTags(response.data || []);
} catch (error) {
console.error("Error fetching tags:", error);
setTags([]);
}
};
fetchTags();
}, []);
const handleTagClick = (idx, tagId) => {
setActiveBtn(idx);
if (onTagChange) {
onTagChange(tagId);
}
};
return (
<ul className="flex w-full overflow-auto hidden-scroll items-center justify-start gap-[8px] sm:gap-[10px] md:gap-[13px] lg:gap-[16px]">
{listTitle.map((name, idx) => (
<ItemTitle
setActiveBtn={() => handleTagClick(0, null)}
activeBtn={activeBtn}
name="همه"
idx={0}
key="all"
/>
{tags.map((tag, idx) => (
<ItemTitle
setActiveBtn={setActiveBtn}
setActiveBtn={() => handleTagClick(idx + 1, tag.id)}
activeBtn={activeBtn}
name={name}
idx={idx}
key={idx}
name={tag.name}
idx={idx + 1}
key={tag.uuid}
/>
))}
</ul>