maotube/public/search/script.js

57 lines
1.5 KiB
JavaScript
Raw Permalink 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-02-10 23:02:33 +00:00
return `
<li>
2024-02-10 23:28:33 +00:00
<div class="video-item">
<div class="video-image">
<img class="shadow" src="/videos/${v.id}.png" alt="">
<img class="non-shadow" src="/videos/${v.id}.png" alt="">
</div>
2024-02-10 23:02:33 +00:00
<span class="video-info">
<a href="/watch?id=${v.id}">${v.title}</a>
<br>
by ${v.author}
</span>
2024-02-10 23:28:33 +00:00
</div>
2024-02-10 23:02:33 +00:00
</li>
`;
2024-01-19 02:10:56 +00:00
})
2024-02-10 23:02:33 +00:00
.join("")
2024-01-19 02:10:56 +00:00
+ "</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();