EntropyStarRover

01. Bus stop

Feb 25th, 2021 (edited)
580
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getInfo() {
  2.     let input = document.getElementById("stopId");
  3.     let stopNameDiv = document.getElementById("stopName");
  4.     let bussesUl = document.getElementById("buses");
  5.  
  6.     let stopCode = input.value;
  7.  
  8.     var requestOptions = {
  9.         method: 'GET',
  10.         redirect: 'follow'
  11.     };
  12.  
  13.  
  14.     fetch(`http://localhost:3030/jsonstore/bus/businfo/${stopCode}`, requestOptions)
  15.         .then(response => response.json())
  16.         .then(o => populate(o))
  17.         .catch(error => wrongId());
  18.  
  19.  
  20.     function populate(obj) {
  21.         bussesUl.innerHTML="";
  22.         stopNameDiv.textContent = obj.name;
  23.         let bussesArr = Array.from(Object.entries(obj.buses));
  24.         bussesArr.forEach(b => {
  25.             let busLi = document.createElement("li");
  26.             busLi.textContent = `Bus ${b[0]} arrives in ${b[1]} minutes`;
  27.             bussesUl.appendChild(busLi);
  28.         });
  29.         input.value = "";
  30.     }
  31.  
  32.     function wrongId() {
  33.         stopNameDiv.textContent = "Error";
  34.         bussesUl.innerHTML = "";
  35.     }
  36. }
RAW Paste Data