fix(tags): update tag fetching logic and improve tag handling in Title component

This commit is contained in:
hamed
2026-06-21 17:34:12 +03:30
parent 1841a16b4d
commit 03eb483486
+12 -10
View File
@@ -10,12 +10,14 @@ function Title({ onTagChange }) {
useEffect(() => {
const fetchTags = async () => {
try {
const params = {
page: 1,
limit: 100,
};
const response = await request.getBlogTags(params);
setTags(response?.data?.data || []);
// Blog tags are stored as names on each blog; build the chip list from
// the tag names actually present in published blogs.
const response = await request.getBlogs({ page: 1, limit: 50 });
const names = (response?.data || []).flatMap((b) =>
Array.isArray(b.tags) ? b.tags : []
);
const uniqueNames = [...new Set(names)];
setTags(uniqueNames.map((name) => ({ name })));
} catch (error) {
console.error("Error fetching tags:", error);
setTags([]);
@@ -25,10 +27,10 @@ function Title({ onTagChange }) {
fetchTags();
}, []);
const handleTagClick = (idx, tagId) => {
const handleTagClick = (idx, tagName) => {
setActiveBtn(idx);
if (onTagChange) {
onTagChange(tagId);
onTagChange(tagName);
}
};
@@ -43,11 +45,11 @@ function Title({ onTagChange }) {
/>
{tags.map((tag, idx) => (
<ItemTitle
setActiveBtn={() => handleTagClick(idx + 1, tag.id)}
setActiveBtn={() => handleTagClick(idx + 1, tag.name)}
activeBtn={activeBtn}
name={tag.name}
idx={idx + 1}
key={tag.uuid}
key={tag.name}
/>
))}
</ul>