From cb42953853f4c151a46f837792860cda0ba2acd7 Mon Sep 17 00:00:00 2001 From: Aevann Date: Tue, 3 Jan 2023 13:25:28 +0200 Subject: [PATCH] fix gif search --- files/assets/js/gif_modal.js | 48 +++++++++------------------------ files/templates/modals/gif.html | 10 +++---- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/files/assets/js/gif_modal.js b/files/assets/js/gif_modal.js index da5bd62676..00c56d039b 100644 --- a/files/assets/js/gif_modal.js +++ b/files/assets/js/gif_modal.js @@ -1,5 +1,4 @@ const gifSearchBar = document.getElementById('gifSearch') -const loadGIFs = document.getElementById('gifs-load-more'); const noGIFs = document.getElementById('no-gifs-found'); const container = document.getElementById('GIFs'); @@ -25,23 +24,11 @@ document.getElementById('gifModal').addEventListener('shown.bs.modal', function }, 1000); }); -document.getElementById('gifModal').addEventListener('shown.bs.modal', function () { - gifSearchBar.focus(); - setTimeout(() => { - gifSearchBar.focus(); - }, 200); - setTimeout(() => { - gifSearchBar.focus(); - }, 1000); -}); - - async function getGifs(form) { commentFormID = form; gifSearchBar.value = null; - noGIFs.innerHTML = null; - loadGIFs.innerHTML = null; + noGIFs.classList.add("d-none"); container.innerHTML = `
@@ -118,38 +105,29 @@ async function getGifs(form) {
` } +document.getElementById('gifs-back-btn').onclick = getGifs; +document.getElementById('gifs-cancel-btn').onclick = getGifs; + async function searchGifs(searchTerm) { - if (searchTerm !== undefined) { - gifSearchBar.value = searchTerm; - } - else { - gifSearchBar.value = null; - } + gifSearchBar.value = searchTerm; container.innerHTML = ''; let response = await fetch("/giphy?searchTerm=" + searchTerm + "&limit=48"); let data = await response.json() - const max = data.length - 1 data = data.data - const gifURL = []; - if (max <= 0) { - noGIFs.innerHTML = '

Aw shucks. No GIFs found...

'; - container.innerHTML = null; - loadGIFs.innerHTML = null; + if (data.length) { + for (const e of data) { + const url = "https://media.giphy.com/media/" + e.id + "/giphy.webp"; + const insert = `` + container.insertAdjacentHTML('beforeend', insert); + } } else { - for (let i = 0; i < 48; i++) { - gifURL[i] = "https://media.giphy.com/media/" + data[i].id + "/giphy.webp"; - const insert = `` - container.insertAdjacentHTML('beforeend', insert); - noGIFs.innerHTML = null; - loadGIFs.innerHTML = '

Thou hast reached the end of the list!

'; - } + noGIFs.classList.remove('d-none') } } -document.getElementById('gifs-back-btn').onclick = getGifs; -document.getElementById('gifs-cancel-btn').onclick = getGifs; +gifSearchBar.onchange = ()=>{searchGifs(gifSearchBar.value)}; diff --git a/files/templates/modals/gif.html b/files/templates/modals/gif.html index 366ba9685c..8664a8f73d 100644 --- a/files/templates/modals/gif.html +++ b/files/templates/modals/gif.html @@ -4,22 +4,20 @@