mirror of
https://git.rdcenter.ru/RDCenter/skb-kit.git
synced 2026-12-05 03:49:45 +00:00
Изменить механизм получения медиа; убрать взаимодействие с API FileBird
This commit is contained in:
1 parent
c3aa1cccf8
commit
a02fc54b8b
4 files changed
+33
-19
No files matched your search
+30
-16
@@ -2,9 +2,6 @@ import * as WPAPI from 'wpapi';
|
||||
|
||||
import reactPress from './reactPress';
|
||||
|
||||
// Токен fileBird
|
||||
const fbToken = 'ltvszr1EL7Q8lX9v4W7ivAEwJsFUvicFGtLepCDj';
|
||||
|
||||
const wp = new WPAPI(
|
||||
process.env.NODE_ENV === 'development'
|
||||
? {
|
||||
@@ -14,19 +11,38 @@ const wp = new WPAPI(
|
||||
}
|
||||
: { endpoint: reactPress.api.rest_url, nonce: reactPress.api.nonce }
|
||||
);
|
||||
wp.setHeaders('Authorization', 'Bearer ' + fbToken);
|
||||
|
||||
// Получаем категории медиафайлов
|
||||
const response = await wp.url(reactPress.api.rest_url + 'filebird/public/v1/folders')
|
||||
const { data: { folders } } = response;
|
||||
const mediaCategories = {};
|
||||
// Функция загрузки медабиблиотеки по категориям
|
||||
async function loadMedia() {
|
||||
// Пытаемся получить медиа, пока не получится
|
||||
let response;
|
||||
while (!response) {
|
||||
try {
|
||||
response = await wp.media();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Деструктурируем объекты в формат {category: id}
|
||||
for (let { id, text } of folders) {
|
||||
mediaCategories[text] = id;
|
||||
// Нам потребуется id, категория и url
|
||||
const result = {};
|
||||
for (let { id, alt_text, source_url: url } of response) {
|
||||
const [category, name] = alt_text.split('_');
|
||||
|
||||
// Создаем ключ с пустым массивом, если еще нет
|
||||
if (!result[category]) {
|
||||
result[category] = [];
|
||||
}
|
||||
|
||||
// Добавляем медиа в массив нужной категории
|
||||
result[category].push({ id, name, url });
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
console.log(response);
|
||||
console.log(mediaCategories);
|
||||
|
||||
const mediaLibrary = await loadMedia();
|
||||
|
||||
export async function getPosts(q = '') {
|
||||
try {
|
||||
const posts = await wp.posts().search(q);
|
||||
@@ -39,9 +55,7 @@ export async function getPosts(q = '') {
|
||||
|
||||
export async function getMedia(category) {
|
||||
try {
|
||||
const response = await wp.url(reactPress.api.rest_url + 'filebird/public/v1/attachment-id/?folder_id=' + mediaCategories[category]);
|
||||
console.log(response);
|
||||
return [];
|
||||
return mediaLibrary[category];
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return [];
|
||||
|
||||
@@ -15,7 +15,7 @@ function Slider({ autoPlay = 0, autoPlayTime = 0 }) {
|
||||
|
||||
useEffect(() => {
|
||||
const loadData = async () => {
|
||||
const newMediaList = await getMedia('CATEGORY');
|
||||
const newMediaList = await getMedia('gallery');
|
||||
setMediaList(newMediaList);
|
||||
};
|
||||
loadData();
|
||||
|
||||
@@ -11,7 +11,7 @@ export default function SlidesList() {
|
||||
style={{ transform: `translateX(-${slideNumber * 100}%)` }}
|
||||
>
|
||||
{mediaList.map((slide, index) => (
|
||||
<Slide key={index} slide={slide.source_url} />
|
||||
<Slide key={index} slide={slide.url} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user