From 431cbf0c424686cc47d3fdac17e89f743159619e Mon Sep 17 00:00:00 2001
From: Michel Roux
Date: Wed, 26 Jul 2023 01:26:46 +0200
Subject: [PATCH] debounce search OK
---
appinfo/routes.php | 3 +-
lib/Controller/SearchController.php | 55 +++++++++++++++++++++++++++++
lib/Controller/TopController.php | 17 +++++----
src/components/Search.vue | 52 +++++++++++++++++++++++++++
src/components/TopList.vue | 5 ++-
src/utils/debounce.js | 20 +++++++++++
src/views/Discover.vue | 3 ++
7 files changed, 147 insertions(+), 8 deletions(-)
create mode 100644 lib/Controller/SearchController.php
create mode 100644 src/components/Search.vue
create mode 100644 src/utils/debounce.js
diff --git a/appinfo/routes.php b/appinfo/routes.php
index 7e46e20..5ffdd49 100644
--- a/appinfo/routes.php
+++ b/appinfo/routes.php
@@ -13,6 +13,7 @@ declare(strict_types=1);
return [
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
- ['name' => 'top#index', 'url' => '/top/{count}', 'verb' => 'GET']
+ ['name' => 'top#index', 'url' => '/top/{count}', 'verb' => 'GET'],
+ ['name' => 'search#index', 'url' => '/search/{value}', 'verb' => 'GET']
]
];
diff --git a/lib/Controller/SearchController.php b/lib/Controller/SearchController.php
new file mode 100644
index 0000000..addef5c
--- /dev/null
+++ b/lib/Controller/SearchController.php
@@ -0,0 +1,55 @@
+clientService->newClient();
+ $fyydReponse = $fyydClient->get("https://api.fyyd.de/0.2/search/podcast", [
+ 'query' => [
+ 'title' => $value,
+ 'term' => $value
+ ]
+ ]);
+ $fyydJson = (array) json_decode((string) $fyydReponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
+
+ if (array_key_exists('data', $fyydJson) && is_array($fyydJson['data'])) {
+ /** @var string[] $fyydFeed */
+ foreach ($fyydJson['data'] as $fyydFeed) {
+ $podcasts[] = [
+ 'id' => $fyydFeed['id'],
+ 'title' => $fyydFeed['title'],
+ 'description' => $fyydFeed['description'],
+ 'image' => $fyydFeed['imgURL'],
+ 'url' => $fyydFeed['xmlURL'],
+ 'lang' => $fyydFeed['language'],
+ 'lastpub' => $fyydFeed['lastpub']
+ ];
+ }
+ }
+ } catch (Exception $e) {
+ }
+
+ return new JSONResponse($podcasts);
+ }
+}
diff --git a/lib/Controller/TopController.php b/lib/Controller/TopController.php
index 7dc9594..7560237 100644
--- a/lib/Controller/TopController.php
+++ b/lib/Controller/TopController.php
@@ -30,24 +30,29 @@ class TopController extends Controller
* @NoCSRFRequired
*/
public function index(int $count = 10): JSONResponse {
- $userLang = 'en';
+ $language = 'en';
try {
$langClient = $this->clientService->newClient();
$langResponse = $langClient->get("https://api.fyyd.de/0.2/feature/podcast/hot/languages");
$langJson = (array) json_decode((string) $langResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $langJson) && is_array($langJson['data'])) {
- $userLang = $this->l10n->getUserLanguage($this->userSession->getUser());
- $userLang = explode('_', $userLang);
- $userLang = count($userLang) > 1 ? $userLang[1] : $userLang[0];
- $userLang = in_array($userLang, $langJson['data']) ? $userLang : 'en';
+ $language = $this->l10n->getUserLanguage($this->userSession->getUser());
+ $language = explode('_', $language);
+ $language = count($language) > 1 ? $language[1] : $language[0];
+ $language = in_array($language, $langJson['data']) ? $language : 'en';
}
} catch (Exception $e) {
}
try {
$podcastClient = $this->clientService->newClient();
- $podcastReponse = $podcastClient->get("https://api.fyyd.de/0.2/feature/podcast/hot?count={$count}&language={$userLang}");
+ $podcastReponse = $podcastClient->get("https://api.fyyd.de/0.2/feature/podcast/hot", [
+ 'query' => [
+ 'count' => $count,
+ 'language' => $language
+ ]
+ ]);
$podcastJson = (array) json_decode((string) $podcastReponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
return new JSONResponse($podcastJson, $podcastReponse->getStatusCode());
} catch (Exception $e) {
diff --git a/src/components/Search.vue b/src/components/Search.vue
new file mode 100644
index 0000000..d5d1050
--- /dev/null
+++ b/src/components/Search.vue
@@ -0,0 +1,52 @@
+
+
+
+
+
diff --git a/src/components/TopList.vue b/src/components/TopList.vue
index 87e3ff7..a6729dd 100644
--- a/src/components/TopList.vue
+++ b/src/components/TopList.vue
@@ -4,7 +4,7 @@
{{ t('Discover') }}
@@ -39,6 +39,9 @@ export default {
tops: [],
loading: true,
count: 10,
+ Deselect: {
+ render: null,
+ },
}
},
async mounted() {
diff --git a/src/utils/debounce.js b/src/utils/debounce.js
new file mode 100644
index 0000000..9c4cf90
--- /dev/null
+++ b/src/utils/debounce.js
@@ -0,0 +1,20 @@
+/**
+ * Returns a function, that, as long as it continues to be invoked, will not
+ * be triggered. The function will be called after it stops being called for
+ * N milliseconds.
+ * https://stackoverflow.com/a/53486112
+ *
+ * @param {Function} fn function to wrap
+ * @param {number} delay timeout in ms
+ */
+export function debounce(fn, delay) {
+ let timeoutID = null
+ return function() {
+ clearTimeout(timeoutID)
+ const args = arguments
+ const that = this
+ timeoutID = setTimeout(function() {
+ fn.apply(that, args)
+ }, delay)
+ }
+}
diff --git a/src/views/Discover.vue b/src/views/Discover.vue
index 414d0cb..2ebd984 100644
--- a/src/views/Discover.vue
+++ b/src/views/Discover.vue
@@ -5,6 +5,7 @@
+
@@ -14,6 +15,7 @@
import AddRss from '../components/AddRss.vue'
import Magnify from 'vue-material-design-icons/Magnify.vue'
import { NcTextField } from '@nextcloud/vue'
+import Search from '../components/Search.vue'
import TopList from '../components/TopList.vue'
export default {
@@ -22,6 +24,7 @@ export default {
AddRss,
Magnify,
NcTextField,
+ Search,
TopList,
},
data() {