Advertisement
informaticage

Javascript page render

Nov 15th, 2022
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const SERVER_ADDRESS = "http://192.168.131.4:3000/api";
  2.  
  3. function renderShopItem(shop, index = 0) {
  4.     return `
  5.         <div class="shop-item">
  6.             <header>
  7.                 <h3> ${shop.title} </h3>
  8.             </header>
  9.  
  10.             <div class="shop-img">
  11.                 <img src="${shop.image}?${index}">
  12.             </div>
  13.  
  14.             <footer>
  15.                 Address: ${shop.geographicArea}
  16.             </footer>
  17.         </div>
  18.     `;
  19. }
  20.  
  21. async function retrieveFromServer() {
  22.     const result = await fetch(SERVER_ADDRESS);
  23.     return await result.json();
  24. }
  25.  
  26. function setShopList(rendered) {
  27.     document.querySelector("#shops-list")
  28.         .innerHTML = rendered.join("<hr>");
  29. }
  30.  
  31. function filterShopsByGeolocation(shops, area) {
  32.     return shops.filter(
  33.         (shop) => shop.geographicArea.toLowerCase() ===
  34.             area.toLowerCase()
  35.     );
  36. }
  37.  
  38. function renderHtmlPage(shops) {
  39.     const rendered = shops.map(
  40.         (shop, index) => renderShopItem(shop, index)
  41.     );
  42.     setShopList(rendered);
  43. }
  44.  
  45. let result;
  46. let shops;
  47.  
  48. function random(max) {
  49.     return Math.round((Math.random() * 100) % (max-1));
  50. }
  51.  
  52. async function onChangeCityButtonPressed() {
  53.     result = await retrieveFromServer();
  54.     const cityChoice = document.querySelector("#city-choice")?.value ?? "";
  55.     shops = filterShopsByGeolocation(result, cityChoice);
  56.     renderHtmlPage(shops);
  57. }
  58.  
  59. async function main() {
  60.     result = await retrieveFromServer();
  61.     shops = filterShopsByGeolocation(result, "Lecce");
  62.  
  63.     renderHtmlPage(shops);
  64. }
  65.  
  66. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement