From 2dad45711f9c4d544ee36397b719a2eb7f652036 Mon Sep 17 00:00:00 2001 From: ifraimka1 Date: Thu, 3 Apr 2025 20:01:39 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=8B:=20=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D0=BD=20=D0=BF=D0=BE=D1=81=D1=82=20=D0=BF=D0=BE=20id=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D1=8B=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=B8=20(=D1=82=D0=B5=D0=B3).=20=D0=A3=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D1=8F=20=D0=B2=20fetchPosts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/posts/api/get.ts | 81 ++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/src/modules/posts/api/get.ts b/src/modules/posts/api/get.ts index a3a153e..1d7cec5 100644 --- a/src/modules/posts/api/get.ts +++ b/src/modules/posts/api/get.ts @@ -4,18 +4,18 @@ import { parseContent } from "@/shared/api/parseContent"; import { fetchMedia } from "@/modules/media/api/get"; import { App, WPv2 } from "@/shared/types/app"; import Parse from "html-react-parser"; +import { QueryFunctionContext } from "@tanstack/query-core"; + export const fetchPosts = async (): Promise => { - const [posts, categories, tags, mediaLibrary] = await Promise.all([ + const [posts, categories, mediaLibrary] = await Promise.all([ wp.posts().param("per_page", 100).get() as Promise, wp.categories().get() as Promise, - wp.tags().get() as Promise, fetchMedia(), ]); const categoryMap = new Map( categories.map((c) => [c.id, c.name]) ); - const tagMap = new Map(tags.map((t) => [t.id, t.name])); const result: App.PostsResult = { projects: {}, @@ -29,18 +29,15 @@ export const fetchPosts = async (): Promise => { (id) => categoryMap.get(id) || "" ); - const newPost: App.ProjectPost = { + const newPost: App.WpPost = { id: post.id, title: post.title.rendered, - content: parseContent(post.content.rendered), - lab: "", - preview: image?.src || null, categories: postCategories, - tag: post.tags.map((id) => tagMap.get(id) || "")[0] || "", + tag: post.tags[0] || -1, + preview: image?.src || null, }; if (postCategories.includes("projects")) { - newPost.lab = postCategories.filter((c) => c !== "projects")[0] || ""; result.projects[post.id] = newPost; } else if (postCategories.includes("labs")) { //@ts-ignore @@ -51,6 +48,72 @@ export const fetchPosts = async (): Promise => { result.other.push(newPost); } } + console.log('postsresult', result); + return result; +}; + +export const fetchPostById = async ({ queryKey }: QueryFunctionContext): Promise => { + const [id] = queryKey as [number]; + + const [post, categories] = await Promise.all([ + wp.posts().id(id).get() as Promise, + wp.categories().get() as Promise + ]); + + const categoryMap = new Map( + categories.map((c) => [c.id, c.name]) + ); + + const postCategories = post.categories.map( + (id) => categoryMap.get(id) || "" + ); + + const result: App.WpPost = { + id: post.id, + title: post.title.rendered, + content: parseContent(post.content.rendered), + categories: postCategories, + tag: post.tags[0], + }; + + return result; +}; + +export const fetchProjects = async ({ queryKey }: QueryFunctionContext): Promise => { + const [tag] = queryKey as [number]; + + const categories = await wp.categories().get() as WPv2.Category[]; + const catId = categories.find(item => item.name === "projects")?.id || -1; + + const postsRequest = wp.posts(); + if (tag !== -1) { + postsRequest.tags(tag); + } + + const [posts, mediaLibrary] = await Promise.all([ + postsRequest.get() as Promise, + fetchMedia(), + ]); + + const labIndex = posts.findIndex((post) => { + return !post.categories.includes(catId); + }); + posts.splice(labIndex, 1); + + const result: App.WpPost[] = []; + + for (const post of posts) { + const image = mediaLibrary.find((item) => item.id === post.featured_media); + + const newPost: App.WpPost = { + id: post.id, + title: post.title.rendered, + tag: post.tags[0] || -1, + preview: image?.src || null, + }; + + result.push(newPost); + } return result; };