fix(tags): update tag fetching logic and improve tag handling in Title component
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user