mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 02:39:45 +00:00
Сверстать страницу проектов; организовать хранение и передачу информации о проектах
This commit is contained in:
1 parent
cb69de3427
commit
8bbd3ed3cc
5 files changed
+222
-9
No files matched your search
+36
-6
@@ -14,7 +14,7 @@ const wp = new WPAPI(
|
||||
: { endpoint: reactPress.api.rest_url, nonce: reactPress.api.nonce }
|
||||
);
|
||||
|
||||
async function getAllMedia(request) {
|
||||
async function getAllContent(request) {
|
||||
return request.then(function (response) {
|
||||
if (!response._paging || !response._paging.next) {
|
||||
return response;
|
||||
@@ -22,7 +22,7 @@ async function getAllMedia(request) {
|
||||
// Запрос к следующей странице и возврат ответов одной коллекцией
|
||||
return Promise.all([
|
||||
response,
|
||||
getAllMedia(response._paging.next)
|
||||
getAllContent(response._paging.next)
|
||||
]).then(function (responses) {
|
||||
return flatten(responses);
|
||||
});
|
||||
@@ -31,7 +31,7 @@ async function getAllMedia(request) {
|
||||
|
||||
// Функция загрузки медабиблиотеки и сортировки по категориям
|
||||
async function loadMedia() {
|
||||
const response = await getAllMedia(wp.media());
|
||||
const response = await getAllContent(wp.media());
|
||||
|
||||
// Нам потребуется id, категория и url
|
||||
const result = {};
|
||||
@@ -51,13 +51,43 @@ async function loadMedia() {
|
||||
return result;
|
||||
}
|
||||
|
||||
async function loadPosts() {
|
||||
// Получаем рубрики
|
||||
const responseCategories = await getAllContent(wp.categories());
|
||||
const categories = {};
|
||||
|
||||
for (let { id, name } of responseCategories) {
|
||||
categories[id] = name;
|
||||
}
|
||||
|
||||
// Получаем метки
|
||||
const responseTags = await getAllContent(wp.tags());
|
||||
const tags = {};
|
||||
|
||||
for (let { id, name } of responseTags) {
|
||||
tags[id] = name;
|
||||
}
|
||||
|
||||
// Получаем записи
|
||||
const responsePosts = await getAllContent(wp.posts());
|
||||
|
||||
for (let post of responsePosts) {
|
||||
post.categories = post.categories.map(el => categories[el]);
|
||||
post.tags = post.tags.map(el => tags[el]);
|
||||
}
|
||||
|
||||
return responsePosts;
|
||||
}
|
||||
|
||||
const mediaLibrary = await loadMedia();
|
||||
console.log(mediaLibrary);
|
||||
const posts = await loadPosts();
|
||||
console.log(posts);
|
||||
|
||||
export async function getPosts(q = '') {
|
||||
export async function getPosts(name) {
|
||||
try {
|
||||
const posts = await wp.posts().search(q);
|
||||
return posts;
|
||||
const result = posts.filter(post => post.tags.includes(name))[0];
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return [];
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
export { default as ContactForm } from './ContactForm';
|
||||
export { default as Field } from './Field';
|
||||
export { default as Footer } from './Footer';
|
||||
export { default as MediaBlock } from './MediaBlock';
|
||||
export { default as MediaContainer } from './MediaContainer';
|
||||
export { default as Navbar } from './Navbar';
|
||||
export { default as Slider } from './Slider';
|
||||
@@ -1,7 +1,11 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useLoaderData, useParams } from "react-router-dom";
|
||||
import Parse from 'html-react-parser';
|
||||
|
||||
import { PageHeader, PageContent } from "../../components";
|
||||
import { getProjects } from "../../../api";
|
||||
|
||||
import { getProjects, getMedia, getPosts } from "../../../api";
|
||||
import { MediaBlock } from "../../../components";
|
||||
|
||||
export async function loader() {
|
||||
const projects = await getProjects();
|
||||
@@ -9,19 +13,51 @@ export async function loader() {
|
||||
}
|
||||
|
||||
function Project() {
|
||||
const [mediaList, setMediaList] = useState([]);
|
||||
const [post, setPost] = useState();
|
||||
|
||||
const params = useParams();
|
||||
const projectID = params.id;
|
||||
|
||||
const { projects } = useLoaderData();
|
||||
const project = projects[projectID];
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
const newMediaList = await getMedia('projects')
|
||||
.then((result) => {
|
||||
return result.filter(el => el.name.includes(project.tag));
|
||||
});
|
||||
setMediaList(newMediaList);
|
||||
};
|
||||
loadData();
|
||||
}, [projectID]);
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
const newPost = await getPosts('ambr');
|
||||
console.log(newPost);
|
||||
setPost(newPost);
|
||||
console.log(Parse(newPost.content.rendered));
|
||||
};
|
||||
loadData();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader className="header">
|
||||
<h1>{project.name}</h1>
|
||||
<h1>{ typeof post !== 'undefined' && post.title.rendered }</h1>
|
||||
</PageHeader>
|
||||
<PageContent className="content">
|
||||
<p>{project.desc}</p>
|
||||
{ typeof post !== 'undefined' &&
|
||||
Parse(post.content.rendered).map(el => {
|
||||
if (el.type === 'figure') {
|
||||
const media = mediaList.find(item => el.props.children.props.src === item.name);
|
||||
return <MediaBlock images={[media.image]}/>
|
||||
}
|
||||
return el;
|
||||
})
|
||||
}
|
||||
</PageContent>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user