add tags logic

This commit is contained in:
Profile Profile
2026-03-02 00:39:07 +03:00
parent 0940493c21
commit 7739647549
11 changed files with 585 additions and 138 deletions

View File

@@ -114,62 +114,95 @@ export async function getPostsByCategory(slug, first = 14, after = null) {
cacheKey,
async () => {
const query = `
query GetPostsByCategory($first: Int!, $after: String, $slug: String!) {
profileArticles(
first: $first
after: $after
where: {
orderby: { field: DATE, order: DESC }
categoryName: $slug
}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
id
databaseId
title
uri
date
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
author {
node {
id
name
firstName
lastName
avatar {
url
}
uri
}
}
# Соавторы как массив
coauthors {
query GetPostsByCategory($first: Int!, $after: String, $slug: ID!) {
category(id: $slug, idType: SLUG) {
name
contentNodes(
first: $first
after: $after
where: {
contentTypes: [PROFILE_ARTICLE, ANEW]
orderby: { field: DATE, order: DESC }
}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
__typename
id
name
firstName
lastName
url
description
databaseId
uri
date
... on NodeWithTitle {
title
}
... on NodeWithFeaturedImage {
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
}
# Для ваших кастомных типов
... on ProfileArticle {
categories {
nodes {
name
slug
color
}
}
}
categories {
nodes {
id
name
color
slug
uri
databaseId
... on ANew {
categories {
nodes {
name
slug
color
}
}
}
... on NodeWithAuthor {
author {
node {
name
avatar {
url
}
}
}
}
# Coauthors для ProfileArticle (без description)
... on ProfileArticle {
coauthors {
id
name
firstName
lastName
url
}
}
# Coauthors для ANew (без description)
... on ANew {
coauthors {
id
name
firstName
lastName
url
}
}
}
}
@@ -180,11 +213,31 @@ export async function getPostsByCategory(slug, first = 14, after = null) {
const data = await fetchGraphQL(query, { first, after, slug });
const posts = data.profileArticles?.edges?.map(edge => edge.node) || [];
// Обрабатываем посты
const posts = data.category?.contentNodes?.edges?.map(edge => {
const node = edge.node;
// Приводим coauthors к единому формату (если нужно)
if (node.coauthors) {
node.coauthors = node.coauthors.map(author => ({
id: author.id,
name: author.name || '',
firstName: author.firstName || '',
lastName: author.lastName || '',
url: author.url || ''
}));
}
return node;
}) || [];
return {
posts,
pageInfo: data.profileArticles?.pageInfo || { hasNextPage: false, endCursor: null }
pageInfo: data.category?.contentNodes?.pageInfo || {
hasNextPage: false,
endCursor: null
},
categoryName: data.category?.name || ''
};
},
{ ttl: CACHE_TTL.POSTS }
@@ -192,6 +245,156 @@ export async function getPostsByCategory(slug, first = 14, after = null) {
}
export async function getPostsByTag(slug, first = 14, after = null) {
// Создаем уникальный ключ для кэша
const cacheKey = `tag-posts:${slug}:${first}:${after || 'first-page'}`;
return await cache.wrap(
cacheKey,
async () => {
const query = `
query GetPostsByTag($first: Int!, $after: String, $slug: ID!) {
tag(id: $slug, idType: NAME) {
id
databaseId
name
slug
count
description
contentNodes(
first: $first
after: $after
where: {
contentTypes: [PROFILE_ARTICLE, ANEW]
orderby: { field: DATE, order: DESC }
}
) {
pageInfo {
hasNextPage
endCursor
}
edges {
cursor
node {
__typename
id
databaseId
uri
date
... on NodeWithTitle {
title
}
... on NodeWithFeaturedImage {
featuredImage {
node {
sourceUrl(size: LARGE)
altText
}
}
}
... on NodeWithAuthor {
author {
node {
name
avatar {
url
}
}
}
}
... on ProfileArticle {
categories {
nodes {
name
slug
}
}
}
... on ANew {
categories {
nodes {
name
slug
}
}
}
... on ProfileArticle {
coauthors {
id
name
}
}
... on ANew {
coauthors {
id
name
}
}
}
}
}
}
}
`;
console.log('Fetching with:', { first, after, slug }); // Добавим лог параметров
const data = await fetchGraphQL(query, { first, after, slug });
// Проверяем структуру data
if (!data?.tag) {
console.log('Tag not found');
return {
posts: [],
pageInfo: { hasNextPage: false, endCursor: null },
tagName: slug,
tagSlug: slug,
tagCount: 0,
tagDescription: ''
};
}
// Обрабатываем посты
const posts = data.tag?.contentNodes?.edges?.map(edge => {
const node = edge.node;
// Приводим данные к единому формату
return {
...node,
// Убеждаемся, что coauthors всегда массив
coauthors: node.coauthors || [],
// Убеждаемся, что categories всегда есть
categories: node.categories || { nodes: [] }
};
}) || [];
return {
posts,
pageInfo: data.tag?.contentNodes?.pageInfo || {
hasNextPage: false,
endCursor: null
},
tagName: data.tag?.name || slug,
tagSlug: data.tag?.slug || slug,
tagCount: data.tag?.count || 0,
tagDescription: data.tag?.description || ''
};
},
{ ttl: CACHE_TTL.POSTS }
);
}