Advertisement
cindex1a

Untitled

Jun 7th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const API_KEY = "16d85bf702974259b17e4dff4faeade4";
  2. const BASE_URL = "https://api.football-data.org/v2/";
  3.  
  4. const LEAGUE_ID = 2021;
  5.  
  6. const ENDPOINT_COMPETITION = `${BASE_URL}competitions/${LEAGUE_ID}/standings`;
  7. const endpoint_team = `${BASE_URL}/v2/teams/${LEAGUE_ID}`;
  8.  
  9. const fetchAPI = url => {
  10.     return fetch(url, {
  11.         headers: {
  12.             'X-Auth-Token': API_KEY
  13.         }
  14.     })
  15.         .then(res => {
  16.             if (res.status !== 200) {
  17.                 console.log("Error: " + res.status);
  18.                 return Promise.reject(new Error(res.statusText))
  19.             } else {
  20.                 return Promise.resolve(res)
  21.             }
  22.         })
  23.         .then(res => res.json())
  24.         .catch(err => {
  25.             console.log(err)
  26.         })
  27. };
  28.  
  29. function getAllStandings() {
  30.     if ("caches" in window) {
  31.         caches.match(ENDPOINT_COMPETITION).then(function (response) {
  32.             if (response) {
  33.                 response.json().then(function (data) {
  34.                     console.log("Competition Data: " + data);
  35.                     showStanding(data);
  36.                 })
  37.             }
  38.         })
  39.     }
  40.  
  41.     fetchAPI(ENDPOINT_COMPETITION)
  42.         .then(data => {
  43.             showStanding(data);
  44.         })
  45.         .catch(error => {
  46.             console.log(error)
  47.         })
  48. }
  49.  
  50. function showStanding(data) {
  51.     let standings = "";
  52.     let standingElement =  document.getElementById("homeStandings");
  53.  
  54.     data.standings[0].table.forEach(function (standing) {
  55.         standings += `
  56.                 <tr>
  57.                     <td><img src="${standing.team.crestUrl.replace(/^http:\/\//i, 'https://')}" width="30px" alt="badge"/></td>
  58.                     <td>${standing.team.name}</td>
  59.                     <td>${standing.won}</td>
  60.                     <td>${standing.draw}</td>
  61.                     <td>${standing.lost}</td>
  62.                     <td>${standing.points}</td>
  63.                     <td>${standing.goalsFor}</td>
  64.                     <td>${standing.goalsAgainst}</td>
  65.                     <td>${standing.goalDifference}</td>
  66.                 </tr>
  67.         `;
  68.     });
  69.  
  70.      standingElement.innerHTML = `
  71.                 <div class="card" style="padding-left: 24px; padding-right: 24px; margin-top: 30px;">
  72.  
  73.                 <table class="striped responsive-table">
  74.                     <thead>
  75.                         <tr>
  76.                             <th></th>
  77.                             <th>Team Name</th>
  78.                             <th>W</th>
  79.                             <th>D</th>
  80.                             <th>L</th>
  81.                             <th>P</th>
  82.                             <th>GF</th>
  83.                             <th>GA</th>
  84.                             <th>GD</th>
  85.                         </tr>
  86.                      </thead>
  87.                     <tbody id="standings">
  88.                         ${standings}
  89.                     </tbody>
  90.                 </table>
  91.                
  92.                 </div>
  93.     `;
  94. }
  95.  
  96.     function getAllTeam() {
  97.         if ("caches" in window) {
  98.             caches.match(endpoint_team).then(function (response) {
  99.                 if (response) {
  100.                     response.json().then(function (data1) {
  101.                         console.log("team: " + data1);
  102.                         showTeam(data1);
  103.                     })
  104.                 }
  105.             })
  106.         }
  107.  
  108.         fetchAPI(endpoint_team)
  109.             .then(data1 => {
  110.                 showTeam(data1);
  111.             })
  112.             .catch(error => {
  113.                 console.log(error)
  114.             })
  115.     }
  116.  
  117.     function showTeam (data1) {
  118.         let teams = "";
  119.     let teamElement = document.getElementById("teams");
  120.  
  121.     data.teams.forEach(function (team) {
  122.         teams += `
  123.                 <a href="./detail.html?id=${team.id}" style="color:black;">
  124.                     <div class="col s12 m4 l3" style="float: left; height: 40rem; margin: 0; padding: 5px; ">
  125.                         <div class="card">
  126.                             <div class="card-image" style="height : 15rem;">
  127.                             <img src="${team.crestUrl}" style="margin: auto; padding: 1rem 1rem 0 1rem; height: 100%; width:auto; max-width: 100%; ">
  128.                             </div>
  129.                             <div class="card-content" style="padding-top: 0.5rem; height : 6rem;">
  130.                             <h5><strong>${team.name}</strong></h5>
  131.                             </div>
  132.                             <div class="card-action">
  133.                             <a href="./detail.html?id=${team.id}" style="float: left; ">Read More</a>
  134.                             </div>
  135.                         </div>
  136.                     </div>
  137.                 </a>
  138.                 `;
  139.     });
  140.  
  141.     teamElement.innerHTML = `
  142.         <div class="row">    
  143.         ${teams}
  144.         </div>
  145.     `;
  146.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement