Advertisement
talalaite

Untitled

Mar 31st, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.getElementById("logout").addEventListener("click", () => {
  2.   localStorage.removeItem("user");
  3.   location.replace("login.html");
  4. });
  5.  
  6. fetch(
  7.   `https://backend-test-js-eovio.ondigitalocean.app/getsome/${localStorage.getItem(
  8.     "user"
  9.   )}`
  10. )
  11.   .then((res) => res.json())
  12.   .then((data) => displayCourses(data));
  13.  
  14. //   data.forEach((item) => console.log(item)));
  15. //   displayCourses(data));
  16.  
  17. function displayCourses(array) {
  18.   array.forEach((item) => {
  19.     const img = document.createElement("img");
  20.     img.src = item.url;
  21.  
  22.     const title = document.createElement("h4");
  23.     title.textContent = item.title;
  24.  
  25.     const params = document.createElement("div");
  26.     params.className = "params";
  27.  
  28.     // Game
  29.     if (item.parameters.game) {
  30.       const game = document.createElement("div");
  31.       game.className = "game";
  32.       params.append(game);
  33.     }
  34.  
  35.     // Duration
  36.     const time = document.createElement("div");
  37.     time.className = "time";
  38.     const vidlength = item.parameters.vidlength;
  39.  
  40.     if (vidlength % 60 === 0) {
  41.       const hours = vidlength / 60;
  42.       time.textContent = hours + "h";
  43.     } else {
  44.       const hours = vidlength / 60;
  45.       const roundedh = Math.floor(hours);
  46.       const minutes = (hours - roundedh) * 60;
  47.       const roundedmin = Math.round(minutes);
  48.       time.textContent = roundedh + "h " + roundedmin + "min";
  49.     }
  50.  
  51.     params.append(time);
  52.  
  53.     // Beginner
  54.     if (item.parameters.beginner) {
  55.       const beginner = document.createElement("div");
  56.       beginner.className = "beginner";
  57.       beginner.textContent = "Beginner";
  58.       params.append(beginner);
  59.     }
  60.  
  61.     //   CC
  62.     if (item.parameters.cc) {
  63.       const cc = document.createElement("div");
  64.       cc.className = "cc";
  65.       cc.textContent = "CC";
  66.       params.append(cc);
  67.     }
  68.  
  69.     const oldprice = document.createElement("div");
  70.     oldprice.className = "oldprice";
  71.     oldprice.textContent = `€${item.oldprice}`;
  72.  
  73.     const newprice = document.createElement("div");
  74.     newprice.className = "newprice";
  75.     newprice.textContent = `€${item.newprice}`;
  76.  
  77.     const price = document.createElement("div");
  78.     price.className = "price";
  79.     price.append(oldprice, newprice);
  80.  
  81.     const course = document.createElement("div");
  82.     course.className = "course";
  83.     course.append(img, title, params, price);
  84.  
  85.     document.getElementById("content").append(course);
  86.   });
  87. }
  88.  
  89. // allcourses
  90. const allcourses = document.getElementById("allcourses");
  91. allcourses.addEventListener("click", () => {
  92.   fetch(
  93.     `https://backend-test-js-eovio.ondigitalocean.app/getsome/${localStorage.getItem(
  94.       "user"
  95.     )}`
  96.   )
  97.     .then((res) => res.json())
  98.     .then((data) => displayCourses(data));
  99. });
  100.  
  101. // search
  102. fetch(
  103.   `https://backend-test-js-eovio.ondigitalocean.app/getall/${localStorage.getItem(
  104.     "user"
  105.   )}`
  106. )
  107.   .then((res) => res.json())
  108.   .then((data) => {
  109.     document.getElementById("search").addEventListener("keyup", () => {
  110.       displayCourses(
  111.         data.filter((v) =>
  112.           v.title
  113.             .toLowerCase()
  114.             .includes(document.getElementById("search").value.toLowerCase())
  115.         )
  116.       );
  117.     });
  118.   });
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement