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(() => { useEffect(() => {
const fetchTags = async () => { const fetchTags = async () => {
try { try {
const params = { // Blog tags are stored as names on each blog; build the chip list from
page: 1, // the tag names actually present in published blogs.
limit: 100, const response = await request.getBlogs({ page: 1, limit: 50 });
}; const names = (response?.data || []).flatMap((b) =>
const response = await request.getBlogTags(params); Array.isArray(b.tags) ? b.tags : []
setTags(response?.data?.data || []); );
const uniqueNames = [...new Set(names)];
setTags(uniqueNames.map((name) => ({ name })));
} catch (error) { } catch (error) {
console.error("Error fetching tags:", error); console.error("Error fetching tags:", error);
setTags([]); setTags([]);
@@ -25,10 +27,10 @@ function Title({ onTagChange }) {
fetchTags(); fetchTags();
}, []); }, []);
const handleTagClick = (idx, tagId) => { const handleTagClick = (idx, tagName) => {
setActiveBtn(idx); setActiveBtn(idx);
if (onTagChange) { if (onTagChange) {
onTagChange(tagId); onTagChange(tagName);
} }
}; };
@@ -43,11 +45,11 @@ function Title({ onTagChange }) {
/> />
{tags.map((tag, idx) => ( {tags.map((tag, idx) => (
<ItemTitle <ItemTitle
setActiveBtn={() => handleTagClick(idx + 1, tag.id)} setActiveBtn={() => handleTagClick(idx + 1, tag.name)}
activeBtn={activeBtn} activeBtn={activeBtn}
name={tag.name} name={tag.name}
idx={idx + 1} idx={idx + 1}
key={tag.uuid} key={tag.name}
/> />
))} ))}
</ul> </ul>