From 26018016e13ad7946c67046a145262d68c87f011 Mon Sep 17 00:00:00 2001 From: ifraimka1 Date: Tue, 15 Apr 2025 15:31:12 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BB=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D1=8B=20?= =?UTF-8?q?=D0=BA=20=D0=BF=D0=BE=D1=81=D1=82=D0=B0=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/posts/api/get.ts | 119 ------------------ src/modules/posts/api/get/fetchPostById.ts | 29 +++++ .../posts/api/get/fetchPostProjects.ts | 41 ++++++ src/modules/posts/api/get/fetchPosts.ts | 63 ++++++++++ src/modules/posts/hooks/usePostById.ts | 8 +- src/modules/posts/hooks/usePostProjects.ts | 10 +- src/modules/posts/hooks/usePosts.ts | 2 +- 7 files changed, 143 insertions(+), 129 deletions(-) delete mode 100644 src/modules/posts/api/get.ts create mode 100644 src/modules/posts/api/get/fetchPostById.ts create mode 100644 src/modules/posts/api/get/fetchPostProjects.ts create mode 100644 src/modules/posts/api/get/fetchPosts.ts diff --git a/src/modules/posts/api/get.ts b/src/modules/posts/api/get.ts deleted file mode 100644 index 1d7cec5..0000000 --- a/src/modules/posts/api/get.ts +++ /dev/null @@ -1,119 +0,0 @@ -// posts/api/get.ts -import { wp } from "@/shared/api/wp-client"; -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, mediaLibrary] = await Promise.all([ - wp.posts().param("per_page", 100).get() as Promise, - wp.categories().get() as Promise, - fetchMedia(), - ]); - - const categoryMap = new Map( - categories.map((c) => [c.id, c.name]) - ); - - const result: App.PostsResult = { - projects: {}, - labs: {}, - other: [], - }; - - for (const post of posts) { - const image = mediaLibrary.find((item) => item.id === post.featured_media); - const postCategories = post.categories.map( - (id) => categoryMap.get(id) || "" - ); - - const newPost: App.WpPost = { - id: post.id, - title: post.title.rendered, - categories: postCategories, - tag: post.tags[0] || -1, - preview: image?.src || null, - }; - - if (postCategories.includes("projects")) { - result.projects[post.id] = newPost; - } else if (postCategories.includes("labs")) { - //@ts-ignore - const excerptElement = Parse(post.excerpt.rendered)[0]; - newPost.previewText = (excerptElement as any)?.props?.children || ""; - result.labs[post.id] = newPost as App.LabPost; - } else { - 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; -}; diff --git a/src/modules/posts/api/get/fetchPostById.ts b/src/modules/posts/api/get/fetchPostById.ts new file mode 100644 index 0000000..55f3a01 --- /dev/null +++ b/src/modules/posts/api/get/fetchPostById.ts @@ -0,0 +1,29 @@ +// posts/api/get.ts +import { wp } from "@/shared/api/wp-client"; +import { parseContent } from "@/shared/api/parseContent"; +import { App, WPv2 } from "@/shared/types/app"; + +export const fetchPostById = async (id: number): Promise => { + 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.WpPostPage = { + id: post.id, + title: post.title.rendered, + content: parseContent(post.content.rendered), + categories: postCategories, + tag: post.tags[0], + }; + + return result; +}; diff --git a/src/modules/posts/api/get/fetchPostProjects.ts b/src/modules/posts/api/get/fetchPostProjects.ts new file mode 100644 index 0000000..69dc48e --- /dev/null +++ b/src/modules/posts/api/get/fetchPostProjects.ts @@ -0,0 +1,41 @@ +// posts/api/get/getPostProjects.ts +import { wp } from "@/shared/api/wp-client"; +import { fetchMedia } from "@/modules/media/api/get"; +import { App, WPv2 } from "@/shared/types/app"; + +export const fetchPostProjects = async (tag: number): Promise => { + 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.Card[] = []; + + for (const post of posts) { + const image = mediaLibrary.find((item) => item.id === post.featured_media); + + const newPost: App.Card = { + id: post.id, + title: post.title.rendered, + //tag: post.tags[0] || -1, + preview: image?.src || null, + }; + + result.push(newPost); + } + + return result; +}; diff --git a/src/modules/posts/api/get/fetchPosts.ts b/src/modules/posts/api/get/fetchPosts.ts new file mode 100644 index 0000000..2d59034 --- /dev/null +++ b/src/modules/posts/api/get/fetchPosts.ts @@ -0,0 +1,63 @@ +// posts/api/get.ts +import { wp } from "@/shared/api/wp-client"; +import { fetchMedia } from "@/modules/media/api/get"; +import { App, WPv2 } from "@/shared/types/app"; +import Parse from "html-react-parser"; +import { parseContent } from "@/shared/api/parseContent"; + +export const fetchPosts = async (): Promise => { + const [posts, categories, mediaLibrary] = await Promise.all([ + wp.posts().param("per_page", 100).get() as Promise, + wp.categories().get() as Promise, + fetchMedia(), + ]); + + const categoryMap = new Map( + categories.map((c) => [c.id, c.name]) + ); + + const result: App.PostsResult = { + projects: {}, + labs: {}, + other: [], + }; + + for (const post of posts) { + const image = mediaLibrary.find((item) => item.id === post.featured_media); + const postCategories = post.categories.map( + (id) => categoryMap.get(id) || "" + ); + + const newPost: App.WpPost = { + id: post.id, + title: post.title.rendered, + categories: postCategories, + }; + + if (postCategories.includes("projects")) { + const newProject: App.Card = { + ...newPost, + preview: image?.src || null, + }; + result.projects[post.id] = newProject; + } else if (postCategories.includes("labs")) { + //@ts-ignore + const excerptElement = Parse(post.excerpt.rendered)[0]; + const newLab: App.LabCard = { + ...newPost, + preview: image?.src || null, + previewText: (excerptElement as any)?.props?.children || "", + }; + result.labs[post.id] = newLab; + } else { + const newWpPostPage: App.WpPostPage = { + ...newPost, + content: parseContent(post.content.rendered), + tag: post.tags[0], + }; + result.other.push(newWpPostPage); + } + } + + return result; +}; diff --git a/src/modules/posts/hooks/usePostById.ts b/src/modules/posts/hooks/usePostById.ts index 3dd74c8..5c25938 100644 --- a/src/modules/posts/hooks/usePostById.ts +++ b/src/modules/posts/hooks/usePostById.ts @@ -1,12 +1,12 @@ // posts/api/usePostById.ts -import { fetchPostById } from "@/modules/posts/api/get"; +import { fetchPostById } from "@/modules/posts/api/get/fetchPostById"; import { useQuery } from "@tanstack/react-query"; import { App } from "@/shared/types/app"; export const usePostById = (id: number) => { - return useQuery({ - queryKey: [id], - queryFn: fetchPostById, + return useQuery({ + queryKey: ["post", id], + queryFn: () => fetchPostById(id), enabled: !!id, }); }; diff --git a/src/modules/posts/hooks/usePostProjects.ts b/src/modules/posts/hooks/usePostProjects.ts index 4a093f7..f074d43 100644 --- a/src/modules/posts/hooks/usePostProjects.ts +++ b/src/modules/posts/hooks/usePostProjects.ts @@ -1,12 +1,12 @@ // posts/api/useProjects.ts -import { fetchProjects } from "@/modules/posts/api/get"; +import { fetchPostProjects } from "@/modules/posts/api/get/fetchPostProjects"; import { useQuery } from "@tanstack/react-query"; import { App } from "@/shared/types/app"; -export const useProjects = (tag: number) => { - return useQuery({ - queryKey: [tag], - queryFn: fetchProjects, +export const usePostProjects = (tag: number) => { + return useQuery({ + queryKey: ["projects", tag], + queryFn: () => fetchPostProjects(tag), enabled: !!tag, }); }; diff --git a/src/modules/posts/hooks/usePosts.ts b/src/modules/posts/hooks/usePosts.ts index 78c0a31..1f1f4a0 100644 --- a/src/modules/posts/hooks/usePosts.ts +++ b/src/modules/posts/hooks/usePosts.ts @@ -1,5 +1,5 @@ // posts/api/usePosts.ts -import { fetchPosts } from "@/modules/posts/api/get"; +import { fetchPosts } from "@/modules/posts/api/get/fetchPosts"; import { App } from "@/shared/types/app"; import { useQuery } from "@tanstack/react-query";