maotube/public/search/script.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-01-19 02:10:56 +00:00
function error(message) {
const errorContainer = document.getElementById("mao-error");
const errorElement = document.getElementById("mao-error-message");
errorElement.innerText = message;
errorContainer.classList.remove("hidden");
}
function displayResponse(response) {
if (!response.ok) {
error(response.error);
return;
}
const { videos, total } = response;
if (videos.length === 0) {
error("search returned no results");
return;
}
2024-02-10 22:30:03 +00:00
console.log(videos);
2024-01-19 02:10:56 +00:00
const resultElement = document.getElementById("result");
resultElement.innerHTML = `<p>Showing ${videos.length}/${total} results.</p>`
+ "<ul id='video-list'>"
+ videos
.map(v => {
2024-02-10 22:30:03 +00:00
console.log(v);
2024-01-19 03:22:22 +00:00
return `<li><p><a href="/watch?id=${v.id}">${v.title}</a> - ${v.author}</p></li>`
2024-01-19 02:10:56 +00:00
})
.join("")
+ "</ul>";
}
function main() {
const params = new URLSearchParams(window.location.search);
2024-01-19 03:04:21 +00:00
const searchElement = document.getElementById("query");
searchElement.value = params.get("query") || "";
2024-01-19 02:10:56 +00:00
return fetch(`/api/search?${params.toString()}`)
.then(v => v.json())
.then(displayResponse);
}
main();