mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 05:49:45 +00:00
Переделал запросы к постам
This commit is contained in:
1 parent
c66b352d3d
commit
26018016e1
7 files changed
+143
-129
No files matched your search
@@ -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<App.PostsResult> => {
|
|
||||||
const [posts, categories, mediaLibrary] = await Promise.all([
|
|
||||||
wp.posts().param("per_page", 100).get() as Promise<WPv2.Post[]>,
|
|
||||||
wp.categories().get() as Promise<WPv2.Category[]>,
|
|
||||||
fetchMedia(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const categoryMap = new Map<number, string>(
|
|
||||||
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<App.WpPost> => {
|
|
||||||
const [id] = queryKey as [number];
|
|
||||||
|
|
||||||
const [post, categories] = await Promise.all([
|
|
||||||
wp.posts().id(id).get() as Promise<WPv2.Post>,
|
|
||||||
wp.categories().get() as Promise<WPv2.Category[]>
|
|
||||||
]);
|
|
||||||
|
|
||||||
const categoryMap = new Map<number, string>(
|
|
||||||
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<App.WpPost[]> => {
|
|
||||||
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<WPv2.Post[]>,
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
@@ -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<App.WpPostPage> => {
|
||||||
|
const [post, categories] = await Promise.all([
|
||||||
|
wp.posts().id(id).get() as Promise<WPv2.Post>,
|
||||||
|
wp.categories().get() as Promise<WPv2.Category[]>
|
||||||
|
]);
|
||||||
|
|
||||||
|
const categoryMap = new Map<number, string>(
|
||||||
|
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;
|
||||||
|
};
|
||||||
@@ -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<App.Card[]> => {
|
||||||
|
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<WPv2.Post[]>,
|
||||||
|
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;
|
||||||
|
};
|
||||||
@@ -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<App.PostsResult> => {
|
||||||
|
const [posts, categories, mediaLibrary] = await Promise.all([
|
||||||
|
wp.posts().param("per_page", 100).get() as Promise<WPv2.Post[]>,
|
||||||
|
wp.categories().get() as Promise<WPv2.Category[]>,
|
||||||
|
fetchMedia(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const categoryMap = new Map<number, string>(
|
||||||
|
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;
|
||||||
|
};
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
// posts/api/usePostById.ts
|
// 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 { useQuery } from "@tanstack/react-query";
|
||||||
import { App } from "@/shared/types/app";
|
import { App } from "@/shared/types/app";
|
||||||
|
|
||||||
export const usePostById = (id: number) => {
|
export const usePostById = (id: number) => {
|
||||||
return useQuery<App.WpPost>({
|
return useQuery<App.WpPostPage>({
|
||||||
queryKey: [id],
|
queryKey: ["post", id],
|
||||||
queryFn: fetchPostById,
|
queryFn: () => fetchPostById(id),
|
||||||
enabled: !!id,
|
enabled: !!id,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
// posts/api/useProjects.ts
|
// 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 { useQuery } from "@tanstack/react-query";
|
||||||
import { App } from "@/shared/types/app";
|
import { App } from "@/shared/types/app";
|
||||||
|
|
||||||
export const useProjects = (tag: number) => {
|
export const usePostProjects = (tag: number) => {
|
||||||
return useQuery<App.WpPost[]>({
|
return useQuery<App.Card[]>({
|
||||||
queryKey: [tag],
|
queryKey: ["projects", tag],
|
||||||
queryFn: fetchProjects,
|
queryFn: () => fetchPostProjects(tag),
|
||||||
enabled: !!tag,
|
enabled: !!tag,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// posts/api/usePosts.ts
|
// 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 { App } from "@/shared/types/app";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user