Merge pull request #39 from ifraimka1/fix/posts-sort

Пофиксил отображение проектов. Убрал лишнее поле categories в типе
This commit is contained in:
Ifraim Solomonov authored and GitHub committed 2025-04-17 19:05:14 +03:00
commit b5af78d89e
3 files changed
+11 -5

No files matched your search

+10 -2
View File
@@ -6,10 +6,16 @@ import { App, WPv2 } from "@/shared/types/app";
export const fetchPostProjects = async (tag: number): Promise<App.Card[]> => { export const fetchPostProjects = async (tag: number): Promise<App.Card[]> => {
const categories = await wp.categories().get() as WPv2.Category[]; const categories = await wp.categories().get() as WPv2.Category[];
const catId = categories.find(item => item.name === "projects")?.id || -1; const catId = categories.find(item => item.name === "projects")?.id || -1;
const isLabPage = tag !== -1; // Если передали тег, значит, это страница лаборатории
// Одновременно отфильтровать и по тегу, по категории WP не дает
// Если тега нет, проще взять категорию projects
// Если тег есть, надо искать по нему
const postsRequest = wp.posts(); const postsRequest = wp.posts();
if (tag !== -1) { if (isLabPage) {
postsRequest.tags(tag); postsRequest.tags(tag);
} else {
postsRequest.categories(catId);
} }
const [posts, mediaLibrary] = await Promise.all([ const [posts, mediaLibrary] = await Promise.all([
@@ -17,10 +23,13 @@ export const fetchPostProjects = async (tag: number): Promise<App.Card[]> => {
fetchMedia(), fetchMedia(),
]); ]);
// При выборке по тегу подтянется еще и лаборатория, ее нужно убрать из массива
if (isLabPage) {
const labIndex = posts.findIndex((post) => { const labIndex = posts.findIndex((post) => {
return !post.categories.includes(catId); return !post.categories.includes(catId);
}); });
posts.splice(labIndex, 1); posts.splice(labIndex, 1);
}
const result: App.Card[] = []; const result: App.Card[] = [];
@@ -30,7 +39,6 @@ export const fetchPostProjects = async (tag: number): Promise<App.Card[]> => {
const newPost: App.Card = { const newPost: App.Card = {
id: post.id, id: post.id,
title: post.title.rendered, title: post.title.rendered,
//tag: post.tags[0] || -1,
preview: image?.src || null, preview: image?.src || null,
}; };
+1 -2
View File
@@ -31,7 +31,6 @@ export const fetchPosts = async (): Promise<App.PostsResult> => {
const newPost: App.WpPost = { const newPost: App.WpPost = {
id: post.id, id: post.id,
title: post.title.rendered, title: post.title.rendered,
categories: postCategories,
}; };
if (postCategories.includes("projects")) { if (postCategories.includes("projects")) {
@@ -58,6 +57,6 @@ export const fetchPosts = async (): Promise<App.PostsResult> => {
result.other.push(newWpPostPage); result.other.push(newWpPostPage);
} }
} }
console.log('fetchPosts result', result);
return result; return result;
}; };
-1
View File
@@ -276,7 +276,6 @@ declare namespace App {
export interface WpPost { export interface WpPost {
id: number; id: number;
title: string; title: string;
categories: string[];
} }
export interface WpPostPage extends WpPost { export interface WpPostPage extends WpPost {