Advertisement
Guest User

Untitled

a guest
Oct 4th, 2021
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import EventEmitter from "eventemitter3";
  2. import image from "../images/planet.svg";
  3.  
  4. export default class Application extends EventEmitter {
  5.   static get events() {
  6.     return {
  7.       READY: "ready",
  8.     };
  9.   }
  10.  
  11.   constructor() {
  12.     super();
  13.  
  14.     this._load();
  15.     this._create();
  16.     this._startLoading();
  17.     this._stopLoading();
  18.     this._loading = document.querySelector('progress');
  19.  
  20.     const box = document.createElement("div");
  21.     box.classList.add("box");
  22.    
  23.     //I suppose this is where the values from _load() should be put into _method()
  24.     box.innerHTML = this._render({
  25.       name: 'Placeholder',
  26.       terrain: "placeholder",
  27.       population: 0,
  28.     });
  29.  
  30.     document.body.querySelector(".main").appendChild(box);
  31.  
  32.     this.emit(Application.events.READY);
  33.   }
  34.  
  35.   async _load() {
  36.  
  37.     //function that gets the data for each planets
  38.     async function getPlanets() {
  39.       const urls = Array.from({ length: 7 },
  40.         (v, i) => `https://swapi.boom.dev/api/planets?page=${i + 1}`);
  41.  
  42.       const promises = urls.map(url => fetch(url)
  43.         .then(res => res.json())
  44.         .then(data => data.results));
  45.  
  46.       const planetData = (await Promise.all(promises)).flat();
  47.       console.log('Results:', planetData);
  48.      
  49.       //arrays where I filter the necessary data from planetData - name, terrain, population
  50.       const names = [];
  51.       const terrain = [];
  52.       const population = [];
  53.  
  54.       for (let i = 0; i < planetData.length; i++) {
  55.         names.push(planetData[i].name);
  56.         terrain.push(planetData[i].terrain);
  57.         population.push(planetData[i].population);
  58.       }
  59.       console.log(names);
  60.     }
  61.     getPlanets();
  62.   }
  63.  
  64.   _render({ name, terrain, population }) {
  65.     return `
  66. <article class="media">
  67.   <div class="media-left">
  68.     <figure class="image is-64x64">
  69.       <img src="${image}" alt="planet">
  70.     </figure>
  71.   </div>
  72.   <div class="media-content">
  73.     <div class="content">
  74.     <h4>${name}</h4>
  75.       <p>
  76.         <span class="tag">${terrain}</span> <span class="tag">${population}</span>
  77.         <br>
  78.       </p>
  79.     </div>
  80.   </div>
  81. </article>
  82.     `;
  83.   }
  84.  
  85.   //here should be moved the rendering of the boxes, I suppose by establishing a connection with _render()
  86.   _create() { }
  87.  
  88.   // we can leave these two alone for now
  89.   _startLoading() { }
  90.  
  91.   _stopLoading() { }
  92.  
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement