From 3d2b359e9f99c47ca2c3b428805b1b0251b6bc3f Mon Sep 17 00:00:00 2001 From: Demian Katz Date: Fri, 5 Jul 2024 09:04:47 -0400 Subject: [PATCH] More plumbing. --- module/GeebyDeeby/config/module.config.php | 11 +++++++++++ .../src/GeebyDeeby/Controller/AbstractBase.php | 2 +- .../GeebyDeeby/Controller/SearchController.php | 15 +++++++++++++++ public/js/search.js | 14 ++++++++++---- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/module/GeebyDeeby/config/module.config.php b/module/GeebyDeeby/config/module.config.php index 9ffa8448..4536ecf5 100644 --- a/module/GeebyDeeby/config/module.config.php +++ b/module/GeebyDeeby/config/module.config.php @@ -1204,6 +1204,17 @@ ], ], ], + 'search-creator-ajax' => [ + 'type' => 'Literal', + 'options' => [ + 'route' => '/Search/CreatorAjax', + 'defaults' => [ + '__NAMESPACE__' => 'GeebyDeeby\Controller', + 'controller' => 'Search', + 'action' => 'creatorAjax', + ], + ], + ], 'series' => [ 'type' => 'Segment', 'options' => [ diff --git a/module/GeebyDeeby/src/GeebyDeeby/Controller/AbstractBase.php b/module/GeebyDeeby/src/GeebyDeeby/Controller/AbstractBase.php index bf82947a..6dd94e09 100644 --- a/module/GeebyDeeby/src/GeebyDeeby/Controller/AbstractBase.php +++ b/module/GeebyDeeby/src/GeebyDeeby/Controller/AbstractBase.php @@ -90,7 +90,7 @@ protected function getDbTable($table) } /** - * Die with a JSON-encoded error message. + * Die with a JSON-encoded message. * * @param string $msg The message to send back. * @param bool $success Success status diff --git a/module/GeebyDeeby/src/GeebyDeeby/Controller/SearchController.php b/module/GeebyDeeby/src/GeebyDeeby/Controller/SearchController.php index 2b572b2f..455ad26c 100644 --- a/module/GeebyDeeby/src/GeebyDeeby/Controller/SearchController.php +++ b/module/GeebyDeeby/src/GeebyDeeby/Controller/SearchController.php @@ -55,6 +55,21 @@ protected function tokenize($q) return explode(' ', $q); } + /** + * Retrieve creator details by AJAX. + * + * @return mixed + */ + public function creatorAjaxAction() + { + $post = $this->params()->fromPost(); + $ids = explode(',', $post['ids']); + foreach ($ids as $id) { + $response[$id] = "foo $id"; + } + return $this->jsonDie($response, true); + } + /** * Keyword results * diff --git a/public/js/search.js b/public/js/search.js index e45a327f..b506e03b 100644 --- a/public/js/search.js +++ b/public/js/search.js @@ -1,7 +1,8 @@ +/*global basePath */ function setupSearch() { let creatorsVisible = false; let creatorsLoading = false; - const loadCreators = function () { + const loadCreators = async function () { if (!creatorsLoading) { creatorsLoading = true; const ids = []; @@ -9,13 +10,18 @@ function setupSearch() { ids.push(element.dataset.itemId); }); const uniqueIds = [...new Set(ids)]; - uniqueIds.forEach((id) => { + let formData = new FormData(); + formData.append('ids', uniqueIds); + const response = await fetch(basePath + "/Search/CreatorAjax", { body: formData, method: "post" }); + const json = await response.json(); + for (const id in json.msg) { + const text = json.msg[id]; document.querySelectorAll('.creators[data-item-id="' + id + '"]').forEach((element) => { element.dataset.loaded = "1"; element.classList.remove("hidden"); - element.querySelector('.values').innerHTML = id; + element.querySelector('.values').innerHTML = text; }); - }); + }; } }; document.querySelectorAll('.search-controls').forEach((element) => {