search.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. document.addEventListener("DOMContentLoaded", async () => {
  2. document.getElementById("account-button-anchor").href = `/user/${encodeURIComponent(Cookies.get('user'))}`
  3. if (Cookies.get("user") != null) {
  4. document.getElementById("login-button").classList.add("disabled");
  5. document.getElementById("logout-button").classList.remove("disabled");
  6. document.getElementById("upload-button").classList.remove("disabled");
  7. document.getElementById("account-button").classList.remove("disabled");
  8. }
  9. const urlParams = new URLSearchParams(window.location.search);
  10. if(!urlParams.has("query")) alert("you must search something to see results");
  11. const results = await search(urlParams.get("query"));
  12. if(results.status != 200) {
  13. console.error(results);
  14. return alert("ERRORE ERRORE ERRORE");
  15. }
  16. if(results.data.length < 1) {
  17. document.getElementById("no-results").innerText = `the search query "${urlParams.get("query")}" on the site "skibidihub" is not bringing up any results on the site "skibidihub"`
  18. } else {
  19. document.getElementById("no-results").classList.add("disabled");
  20. }
  21. results.data.forEach(video => {
  22. makeVideo(video);
  23. });
  24. })
  25. async function search(query) {
  26. return await axios.post("/api/search", { query: query }).then(data => {
  27. return data;
  28. }).catch(error => {
  29. console.error(error);
  30. throw new Error("search error: ", error)
  31. })
  32. }
  33. function makeVideo(video) {
  34. // search-video
  35. const parent = document.createElement("div")
  36. parent.classList.add("search-video");
  37. // thumbnail anchor
  38. const thumbnailAnchor = document.createElement("a")
  39. thumbnailAnchor.classList.add("anchor");
  40. thumbnailAnchor.setAttribute("href", `/video/${video.id}`);
  41. parent.appendChild(thumbnailAnchor);
  42. // search-thumbnail
  43. const thumbnail = document.createElement("img");
  44. thumbnail.classList.add("search-thumbnail");
  45. thumbnail.setAttribute("height", "155px");
  46. thumbnail.setAttribute("width", "275px");
  47. thumbnail.setAttribute("src", `/api/thumbnail/${video.id}`);
  48. thumbnailAnchor.appendChild(thumbnail);
  49. // search-video-info
  50. const videoInfo = document.createElement("div");
  51. videoInfo.classList.add("search-video-info");
  52. parent.appendChild(videoInfo);
  53. // title anchor
  54. const titleAnchor = document.createElement("a");
  55. titleAnchor.classList.add("anchor");
  56. titleAnchor.setAttribute("href", `/video/${video.id}`);
  57. videoInfo.appendChild(titleAnchor);
  58. // video title
  59. const title = document.createElement("h2");
  60. title.innerText = video.title;
  61. titleAnchor.appendChild(title);
  62. // author anchor
  63. const authorAnchor = document.createElement("a");
  64. authorAnchor.classList.add("anchor");
  65. authorAnchor.setAttribute("href", `/user/${encodeURIComponent(video.uploader)}`);
  66. videoInfo.appendChild(authorAnchor);
  67. // author
  68. const author = document.createElement("p");
  69. author.innerText = `Uploaded by: ${video.uploader}`;
  70. authorAnchor.appendChild(author);
  71. document.getElementById("videos").appendChild(parent);
  72. }