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(() => {
|
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>
|
||||||
|
|||||||
Reference in New Issue
Block a user