From 58b39d511915ef68c8f43bc5b8766bb5d716945b Mon Sep 17 00:00:00 2001 From: ifraimka Date: Wed, 24 Apr 2024 13:59:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D1=82=D1=8C=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B5=D0=B4=D0=B8?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 1 + package.json | 1 + src/api.js | 47 ++++++++++++++++++++++++++--------------------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index e8f79f9..814c5fb 100755 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "lodash": "^4.17.21", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.22.0", diff --git a/package.json b/package.json index aa1b4e1..12d7837 100755 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "@testing-library/jest-dom": "^5.17.0", "@testing-library/react": "^13.4.0", "@testing-library/user-event": "^13.5.0", + "lodash": "^4.17.21", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.22.0", diff --git a/src/api.js b/src/api.js index 0f02d22..40484c8 100755 --- a/src/api.js +++ b/src/api.js @@ -1,4 +1,5 @@ import * as WPAPI from 'wpapi'; +import { flatten } from 'lodash'; import reactPress from './reactPress'; @@ -12,34 +13,38 @@ const wp = new WPAPI( : { endpoint: reactPress.api.rest_url, nonce: reactPress.api.nonce } ); -// Функция загрузки медабиблиотеки по категориям -async function loadMedia() { - // Пытаемся получить медиа, пока не получится - let response; - while (!response) { - try { - response = await wp.media(); - } catch (e) { - console.error(e); +async function getAllMedia(request) { + return request.then(function (response) { + if (!response._paging || !response._paging.next) { + return response; } - } + // Запрос к следующей странице и возврат ответов одной коллекцией + return Promise.all([ + response, + getAllMedia(response._paging.next) + ]).then(function (responses) { + return flatten(responses); + }); + }); +} + +// Функция загрузки медабиблиотеки и сортировки по категориям +async function loadMedia() { + const response = await getAllMedia(wp.media()); // Нам потребуется id, категория и url const result = {}; - while (response) { - for (let { id, alt_text, source_url: image } of response) { - const [category, name] = alt_text.split('_'); - // Создаем ключ с пустым массивом, если еще нет - if (!result[category]) { - result[category] = []; - } + for (let { id, alt_text, source_url: image } of response) { + const [category, name] = alt_text.split('_'); - // Добавляем медиа в массив нужной категории - result[category].push({ id, name, image }); + // Создаем ключ с пустым массивом, если еще нет + if (!result[category]) { + result[category] = []; } - if (!response._paging.next) break; - response = await response._paging.next.get(); + + // Добавляем медиа в массив нужной категории + result[category].push({ id, name, image }); } return result;